可能导致内存损坏的情况 [英] Conditions that may lead to memory corruption

查看:72
本文介绍了可能导致内存损坏的情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚阅读了一篇文章,其中指出了以下可能导致内存损坏的情况.但是那里没有适当的解释.谁能解释这些情况如何导致内存损坏以及内部情况.

1.在分配和设置内存之前使用地址

I just read an article which stated the following conditions that may cause memory corruption. But no proper expalantion was given there. Can anyone explain that how does these conditions lead to memory corruption and what happens internally.

1. Using an address before memory is allocated and set

void x()
{
  struct *abc_ptr;
  p = abc_ptr->name;
}




2.使用已经释放的指针




2. Using a pointer which is already freed

int *p = new int;
//code
delete p;
//use of p here may lead to memory corruption



3.释放已经释放的内存.



3. Freeing memory which has already been freed.

char *a = malloc(128*sizeof(char));
free(a);
...   Do stuff
free(a);  // A check for NULL would indicate nothing.
          // This memory space may be reallocated and thus we may be freeing
          // memory we do not intend to free or portions of another block of 
          // memory. The size of the block of memory allocated is often held 
          // just before the memory block itself..
                  // some code
}
delete i;



4.释放未动态分配的内存



4. Freeing memory which was not dynamically allocated

void main()
{
struct ABC abc;
struct ABC *abc_ptr = &abc;
free(abc_ptr);
}



5.错误使用删除:删除必须与新使用相匹配



5. Incorrect use of delete: The delete must match the use of new

void x()
{
  abc *abc_ptr = new abc[100];
  delete [] abc_ptr;
}


http://www.yolinux.com/TUTORIALS/C++MemoryCorruptionAndMemoryLeaks.html [ ^ ]


http://www.yolinux.com/TUTORIALS/C++MemoryCorruptionAndMemoryLeaks.html[^]

推荐答案

1)在分配和设置内存之前使用地址
如果您分配了一个指针,并不意味着它指向了有用的地方.
如果您想给妈妈打电话,您是否会随意按一下按钮,然后与接听电话的人交谈?

2)使用已经释放的指针
如果删除内存块,则任何其他内存分配都可以自由使用该内存区域.仅仅因为指针值已更改,并不意味着您可以使用其指向的内存!
如果您卖掉了房子,您是否希望仍然被允许回到那里并在您的"卧室里睡觉?

3.释放已经释放的内存.
如果已经释放,则尝试再次释放它可能会导致问题-因为同一块内存将在空闲列表中两次.因此,两个不同的内存请求可以引用相同的物理内存.

4.释放未动态分配的内存

如果您释放没有在堆上分配的内存,那么它将引起问题-堆栈空间将由其他函数重用,并且任何获得释放块的内存分配都将同时引用它...

5.错误使用删除:删除必须与使用新的匹配
如果通过调用与分配的内存类型不同的类型来释放内存,则释放的内存很有可能与分配的块大小不同.结果,要么内存不再应该在堆中使用,要么释放的块可能包含不属于分配的内存,而是由另一个内存块拥有"的内存.
1) Using an address before memory is allocated and set
If you assign a pointer, that doesn''t mean it points somewhere useful.
If you wanted to call your mother, would you just press buttons at random and talk to whoever answered?

2) Using a pointer which is already freed
If you delete a memory block, then any other memory allocation is free to use that area of memeory. Just because the pointer value has nit changed, does not mean that you can use the memory it points to!
If you sold your house, would you expect to still be allowed to go back there and sleep in "your" bedroom?

3. Freeing memory which has already been freed.
If it is already freed, then trying to free it again could cause problems - because the same block of memory would be in teh free list twice. So two different requests for memory could refer to the same physical memory.

4. Freeing memory which was not dynamically allocated

If you free memory which isn''t allocated on the heap, then it is going to cause problems - the stack space will be reused by a different function, and any memory allocation which gets the freed block will refer to it at the same time...

5. Incorrect use of delete: The delete must match the use of new
If you free memory by calling it a different type to that which you allocated it, then it is quite possible that the memory freed is not the same size as the block that was allocated. As a result, either memory is no longer available to the heap when it should be, or the block freed could include memory which is not part of the allocation, but "owned" by a different memory block.


为避免错误使用未分配的指针,在声明时应将所有指针设置为NULL.当它们所指向的内存被释放时,应再次将它们设置为NULL.

语言规范不要求编译器执行此操作.

如果完成此操作,则使用未分配的指针将引发异常.

例如,#1应该是
To avoid erroneous use of unassigned pointers all pointers should be set to NULL when declared. They should be set to NULL again when the memory they point to is freed.

The language specification does not require the compiler to do this.

If this is done then using the unassigned pointer will throw an exception.

For example #1 should be
struct *abc_ptr = NULL;



#2应该是



#2 should be

int *p = new int;
//code
delete p;
p = NULL;




和#5应该是




and #5 should be

void x()
{
  abc *abc_ptr = new abc[100];
  delete [] abc_ptr;
  abc_ptr = NULL;
}



在所有其他示例中,指针应指向有效内存或设置为NULL.

如果指针未设置为NULL,则它可能具有任何值,并且对该指针的后续使用将产生不可预测的结果.

这是导致许多细微错误的原因.



In all the other examples the pointer should either point to valid memory or be set to NULL.

If the pointer is not set to NULL it could have any value and subsequent use of that pointer will have unpredictable results.

The is the cause of many subtle bugs.


这篇关于可能导致内存损坏的情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆