[oclug] time for my weekly dumb question -> C and memory
Taavi Burns
tburns at ualberta.ca
Mon Apr 2 09:46:04 EDT 2001
On Sun, 1 Apr 2001, Greg Sarsons wrote:
> this may explain the difficulty I'm having. So how do you then copy using
> strcpy when you have a struct that contains zero length strings like are in
> my struct message example?
>
> I'm trying to send a message but the behaviour is not stable. For example,
> given the following:
>
> typedef struct{
> unsigned short url_lenght;
> char url[0];
> unsigned short lengthOfScopeList;
> char scope_list[0];
> } ServiceAdvert;
Another similar problem can be caused by standard compiler optimisations:
char StringA[] = "Hello.";
char StringB[] = "Hello.";
StringA[0] = 'B';
puts(StringB); /* prints "Bello." */
Because the strings are statically allocated, some compilers will in some
situations save space by only keeping one copy of two identical strings
(but not always; depends on the compiler and optimisation settings). I
_have_ seen it happen.
> char recvBuffer[100]; /*buffer used to send message*/
>
> ServiceAdvert *aMessage;
>
> aMessage = (ServiceAdvert *) recvBuffer;
>
> strcpy(aMessage->url, "www.somewhere.come");
Hmmm...you need to have a structure in memory that you can just dump to
output, eh? Perhaps you could use char* in your structure, and then
aMessage->url = strdup("www.somewhere.com"); to set it. Your output
procedures could then pull the string in before actually outputting. It's
much easier to manipulate things in C when you use pointers (that's why
they exist!).
>
>
> I always get the correct length if I use strlen(aMessage->url);
>
> However, as soon as I do a strcpy into the other zero length string,
> scope_list, the url is messed up.
>
> My intent is to then send this message.
>
> Greg
>
> On Sunday 01 April 2001 11:12, you wrote:
> > Greg Sarsons wrote:
> > > struct message {
> > >
> > > char aString[0];
> > >
> > > }
> > >
>
> >
> > Also,
> > strcpy("fred", string3) ; /* is legal, works fine */
> > but any of
> > strcpy("fred", string1) ;
> > strcpy("fred", string2) ;
> > strcpy("susan", string3) ;
> > go off the end of the array with unpredictable results.
>
> >
> > There's only one use I know of for
> >
> > struct message {
> > ...
> > char aString[0];
> > }
> >
> > That's when the actual structure is variable-sized. In use there'll be
> > a non-zero length string after the message header and you can access it
> > as the aString element. When you're declaring the function, though, you
> > don't know its size so you pretend it is zero.
> >
> > I know this is moderately widely used, works fine with many compilers,
> > but I'm not certain if it is actually sanctified by ANSI, legal in the
> > C standard.
> _______________________________________________
> oclug mailing list
> oclug at lists.oclug.on.ca
> http://www.oclug.on.ca/mailman/listinfo/oclug
>
taa
Science is but a perversion of itself unless it has
as its ultimate goal the betterment of humanity.
--Nikola Tesla
/*eof*/
More information about the OCLUG
mailing list