-1

So, while writeing a program I realised that when using realloc in a function outside of main if the original block of memory was declaired in main it doesnt seem to keep the changes outside of the function. E.G.

void main()
{

    int *ptr;

    //allocates memory
    ptr = calloc(4, sizeof(int));

    exampleFunction(&ptr);

} //end main


//this function reallocates the memory block of ptr
void exampleFunction(int *ptr)
{

    ptr = realloc(ptr, (sizeof(int) * 10));

} // end exampleFunction

Do I need to do something different or should this work fine? Also this is just example code and is not intended to be runnable

Extra info I am using MinGW on windows 10

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    Does this answer your question? [C Programming: malloc() inside another function](https://stackoverflow.com/questions/2838038/c-programming-malloc-inside-another-function) – 001 May 01 '20 at 16:36
  • 1
    Try to keep your programs with 0 (zero) warnings. Now you have warnings and don't read them but prefer to ask for help. – i486 May 01 '20 at 17:02
  • So again as I stated in the original post that was example code and not my actual code I wanted to make sure that was a viable thing that I could do – Paul Geoghegan May 01 '20 at 21:31

2 Answers2

2

You passes to the function the expression &ptr that has the type int **.

exampleFunction(&ptr);

But the function parameter has the type int *.

void exampleFunction(int *ptr)

So the function declaration and its call do not make sense.

You have to declare and define the function at least like

//this function reallocates the memory block of ptr
void exampleFunction( int **ptr)
{

    *ptr = realloc( *ptr, (sizeof(int) * 10));

}

Though it will be better to use a temporary pointer with the call of realloc because the function can return NULL. In this case the original value of *ptr will be lost.

So you should declare the function like

//this function reallocates the memory block of ptr
int exampleFunction( int **ptr)
{
    int *tmp = realloc( *ptr, (sizeof(int) * 10));

    int success = tmp != NULL;

    if ( success ) *ptr = tmp;

    return success;

}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Okay so you need dto dereference and then pass back the new adress? I had sugested this to someone befor but they said I couldnt do that. – Paul Geoghegan May 01 '20 at 21:34
  • @PaulGeoghegan If you want to change an object in a function then you need to pass it by reference that in C means passing a pointer to the object. If you want to change your pointer ptr in a function you have to pass a pointer to this pointer ptr as for example exampleFunction( &ptr ); – Vlad from Moscow May 01 '20 at 21:47
0

You can write like this.

void main()
{

    int *ptr;

    //allocates memory
    ptr = calloc(4, sizeof(int));

   ptr= exampleFunction(ptr);

}

int * exampleFunction(int *ptr)
{
    ptr = realloc(ptr, (sizeof(int) * 10));
  return(ptr);
}
  • You're correct: this is a viable, alternative solution ... But it misses the point that the OP tried passing `&ptr` (OK) ... but then failed to a) declare the parameter as `int **ptr` and b) assign to `*ptr = realloc(...)` (hence the error). – FoggyDay May 01 '20 at 17:29
  • Yes sir i knew this.. I just tried to give a easier one solution and thanks for correcting me. @FoggyDay – Amit Pal Singh May 01 '20 at 17:36
  • I like this dolution better if I am honest it is sympler and altho the error checking from the other one is nice to have I already know how to do that and it wasnt really what I asked for. – Paul Geoghegan May 01 '20 at 21:36