Topic: Need to print pointer address to uart without using printf or sprintf (Read 5625 times)previous topic - next topic

I would like to print a pointer address to the uart without using printf or sprintf with %p parameter. Is this possible? Including these functions takes up too much space in my bootloader. I have found Can you store the pointer address in a variable and print that out? The only function that I have to print to uart is putchar(char ch).

The Arduino print functions do not have a version that is defined to print a pointer to int.

The sketch is compiled as part of a C++ program, and C++ enforces a certain amount of data type integrity. The C++ compiler won't let you assign a pointer value to some non-pointer data type, so you have to use a cast of some kind. For me, the simplest is just an old C-style cast.

The only thing is that you have to make sure the size of the integer data type is the same size as the pointer, so the method is not portable.In other words, when you are casting a pointer to another data type it is up to you to make sure they are commensurate.

Now, with avr-gcc (Arduino's compiler) the size of an int is two bytes and the size of a pointer is two bytes.

You can verify this by running something like the following. Note that sprintf does support printing with %p format specifier, so I use that to compare output with various ways of using casts.

Bottom line: Whatever method you use to put integer values to your uart can be used to put pointer values. At the receiving end, the program reading the integer value can cast it to a pointer type and use it for whatever it needs to do.

Regards,

DaveFootnote:Note that sprintf with "%p" always prints hex and the version in avr-libc (like all GNU compiler libraries that I have used) always puts "0x" in front of it. By using a cast to unsigned int instead of sprintf, you can print with whatever base you want. As long as the sender and receiver agree on the exact format, All is Good.

Being able to do something does not mean that it is smart to do it. Typically, pointers are useful only on the system they were defined on. So, what value does the pointer you are trying to send have on another system? In other words, why are you trying to write a pointer's address to the serial port?

Thanks for the replies. I wasn't sure if I would get any. Looks like I will have something to try out later today when I get back home.

The pointer is a uint8_t *, which I believe is an unsigned char. Any ideas on to put that to the putchar(char ch)?

To answer your questions. Can't use Serial.print, I am in the bootloader. I am not planning on using ptr address for my program, just debugging code, trying to figure what is going in a bootloader that I downloaded from internet.

How do you know that the pointer will be a 16-bit address? I looked at the avr-libc pages. They indicate that a uint16_t is an unsigned integer and is 16 bit. On some other places on the internet an unsigned integer is a 4 byte variable (or 32 bit). Not sure why there is a difference here, maybe depends on the system you are using the variable on.

The pointer value that I am trying to display is iterating. I try to display it after each iteration, but the value doesn't update when outputed to uart. Are there issues regarding outputing a pointer at runtime?

How do you know that the pointer will be a 16-bit address? I looked at the avr-libc pages. They indicate that a uint16_t is an unsigned integer and is 16 bit. On some other places on the internet an unsigned integer is a 4 byte variable (or 32 bit). Not sure why there is a difference here, maybe depends on the system you are using the variable on.

A pointer is just a memory address, so the size of the variable storing the pointer value has to match the size of the memory address. I was assuming you were using an ATmega328 or 168, which have 16-bit memory addresses. So my code would not work on a processor with a larger memory space. A uint16_t is always 16-bit by definition, whereas the size of an int varies depending on the platform and compiler.

Quote

The pointer value that I am trying to display is iterating. I try to display it after each iteration, but the value doesn't update when outputed to uart. Are there issues regarding outputing a pointer at runtime?

You'll need to re-read the pointer each time it changes. So after every iteration you'll need to do this line before writing to the serial port: