-1

I allocate dynamic array in main like this:

char *arr = malloc(sizeof(char));

then in a random function I reallocate that array to n elements like:

arr = realloc(arr, n * sizeof(char));

then I do a random stuff with the array, and in another function I want to allocate one more array with n elements like this:

char *arr2 = malloc(n * sizeof(char));

but this malloc returns the same adress as arr. I tried everything, but still returns the same adress, so arr2 is pointing to arr. What am I doing wrong? If i allocate new array again lets say arr3 with the same method, now it works and it gives me new adress.

Edit:

void reallocate(char *arr, int newLength) {
    arr = realloc(arr, newLength * sizeof(char));
}

void fc1 (char *arr, int *length) {
    char *temp = malloc(*length * sizeof(char));
    strcpy(temp, arr);

    int d;
    scanf("%d", &d);

    char *arr2 = malloc(d * sizeof(char)); //there it gives me the same adress 
    scanf("%s", arr2);
}

int main(void) {
    char arr = malloc(sizeof(char));
    int *length = malloc(sizeof(int));
    *length = 10;

    reallocate(arr, 10);
    fc1(arr, length);

    return 0;
}
rastiq
  • 343
  • 1
  • 6
  • 8
    Show us all of your code, not just the isolated parts. What you're describing doesn't seem possible, which means you've left out some critical details. – Robert Harvey Mar 23 '19 at 21:34
  • 1
    please post a [mcve] so we can duplicate the problem and thereby help you debug it – user3629249 Mar 23 '19 at 21:39
  • 1
    it can be possible when `n` is zero. – ensc Mar 23 '19 at 21:45
  • does strcpy free the source array? – rastiq Mar 23 '19 at 22:02
  • You have `char *arr` as an argument to `reallocate()` and when it does the `realloc()` you do not get the pointer to the new memory area provided by `realloc()`. You need to either return the new pointer by making `reallocate()` a `void *` return rather than `void` and then return `arr` or you need to make `arr` a pointer to a pointer (`char ** arr`) and do the dereferencing properly as in `*arr = realloc(*arr, newLength * sizeof(char));`. – Richard Chambers Mar 23 '19 at 22:08

1 Answers1

1

We'd need to see the code to be sure, but here's two ways it can happen:

 void func2(char *s)
 {
      do_something(s);
      free(s); // we are done with it
 }

 void func1(void)
 {
      char * array = some_func();
      func2(array); // Oops, func2 frees it
      char * array2= malloc (...); // could get the same pointer
 }

Here, func1 passes the pointer to func2 which frees it. It is an error for func1 to do anything with array after that point as it can be re-assigned.

And the second way:

 void get_data(char *s)
 {
      char *data = // code to get some data from somewhere
      s = realloc (s, strlen(data) + 1);
      strcpy(s, data);
 }

 void func1(void)
 {
      char *ptr = malloc(12);
      get_data(ptr);
      // oops, ptr has the old value
      char *ptr2 = malloc(12); // could get the same pointer
 }

Here, get_data calls realloc but the caller still has the old pointer which realloc might free.

But we'd need to see the code to be sure.

Update:

I guessed right. This is the second way in my example above:

void reallocate(char *arr, int newLength) {
    arr = realloc(arr, newLength * sizeof(char));
}

This looks exactly like my get_data function above. This function does not pass the new value of arr back to the caller, so the caller still has the old value which may be freed.

reallocate(arr, 10);
fc1(arr, length);

This looks exactly like my second func1 above. You pass arr to reallocate which may invalidate it, but then you pass the old value of arr to fc1. But reallocate may have freed it. There's a reason realloc returns the new value, but your reallocate function doesn't.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278