One very dangerous thing about your code is you have an unqualified
"char *" pointing to a string literal. String literals must never be
modified. so it would be better to declare the struct member as

const char *Foo;

or alternatively, leave it un-const but have it point to the start of a
"normal" char array rather than a string literal, e.g.

bar = malloc(sizeof(struct Frozz));
bar->Foo = malloc(strlen("Test") + 1);
strcpy(bar->Foo, "Test");

The array can then be modified.

Warning: I've not tested this.

Daniel.

On Sun, 25 Nov 2001, Steve wrote:

>
> Hi all,
>
>  I was just wondering the 'correct' method to handle something like:
>   struct Frozz {
>       int a;
>       char *Foo;
>   };
>
>   struct Frozz *bar;
>   bar = malloc(sizeof(struct Frozz));
>   bar->Foo = "Test";
>
>  LCLint complaines about implicit only not free'd or some such on the
> 'bar->Foo...' line.  Could someone
> point out the common way of handling such cases ?
>
> Also, is there a 'cookbook' somewhere with common program
> constructs/methods/etc and how to decorate them ?
> for instance, a function returning a pointer to memory it dynamically
> allocates ?  I found the users guide (on-line v2.4 html) very in-depth on
> the WHAT lclint does, but very light on HOW to use it.  Was there another
> doc/faq/guide I should be RTFM'ing ??
>
> Steve

Reply via email to