简体   繁体   中英

C: using realloc() in seperate function doesn't change array size

I feel as though this shouldn't be such a hard problem and I've tried everything that I've found on similar questions to no avail, so I thought I'd give it a try to ask myself. My code (simplified) is as follows:

#include <stdio.h>
#include <stdlib.h>

void setArraySize(int** arr) {
    int arrSize;
    printf("\nEnter array size: ");
    scanf("%d", &arrSize);
    int* tmp = realloc(*arr, arrSize);
    if (tmp == NULL) {
        printf("Error");
        return;
    }
    *arr = tmp;
}

void auswahl() {
    int* arr = NULL;
    setArraySize(&arr);
    free(arr);
}

int main() {
    auswahl();
    return 0;
}

When debugging I noticed that my array only contains the integer -842150451. I am using Microsoft Visual Studios 2019 Community. Additionally, even when trying the most basic and safest functions, I attain the same result. Could the problem then have to do with Visual Studios?

#include <stdio.h>
#include <stdlib.h>

void increase(int** data)
{
    int* tmp = realloc(*data, 20);
    if (tmp == NULL) {
        printf("Error");
        return;
    }
    *data = tmp;
}

int main() {
    int* arr = NULL;
    increase(&arr);
    printf("%d", sizeof(arr));
    return 0;
}

Thanks in advance for any help!

I changed your second code a little bit (added printf's), it will help you understand what is happening

#include <stdio.h>
#include <stdlib.h>

void increase(int** data)
{
    printf ("in increase|*data|%p|\n", *data);
    int* tmp = realloc(*data, 20);
    printf ("in increase|*data after realloc|%p|\n", *data);
    printf ("in increase|tmp after realloc|%p|\n", tmp);
    if (tmp == NULL) {
        printf("Error");
        return;
    }
    *data = tmp;
    printf ("in increase|*data after assign|%p|\n", *data);
}

int main() {
    int* arr = NULL;
    printf ("in main|arr|%p|\n", arr);
    printf ("in main|arr size|%d|\n", sizeof(arr));
    increase(&arr);
    printf ("in main|arr after increase|%p|\n", arr);
    printf ("in main|arr size after increase|%d|\n", sizeof(arr));
    return 0;
}

this will output this :

in main|arr|(nil)|                                                                                                            
in main|arr size|8|                                                                                                           
in increase|*data|(nil)|                                                                                                      
in increase|*data after realloc|(nil)|                                                                                        
in increase|tmp after realloc|0x2441010|                                                                                      
in increase|*data after assign|0x2441010|                                                                                     
in main|arr after increase|0x2441010|                                                                                         
in main|arr size after increase|8|  

So basically the size of arr doesn't change because it's not an array, it's a pointer to an int, the size here is 8 because it's a 64bits machine, and memory adresses for 64bits need 8 Bytes to be stored, and this is what sizeof() returns the size in bytes of the type you gave it ( int* arr a pointer to an int )

for the other values you can see that you have effectively allocated memory in the heap at address 0x2441010 so your QUOTE"array"QUOTE (it's not an array), starts at this address and have enough space for 20(BYTES) if you wanted 20 integers you should have used realloc(..., 20* sizeof(int)) because these alloc functions use bytes as their unit.

hope this helps you somehow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM