In message <50650000000500000093DF0100-1079395200000@exchange.ni.com>,
Rafi2003 writes
>Thanks Al,
>
>I noted your remarks and experimented with them. It looks fine!
>
>Back to my other question regarding the memory allocation.
>
>As a reminder: I declared in a local subroutine
> char *Title=NULL;
>
>Then, throughout my procedure I set Title to differnt values:
> Title = "first message";
> ------
> ------
> Title = "Second message";
>
>I do it throught my program and never had any problem with it (I use
>CVI 7.0). I compile my project in release mode and still, no problem
>with these messages.
>
>Can there be any explenation why it is working?
>
>From you answere it is indicated that me
mory must be allocated. If
>I'll have to do it I will choose declaring a fix buffer like
> char Title[256];
>and then just use it in a form-
> strcpy(Title, "this is my message");
>
>If I must do that it means modifying my program in hundreds
>places..... I really like to be sure I must do it. (this is why I
>asked your assistance in understanding how it workded)
>
>Thank you
>Rafi
The examples you show should be OK. C compilers must allocate memory for
constants such as "this is my message" and your variable, Title, is able
to store addresses of such constants. No additional memory allocation is
required.
However, this is only safe if your programme only ever points to
constants. Strings generated at run time will not have memory allocated.
In the following example the compiler will not allocate (or cause to be
allocated) memory for the string that results from sprintf().
char *Title=NULL;
int i=25;
sprintf (Title , "%d" , i );
Another less obvious bad example;
char *Title1 = "String 1";
char *Title2 = "String2";
strcat (Title1, Title2);
Most questions of this type (I.E. relating to ANSI C) have been answered
here;
http://www.eskimo.com/~scs
--
Regards,
John Cameron.
Type softly, read gently.