简体   繁体   English

整数数组的动态内存分配

[英]Dynamic memory allocation of integer array

I am trying to create an array of size 2, dynamically, using malloc. 我正在尝试使用malloc动态创建一个大小为2的数组。 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 . 当运行该代码,它打印4, 5, 8, 9, 7

I am wondering how it was able to allocate more memory (5 integers) than I requested (2 integers)? 我想知道它是如何能够分配比我要求的更多的内存(5个整数)(2个整数)?

i wondering how it was able to allocate more memory than I requested. 我想知道它是如何能够分配比我要求更多的内存。

It didn't. 它没有。 You are invoking undefined behaviour . 您正在调用未定义的行为 One possible outcome * is that your program appears to "work". 一个可能的结果*是您的程序似乎“工作”。


* Arguably, the worst. *可以说是最糟糕的。

Like Oli said, it's undefined . 就像奥利说的那样,它是未定义的 It may work, it may not. 它可能有效,但可能没有。 But the reality is that even though it might work 99% of the time, it will, at least once, fail, and you'll get a SEGFAULT from reading or writing memory that your process isn't supposed to be using, and you'll also end up with virtually un-debugable memory leaks. 但实际情况是,即使它可能在99%的时间内都能正常工作,但它至少会失败一次,并且你会得到一个SEGFAULT来读取或写入你的进程不应该使用的内存,而你最终还会出现几乎无法调试的内存泄漏。

In addition to what's been suggested if I could add a few extra notes. 如果我可以添加一些额外的笔记,除了建议。

Not sure what OS you are running this on but if you are on Linux any time you're unsure if something should work or not run it through valgrind . 不知道你在运行什么操作系统,但是如果你在Linux上,你不确定某些东西是否可行,或者不通过valgrind运行它。 In my case it compiled a nice report about all the errors with your code (including not freeing the malloc -ed memory). 在我的例子中,它汇编了一个关于代码中所有错误的好报告(包括不释放malloc -ed内存)。

I got three Invalid write of size 4 for the three extra writes you do to the memory. 对于你对内存执行的三次额外写入,我得到三次Invalid write of size 4 It also notified me of the invalid memory that you are reading in the loop with Invalid read of size 4 and finally it gave me some stats on the leaks in your code: 它还告诉我你在循环中读取的无效内存, Invalid read of size 4 ,最后它给了我一些关于代码泄漏的统计信息:

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, don't cast the result of malloc. 最后, 不要转换malloc的结果。

C doesn't do bounds checking. C不进行边界检查。 You are stomping on memory that your program doesn't own. 你正在踩着你的程序不拥有的内存。

When you are invoking undefined behavior, you never know what might happen. 当您调用未定义的行为时,您永远不知道会发生什么。 For example when I ran the program I got the following as my output: 例如,当我运行程序时,我得到以下作为我的输出:

4, 5, 8, 51, 1629501832 4,5,8,51,1629501832

The reason this appears to work is because you are incrementing your pointer to point to new spots in memory (which your program may or may not be allocated to use). 这似乎起作用的原因是因为您正在递增指针指向内存中的新位置(您的程序可能会或可能不会被分配使用)。 I am guessing you are declaring this on the stack and that is why your undefined behavior appears to be "ok". 我猜你在堆栈上声明这个,这就是为什么你的未定义行为似乎是“ok”。

I do not think you understand the power of a pointer and the syntax you used. 我不认为您理解指针的功能和您使用的语法。 Remark 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, alternate implementation of your code to emphasize how you are pointing to new memory addresses by indexing elements outside of 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 a quick note on your code. 只需快速了解您的代码。 A malloc should typically be followed by a free -- so use it appropriately so there are no memory leaks. malloc通常后面应该是free - 所以要适当地使用它,这样就不会有内存泄漏。

I hope this helped! 我希望这有帮助! Feel free to correct me if I made a mistake. 如果我犯了错误,请随意纠正我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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