Hello,
why you do you declare a string in that way?
char * f = "Hello";
'char* f' declares only a pointer to something. In this declaration
"Hello" should be treated as constant char array by the compiler. Does
the compiler complains about assigning const to non-const if you turn on
-Wall???
Correct way to declare a pre-defined char array containing "Hello" would be:
char f[] = "Hello";
That's a different! May the first line result in some confusions in the
compiler?!?
Sten
Robert Adsett wrote:
> At 01:51 AM 10/18/05 +0000, embeddedjanitor wrote:
>
>>--- In lpc2000@yahoogroups.com, "Guillermo Prandi" <yahoo.messenger@m
>>...> wrote:
>>
>>>I too gave a suspicious look at the startup code. But the thing is,
>>>in order to copy the modifiable data to RAM it must be in the ROM in
>>>the first place! It doesn't show in the lst file, whilst puts()
>>>parameters do.
>>
>>They will be in a different area.
>
>
> Good point.
>
>
>
>>A constant string will be on the rommable constants area (.rodata in
>>GNU talk).
>>
>>A modifiable string will be in the data initialisation section (.data
>>in GNU talk). This is rommed, but is copied into RAM for the
>>execution.
>>
>>
>>>I tried giving a const to the function's declaration, with no luck.
>>
>>No luck expected :-). consting a function just says that the function
>>will not try to modify the string that it is passed. It does not
>>modify the placement of the string.
>
>
> But it will make a difference in this case.
>
> Case 1:
> void myfun( char *);
>
> myfun("test");
>
> In this case we have a pointer to a modifiable string and therefore in ram.
>
> Case 2:
>
> void myfun( const char *)
>
> myfun("test");
>
> In this case we have a pointer to a non modifiable string and which can be
> in flash. If this isn't true then printf wouldn't work given your above
> and the OP has already stated that
>
> printf("test");
>
> works.
>
> In the third case
>
> void myfun( const char *);
> void myfun2( char *);
> char * f = "test";
>
> myfun(f);
> myfun2(f);
>
> The source is the same and whether the argument is const makes no difference.
>
> I think your diagnosis is likely right unless Case 2 above behaves
> differently then printf. If it does something else is going on.
>
>
>>>However, I narrowed down the problem to auto-inlining: if I move the
>>>do_something() function to another source (I was testing with an
>>>empty body on the function!), the strings do show in the .data. This
>>>kinda makes sense, since the compiler realized that there was no
>>>point for that constant to be in the code in the first place. What
>
>
> Um, well yeah, with
>
> static void myfunc( const char *)
> {
> }
>
> func()
> {
> myfunc("test");
> }
>
> the compiler could quite legitimately optimize myfunc out of
> existence. Even if it wasn't declared as a static function the compiler
> could optimize the call away and leave the hook for any external callers. I
> was sort of assuming you actually did something with the string, otherwise
> how would you notice? Checking for a Copyright in the image maybe?
>
> Robert
>
>
> " 'Freedom' has no meaning of itself. There are always restrictions, be
> they legal, genetic, or physical. If you don't believe me, try to chew a
> radio signal. " -- Kelvin Throop, III
> http://www.aeolusdevelopment.com/
>
>
>
>
>
> Yahoo! Groups Links
>
>
>
>
>
>
>
>
--
/************************************************
Do you need a tiny and efficient real time
operating system (RTOS) with a preemtive
multitasking for LPC2000 or AT91SAM7?
http://nanortos.net-attack.de/
Or some open-source tools and code for LPC2000?
http://www.net-attack.de/
************************************************/Message
Re: [lpc2000] Re: Problems with sting constants and gcc -On
2005-10-18 by Sten
Attachments
- No local attachments were found for this message.