EmbDev.net

Forum: ARM programming with GCC/GNU tools Easy (I hope) C const pointer question


Author: jrmymllr jrmymllr (jrmymllr)
Posted on:

Rate this post
0 useful
not useful
I have this array:
const unsigned char *param_tag[]=        
{
  "SMSH=",
  "SMIN=",
  "SMUS=",
  "SMPA=",
  "SMDO=",
  "SYIP=",
  "SYNE=",
  "SYGA=",
  "SYDN=",
  "SYIM="
};

I had thought that the const would put everything into Flash, however 
according to the .map file it appears that the strings are in Flash, but 
the pointers to each of the 10 strings are being placed into RAM.  It's 
using 40 bytes of RAM: 32 bit micro, 4bytes * 10 strings = 40.

How do I put everything into Flash?  I'm not even sure what to search 
for to answer this myself.  My guess is the answer is easy if you know!

Author: A. K. (prx)
Posted on:

Rate this post
0 useful
not useful
A pointer to a constant ist not a constant pointer.

const unsigned char *const param_tag[] = ...

Author: jrmymllr jrmymllr (jrmymllr)
Posted on:

Rate this post
0 useful
not useful
A. K. wrote:
> A pointer to a constant ist not a constant pointer.
>
> const unsigned char *const param_tag[] = ...

Great, that fixed it!

Author: Rolf Magnus (Guest)
Posted on:

Rate this post
0 useful
not useful
> I have this array:
const unsigned char *param_tag[]=        
{
  "SMSH=",
  "SMIN=",
  "SMUS=",
  "SMPA=",
  "SMDO=",
  "SYIP=",
  "SYNE=",
  "SYGA=",
  "SYDN=",
  "SYIM="
};

Do you actually need the pointers? If all arrays are the same size, you 
can also make it an array of arrays and save the pointers.
const unsigned char param_tag[][6]=
{
  "SMSH=",
  "SMIN=",
  "SMUS=",
  "SMPA=",
  "SMDO=",
  "SYIP=",
  "SYNE=",
  "SYGA=",
  "SYDN=",
  "SYIM="
};

Btw: Why unsigned char? There is no such thing as signedness for 
characters, so if those are strings, the element type should be char.

Author: jrmymllr jrmymllr (jrmymllr)
Posted on:

Rate this post
0 useful
not useful
Rolf Magnus wrote:

>
> Do you actually need the pointers? If all arrays are the same size, you
> can also make it an array of arrays and save the pointers.

That's true.  I have another array similar to this one, but the strings 
are different sizes, so I sort of copied and pasted and modified.  But 
good point.

>
> Btw: Why unsigned char? There is no such thing as signedness for
> characters, so if those are strings, the element type should be char.

The only answer I can give is because I'm a novice :)

I've occasionally wondered about this but just kept going as long as it 
worked and I had no warnings.  After initially shunning C, I decided to 
teach it to myself and have learned the hard way.  I was one of those in 
college who said "I don't want to do any programming".  Now I use all of 
it for hobbies and none for my real job.

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.