Runtime detection of PROGMEM char pointers

Hi all,

Is there a way to detect if a pointer is pointing to a PROGMEM character array and not to RAM?

I have some classes that use a lot of string but for some of those classes I need to define the strings at run time.

minimalistic example code.

char string1[] PROGMEM = "This is a string in PROGMEM";

char string2[] = "This is a string in RAM";
setup() {
    char *temp1, *temp2;

    temp1 = string1;
    temp2 = string2;
}

How would I detect that temp1 is a string in PROGMEM and temp2 is a string in RAM?

I don't believe you can access strings stored in flash memory using C pointers.

Lefty

You can't, you're right.

The question was not if I can or can't access the string with a normal pointer the question was is there a way to detect if the pointer is a PROGMEM pointer or a RAM pointer after it's stored in a pointer variable.

is there a way to detect if the pointer is a PROGMEM pointer or a RAM pointer after it’s stored in a pointer variable.

No.

is there a way to detect if the pointer is a PROGMEM pointer

Well, if the pointer is larger than the highest RAM location, or lower than the start of RAM, you are probably OK assuming that it's a PROGMEM pointer. Since there is only 2K+256 bytes of address space that is "overlapping", that might not be TOO difficult to ensure...

Apparently progmem is an attribute that is only associated with variables, not with types. (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38342 )

westfw:

is there a way to detect if the pointer is a PROGMEM pointer

Well, if the pointer is larger than the highest RAM location, or lower than the start of RAM, you are probably OK assuming that it's a PROGMEM pointer. Since there is only 2K+256 bytes of address space that is "overlapping", that might not be TOO difficult to ensure...

Apparently progmem is an attribute that is only associated with variables, not with types. (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38342 )

Yeah I figured something like that. I'm working on something that would let the program detect a pointer but it involves changing the linker script and some defines around the pointer being used. I'll post an example when I got it working properly.