0

The question about realloc().

If I want to size up the memory I allocated before with realloc().

Will the additional memory be initialized to 0 just like calloc() or not?

The second question is:

    int * p =(int*)malloc(10*sizeof(int));
    int* s = (int*)realloc(p,20);
    p=s;

Is assigning s to p a good way to resize the pointer p?

And can we realloc() the memory allocated with calloc()?

alk
  • 69,737
  • 10
  • 105
  • 255
Liu
  • 413
  • 4
  • 11
  • 1
    What did the manual say? – wildplasser Jan 26 '19 at 14:57
  • 2
    Except [the casting](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/) and lack of error checking, that looks almost like it should look. And no, the extra memory won't be initialized. You might want to check e.g. [this `realloc` reference](https://en.cppreference.com/w/c/memory/realloc) – Some programmer dude Jan 26 '19 at 14:58
  • 1
    Oh, and your reallocation actually allocate *shrink* the allocated memory. The size you pass to `realloc` is the ***new*** size (in bytes) to allocate. – Some programmer dude Jan 26 '19 at 15:02

1 Answers1

3

Will the additional memory be initialized to 0?

No.

can we realloc() the memory allocated with calloc()?

Yes.

Is assigning s to p a good way to resize the pointer p

Depends.

Just doing

int * p = malloc(...);
int * s = realloc(p, ...);
p = s;

is the same as

int * p = malloc(...);
p = realloc(p, ...);
int * s = p;

In both cases, if realloc() fails (and with this returned NULL) the address of the original memory is lost.

But doing

int * p = malloc(...);

{
  int * s = realloc(p, ...); /* Could use a void* here as well. */
  if (NULL == s)
  {
     /* handle error */
  }
  else
  {
    p = s;
  }
}

is robust against failures of realloc(). Even in case of failure the original memory is still accessible via p.

Please note that if realloc() succeeded the value of the pointer passed in not necessarily addresses any valid memory any more. Do not read it, nor read the pointer value itself, as doing this in both cases could invoke undefined behaviour.

alk
  • 69,737
  • 10
  • 105
  • 255
  • @xing: Why? We have no context. So code as it stands is correct. – alk Jan 26 '19 at 15:07
  • because in the context, I want to resize p from 10*sizeof(int) to 20*sizeof(int). – Liu Jan 26 '19 at 15:12
  • @Liu: Your question does not mention this. – alk Jan 26 '19 at 15:13
  • 2
    anyway it doesn't matter. – Liu Jan 26 '19 at 15:17
  • I don't understand the first and the last brackets. And if realloc() failed, may I read the pointer I passed to it??? – Liu Jan 26 '19 at 16:16
  • @Liu: Curly braces can be used to create a scope. This can be done to limit the validity of temporarily used variables for example `s` here. Please see my updated answer. – alk Jan 26 '19 at 16:19
  • You mean if int * s = realloc(p, ...); succeeded, don't read what p points to or p itself?? But after we state p=s; after it succeeded, can we read what p points to or p itself?? – Liu Jan 28 '19 at 15:20
  • @Liu: 1.) don't read neither p nor where it points to 2.) yes, you can read both – alk Jan 28 '19 at 15:28
  • The reason why we can't read p or what p points to in the first case is because pointer p maybe relocated in memory if the following space wasn't large enough to accommodate the memory we add to p?? – Liu Jan 28 '19 at 15:37
  • @liu: Probably, there might be other reasons as well. – alk Jan 28 '19 at 15:48
  • @Liu: BTW, mind your wording: Nor is p reallocated nor is memory added to a pointer. Those things are done to memory the pointer points to and not to the pointer itself. – alk Jan 28 '19 at 16:25