[oclug] time for my weekly dumb question -> C and memory

David F. Skoll dfs at roaringpenguin.com
Sun Apr 1 21:11:15 EDT 2001


On Sun, 1 Apr 2001, Greg Sarsons wrote:

> typedef struct{
> 	unsigned short url_lenght;
> 	char url[0];
> 	unsigned short lengthOfScopeList;
> 	char scope_list[0];
> } ServiceAdvert;

That is not a valid C structure, and I'm amazed anything will compile
it.  Zero-length arrays are meaningless, and are allowed at the end of a
structure (only) as a hack by some compilers.

> However, as soon as I do a strcpy into the other zero length string,
> scope_list, the url is messed up.

Look at the memory layout of the structure.  Anything you write into url
will overwrite lengthOfScopeList and probably scope_list.

--
David.





More information about the OCLUG mailing list