0

I created a memory block with 4 units using calloc function where each unit is 1 byte.

Then I assigned it to a char pointer and filled the units with char values.

Then using realloc function I enlarged each unit to 4 bytes (size of an integer) Then I assigned it to a int pointer and filled integers in 3 of the units.

Theoretically I expected that the for the 4th unit of the memory block, value I gave before realloc will be preserved. But it is not. When I compile, each time I get a random value for that.

 int main() {


    //create a memory block of 4 units (each 1 bytes : char size)
    char *p = (char*)calloc(4,sizeof(char));

    //fill the memory units with chars
    p[0] = 'A';
    p[1] = 'B';
    p[2] = 'C';
    p[3] = 'D';

    //print 
    for(int i = 0; i< 4 ; i++){
        printf("%c \n",p[i]);
    }

    //print integer value of third index
    printf("Third index integer value : %d\n",p[3]);

    printf("\n"); //space



    //change the memory block so that each unit = 4 byte (size of integers)
    int  *B = (int*)realloc(p, sizeof(int));
    p = NULL;


    //fill 3 of the 4 memory units with integers
    B[0] = 20;
    B[1] = 25;
    B[2] = 30;


    //print integers
    printf("%d \n", B[0]);
    printf("%d \n", B[1]);
    printf("%d \n", B[2]);
    printf("Third index integer value : %d \n", B[3]);

    printf("Third index char value : %c \n", B[3]);

    return 0;
}

Outcome

attempt 1

B 
C 
D 
Third index integer value : 68

20 
25 
30 
Third index integer value : 53491 
Third index char value : ó 

attempt 2

A 
B 
C 
D 
Third index integer value : 68

20 
25 
30 
Third index integer value : 42319 
Third index char value : O 
3
  • 3
    B[anything_but_zero] is out-of-range access. "Then using realloc function I enlarged each unit to 4 bytes (size of an integer) " is not true. Commented Jun 6, 2017 at 7:47
  • Side note: size(char) is a pointless expression, its value is 1 by definition (sizeof returns the size in number of char objects). Commented Jun 6, 2017 at 8:06
  • 1
    Please, remember that in C you always have to free the allocated memory, while in C++ it's way better to use containers like std::vector. Commented Jun 6, 2017 at 8:19

1 Answer 1

2

Read suitable documentation of realloc: its size parameter is the new size of memory to allocate. You're asking it to allocate sizeof(int) bytes, which is likely 4 (the same as the size of your previous allocation).

From the C-style allocation functions, calloc is the only one which works in terms of cells & cell size. Everything else (malloc, realloc, even the C++ allocation functions ::operator new and ::operator new[]) works in raw memory size only. So change you call to:

int  *B = (int*)realloc(p, 4 * sizeof(int));
Sign up to request clarification or add additional context in comments.

1 Comment

also FYI: The reason you're seeing garbage on your last 2 printfs is that when you're allocating heap (malloc/realloc/calloc), the OS typically gives you memory in page sizes (4k). And those pages are dirty. Only calloc guarantees to zero out the data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.