Dynamic memory allocation of an integer array

I am trying to create an array of size 2 dynamically using malloc. Here is my code:

int *d = (int*)malloc(2 * sizeof(int)); d[0] = 4; d[1] = 5; d[2] = 8; d[3] = 9; d[4] = 7; int i; for (i = 0; i < 5; i++) printf("%d \n", d[i]); 

When I run this code, it prints 4, 5, 8, 9, 7 .

I am wondering how it was possible to allocate more memory (5 integers) than I requested (2 integers)?

+6
source share
6 answers

I wonder how he could allocate more memory than I requested.

This is not true. You cause undefined behavior . One possible outcome * is that your program seems to be "running."


* Perhaps the worst.
+9
source

As Olya said, he is undefined . It might work, maybe not. But the reality is that even if it can work 99% of the time, it will fail at least once, and you will get SEGFAULT from reading or writing memory that your process should not use, and you will also get practically unsolvable memory leaks.

+3
source

In addition to what was suggested, if I could add a few extra notes.

You do not know what OS you are running on, but if you are on Linux at any time when you are not sure that something should work or not run it through valgrind . In my case, he made a good report of all errors with your code (including without freeing up malloc -ed memory).

I got three Invalid write of size 4 for three additional records that you make in memory. He also notified me of the invalid memory you read in a loop with Invalid read of size 4 , and finally he gave me some statistics on leaks in your code:

 HEAP SUMMARY: in use at exit: 8 bytes in 1 blocks total heap usage: 1 allocs, 0 frees, 8 bytes allocated LEAK SUMMARY: definitely lost: 8 bytes in 1 blocks 

Finally, do not return the result malloc.

+2
source

C does not perform bounds checking. You lose memory that your program does not belong.

+1
source

When you invoke undefined behavior, you never know what might happen. For example, when I ran the program, I received the following as output:

4, 5, 8, 51, 1629501832

+1
source

The reason this works is because you are increasing your pointer by pointing to new spots in memory (which your program may or may not be allocated for use). I assume that you are declaring this on the stack, and so the undefined behavior looks “normal”.

I don’t think you understand the power of the pointer and the syntax you used. Note that the following is equivalent:

 int arr[ 2 ] = { 1, 2 }; int *pi = &arr; // The following output is equivalent to... for ( int i = 0; i < 2; i++ ) { printf( "arr[i] = %d.\n", arr[ i ] ); } // this. for ( int i = 0; i < 2; i++ ) { printf( "*(p + i) = %d.\n", *( p + i ) ); } 

Consider this alternative implementation of your code to emphasize how you point to new memory addresses by indexing elements outside your array.

 int *d = ( int * )malloc( 2 * sizeof( int ) ); *( d + 0 ) = 4; // Observe you are accessing the memory location d points to. *( d + 1 ) = 5; // Observe you are accessing the memory location d + 4 bytes (or 8 if 64-bit) points to... *( d + 2 ) = 8; // ... *( d + 3 ) = 9; // ... *( d + 4 ) = 7; // Observe you are assigning a value to the memory location of d + 24 bytes (or 48 bytes if 64-bit). for ( int i = 0; i < 5; i++) { printf( "%d \n", *( d + i ) ); } 

Just write down your code quickly. Usually malloc follows free - so use it appropriately so that there are no memory leaks.

Hope this helps! Feel free to correct me if I made a mistake.

+1
source

Source: https://habr.com/ru/post/948988/


All Articles