Re: Extra bytes



eb_csilva wrote:
[...]
And Eric,

"In case that ptr is NULL, the [realloc] function behaves exactly as
malloc, assigning a new block of size bytes and returning a pointer to
the beginning of it." (from cplusplus.com)
"The realloc function changes the size of an allocated memory block.
The memblock argument points to the beginning of the memory block. If
memblock is NULL, realloc behaves the same way as malloc and allocates
a new block of size bytes. If memblock is not NULL, it should be a
pointer returned by a previous call to calloc, malloc, or
realloc." (from microsoft.com).

Are you telling me that this behaviour can be platform-dependent or
vendor-dependent?

No, there was nothing wrong with your use of realloc (except that
you didn't compare the returned value to NULL to check for failure).
I was trying to tell you that what your filled the allocated memory
with wasn't a string, and that its non-stringness could explain the
presence of the extra characters. I also pointed you at some library
functions that could replace a good deal of your code.

I don't understand your comment "debug a paraphrase". This is my
production code, from which I stripped out some irrellevant (in my
opinion) details.

The fact that a pointer was initialized to `null' and later
compared to `NULL' led me to suspect otherwise. It is, of course,
perfectly legal to define your own `null' macro or to create a
pointer variable named `null' with the value `NULL', but it seems
a strange thing to do. But if you say so ...

Also, since the problem at hand is extra characters in the
printed output, stripping out the code that printed the output
may not have been the very best choice.

--
Eric.Sosman@xxxxxxx
.



Relevant Pages

  • Re: Extra bytes
    ... "In case that ptr is NULL, the [realloc] function behaves exactly as ... the size of an allocated memory block. ... If memblock is NULL, realloc behaves ... the same way as malloc and allocates a new block of size bytes. ...
    (comp.unix.programmer)
  • Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
    ... Bill Reid wrote in message ... If block is a NULL pointer, realloc works just like malloc. ...
    (comp.lang.c)
  • Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
    ... I believe I actually do call malloc() in some xxx.cpp files... ... What you quoted above is not *the* documentation for qsort. ... If block is a NULL pointer, realloc works just like malloc. ...
    (comp.lang.c)
  • Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
    ... I believe I actually do call malloc() in some xxx.cpp files... ... If block is a NULL pointer, realloc works just like malloc. ... I can use realloc in a loop and the first pass ...
    (comp.lang.c)
  • Re: Question about the *= (and similar) operator
    ... always return a pointer to fresh storage. ... calling realloc. ... Assume malloc works and then realloc fails. ... We'll have ptr set to NULL and no reference to the memory malloc gave us. ...
    (comp.lang.c)