Jump to content

what exactly is the second argument of realloc()???

- - - - -

  • Please log in to reply
2 replies to this topic

#1
shakisparki

shakisparki

    Newbie

  • Members
  • Pip
  • 1 posts
void * realloc(void*ptr , size_t size)

is the second argument supposed to be the number of bytes to be allocated, e.g
5 * sizeof(int)

or just the number of elements needed e.g
5 or 10

#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Second argument is new size in bytes. If you want to add, lets say 4 more bytes, you'd call it something like this:

array = realloc(array, sizeOfArray + 4);


A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Yes, it tries to be a bit more flexible and allows you to enter your own terms of bytes, i.e. to double the size of an arbitrary array type
realloc( array, ( sizeOfArray * 2 ) * sizeof( array[0] ) );

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users