-1

I built a code in the following manner:

void other_func(int *passed_ptr, int size)
{
    passed_ptr = (int *) realloc(passed_ptr, 15*sizeof(int));   // reallocing to new size
    
    passed_ptr[12] = 34;      //some number as input, this is where program ends abruptly with 'segmentation fault' error

    ....
    ....
}

void parent_func()
{
    int *ptr = malloc(10*sizeof(int));
    other_func(ptr, 10);
    ....
    ....
    ....
}

Why I am not able to realloc the pointer (ptr) outside of its parent function?

  • C is pass-by-value. changes to `passed_ptr` are not reflected outside of the function. You would need to pass `int**` instead if you want to change the value of a pointer and reflect it outside. – Raildex Nov 16 '21 at 06:52
  • 1
    Because C passes function arguments by value, not by reference. So an assignment to `passed_ptr` in `other_func` just changes the local copy. It has no effect on `ptr` in `parent_func`. You can either return the new pointer from `other_func`, or pass a pointer to `ptr` to `other_func`. – user3386109 Nov 16 '21 at 06:52
  • Research *emulating pass by reference in C*. – Some programmer dude Nov 16 '21 at 06:53
  • 1
    On another couple of notes, there has been some discussion about [casting the result of `malloc` (and siblings like `realloc`)](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) and the consensus is to *not* do it. Also, remember that `realloc` can fail, and will then return a null pointer while not freeing the already allocated memory. Therefore you should never assign to the same pointer variable you pass to `realloc`. – Some programmer dude Nov 16 '21 at 06:56
  • Once fixed up and made to compile, the code you show should work as expected (unless `realloc` fails). It's not a [mre]. Please take some time to ead [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Then [edit] your question to improve it. – Some programmer dude Nov 16 '21 at 07:04
  • all these comments and answer are about `ptr` in `parent_func`. OP indicates the problem occurs in `other_func` with `passed_ptr`. Perhaps it's too late for me to think about these things, but other than not checking the return value of `realloc`, I don't see anything wrong and [can't reproduce the problem](https://godbolt.org/z/Eqc3q1f8r) – yano Nov 16 '21 at 07:08
  • Does this answer your question? [Changing address contained by pointer using function](https://stackoverflow.com/questions/13431108/changing-address-contained-by-pointer-using-function) – Gerhardh Nov 16 '21 at 08:20

2 Answers2

0

C is pass by value

Which means arguments to functions are copied.
If you want to change the value of an int inside a function, you need to pass a int*.
If you want to change the value of aint* inside a function, you need to pass a int**.

See this: Passing by reference in C

Raildex
  • 2,536
  • 1
  • 16
  • 32
0

The changes you make to passed_ptr inside the function will not be visible outside the function. You need to pass a pointer to the pointer to be able to make changes that are visible to the caller.

Example:

#include <stdbool.h>
#include <stddef.h>

// Both the pointer and the size arguments are here pointers to the variables
// used to call the function:
bool grow_int_arr(int **passed_ptr, size_t *size)
{
    size_t new_size = *size * 15 / 10; // calculate new size

    // don't assign directly to `*passed_ptr` - realloc may fail:
    int *new_ptr = realloc(*passed_ptr, new_size * sizeof *new_ptr);

    if(new_ptr) {               // success, now assign:
        *passed_ptr = new_ptr;
        *size = new_size;
        return true;
    }

    return false;               // realloc failed
}

You could now call the function like so:

void parent_func()
{
    size_t size = 10;
    int *ptr = malloc(size * sizeof *ptr);

    if(ptr) {
        if(grow_int_arr(&ptr, &size)) {   // pass in pointers to the variables
            printf("new size: %zu\n", size);
        } else {
            puts("realloc failed but ptr + size are still valid");
        }
        free(ptr);
    } else {
        puts("malloc failed");
    }
}
Ted Lyngmo
  • 60,763
  • 5
  • 37
  • 77