English 中文(简体)
C. 与自由()有关的权利
原标题:Query related to free() in C
  • 时间:2011-11-21 16:04:26
  •  标签:
  • c
  • free

如果你试图解放一个未使用<代码>的小型/移动<>/代码>的记忆,会发生什么情况?

Here is what I mean :

void main()
{
int temp = 0;
int *ptr = &temp;
free(ptr);
}

我想free()将退回一些错误代码,但free(没有收益价值。

最佳回答

如果您在先前分配的点名上打上<代码>free(),就会引发不明确的行为。

http://linux.die.net/man/3/malloc”rel=“nofollow” 。

免费()功能释放了 p标明的记忆空间,必须用以前向小鼠、电 call或真菌发出的呼吁归还。 否则,或者如果事先已经要求自由(接收人),就会出现不明确的行为。 如果粉碎是NUL,则没有进行操作。

问题回答

To add to Malcolm s answer: This is undefined behavior by ISO/IEC 9899:1999, 7.20.3.2:

Otherwise, if the argument does not match a pointer earlier returned by the calloc, malloc, or realloc function [...] the behavior is undefined.

See the draft standard here:

我将上述法典扩大如下范围:

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

void main()
{
  int temp = 0;
  int *ptr = &temp;
  printf("Before: %0X
", ptr);
  free(ptr);
  printf("After: %0X
", ptr);
  getchar();
}

如果该守则由2010年视觉演播室汇编成,则由Debug配置,打电话free 发出“Debug Assertion败坏”信息。 这一错误信息来自四舍五入。

/*
 * If this ASSERT fails, a bad pointer has been passed in. It may be
 * totally bogus, or it may have been allocated from another heap.
 * The pointer MUST come from the  local  heap.
 */
_ASSERTE(_CrtIsValidHeapPointer(pUserData));

与MinGW-GCC相匹配,由此而产生的外差操作毫无错误(“After:......”线显示与“Before:......”线相同。

所有的伤员都将停工。

这就意味着:

  • If you are lucky, your program will error out and terminate.
  • If you are not lucky, some attacker will execute arbitrary code using your program (free() will usually try to insert your newly freed "chunk" of memory into some data structure, which usually involves some writes at locations determined by values at/near the pointer you passed).
  • Anything between these two extremes. Not terminating in error should be considered worse than terminating in error.

除了Malcom和Lander_gongor的答复外,C网站视窗和Series网站也一样。 http://msdn.microsoft.com/en-us/library/we1whae7%28v=VS.71%29.aspx rel=“nofollow”>。

免费功能处理以前通过电话、小型或实时分配的记忆器(24小时)。 释放的 by数目相当于分配 block块时(或重新定位)要求批量的数量。 如果蒙博拉是民族解放军,那点人就会被忽视,立即自由返回。 试图释放一个无效的点(一个没有由电离层、小型或实际分配的记忆组的点)可能会影响随后的分配要求并造成错误。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签