Mail Archives: djgpp/2000/08/24/00:18:27
"Radical NetSurfer" <radsmail AT juno DOT com> wrote in message
news:s269qsgaqr5446ot429cat1q8vo7rq5t9c AT 4ax DOT com...
> I had a line of code like this:
>
> char buffer[100];
> printf("%d %s\n", (char*)memset(buffer, '-', 64));
>
> and what was discovered is:
>
> IF buffer already contains a string, "ABCD",
> THEN memset() as above printed:
>
> "-------------------------------------------------------------BCD"
> (truncated to fit here)
>
> OBSERVE: that the everything was MOVED DOWN
> THEN: the dashed-line added.
>
> QUESTION: Why would this be ?
You're in big trouble here!
1. Your printf comes with %d and %s, but you only provide one argument
(memset's return value)! Perhaps the %d was a typo.
2. You can't rely on being able to use blocks of memory as strings! The
results will depend on the next 0 byte encountered, and unless you're
explicitly going to have one, your string could be any length at all -- and
your program will misbehave.
Thanks to the above, there is no defined behaviour for this program.
> PROBLEM #2:
> There's no such thing as a memcat() routine...
> why? How can binary data (data containing 0x00) be
> CONCATED to accurately and properly return the
> size (pointer growth) of a buffer?
>
> (growing_pointer *)memcat(destination, source, n_bytes);
>
> such that "growing pointer" is simply something that provides
> the buffer address of 'source', and its length.
>
> Any ideas here?
Since a pointer doesn't know anything about the size of the data it's
pointing to, memcat would probably need to use a terminating 0 byte -- just
like strcat does. Try strcat. Better still -- figure out exactly what it
is you want to do with your data!
Edmund.
- Raw text -