-3

How to reallocate content on bigger storage but that the rest of content be zero ? I have at the moment like

void* oldContent;
size_t oldContentSize;
size_t newBufferSize;

realloc(oldContent, newBufferSize);

How to achieve that all with inde from oldContentSize to the end of new buffer have value '\0' ?

PaolaJ.
  • 10,872
  • 22
  • 73
  • 111

2 Answers2

1
void*newContent;
newContent = realloc(oldContent,newBufferSize);
memset(newContent + oldContentSize, 0, newBufferSize - oldContentSize);
geocar
  • 9,085
  • 1
  • 29
  • 37
1

Just have to do it yourself:

oldContent = realloc(oldContent, newBufferSize);
memset((char *)oldContent + oldBufferSize, 0, newBufferSize-oldBufferSize);
Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
  • Don't forget to check that `oldContent != NULL` before calling `memset`. And if `realloc` fails, you've just lost your old pointer value (which may be ok if you're not going to need it again). – Keith Thompson Sep 04 '13 at 22:40