最佳做法,以避免与指针的问题 [英] Best practices to avoid problems with pointers

查看:130
本文介绍了最佳做法,以避免与指针的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是属于指针程序员错误的现实结果是什么?

What are the realistic outcomes of programmer bugs pertaining to pointers?

有什么不好的影响发生时,程序员创建的指针错误?

What 'bad effects' happen when programmers create pointer bugs?

与code实际的例子是preferable。

Practical examples with code are preferable.

推荐答案

东西可以去错了,当指针被误用:

Things that can go wrong when pointers are misused:


  1. 内存泄漏 - 您的方法分配一个指针,然后让它走出去的范围没有正确解除分配它。指针堆内存现在失去了,但记忆保持分配。这释放内存现在是非常困难的。 更多来自维基百科的信息。

  1. Memory leaks - You allocate a pointer in a method and then let it go out of scope without properly deallocating it. The pointer to the memory on the heap is now lost, but the memory remains allocated. Freeing this memory is now extremely difficult. More info from Wikipedia.

访问违规 - 您可以创建一个指向你没有访问内存地址的指针,或不存在。指针只是的整数毕竟,并可以像任何其他数量来操作。当您尝试取消引用您的无效的指针,你的程序将停止。 更多来自维基百科的信息。

Access violations - You create a pointer that points at a memory address that you do not have access to, or that does not exist. Pointers are just integers afterall, and can be manipulated like any other number. When you attempt to dereference your invalid pointer, your program will halt. More info from Wikipedia.

空指针的错误的 - 这是访问冲突的一种特殊情况。以正确的方法公园的指针,因此它不会在什么特别点,就是它的值设置为零或空。试图取消引用空指针将暂停你的程序。 更多来自维基百科的信息。

Null pointer errors - This is a special case of an access violation. The proper way to "park" a pointer, so that it doesn't point at anything in particular, is to set its value to zero or null. Attempting to dereference a null pointer will halt your program. More info from Wikipedia.

缓冲区溢出 - 您分配一个指针的30个字符的字符缓冲区。这样就可以进行流式传输用户输入(从插座,文件,控制台等等)到这个缓冲区。如果你没有正确地实现缓冲区边界检查,那么你的程序可能把超过30个字符到缓冲区。这将相邻存储到内存缓冲区损坏任何数据,并可能会使您恶意code攻击。 更多来自维基百科的信息。

Buffer overflows - You allocate a pointer for a character buffer of 30 characters. You then proceed to stream user input (from a socket, file, console, etc.) into this buffer. If you fail to properly implement buffer bounding checks, then your program could potentially put more than 30 characters into the buffer. This will corrupt any data stored adjacent to the buffer in memory and possibly expose you to a malicious code attack. More info from Wikipedia.

内存损坏 - 指针只是一个包含一些的内存地址指向一个整数。作为一个整数,指针运算可用于各种有趣的操纵指针的值方法。如果指针计算出错微妙的错误才能发展。指针现在将指向内存中一些不为人知的位置,什么事情都可能发生,当它被取消引用。

Memory corruption - A pointer is just an integer that contains the memory address of something it points to. As an integer, pointer arithmetic can be used to manipulate the pointer's value in all sorts of interesting ways. Subtle bugs can develop if the pointer calculations go wrong. The pointer will now point to some unknown location in memory, and anything could happen when it is dereferenced.

空值终止字符串问题 - 当字符串库函数期望空值终止字符串是不是NULL终止美联储字符指针会发生这些错误。字符串库函数将继续处理的字符,一次一个,直到空发现 - 可以是任何地方。 开玩笑最能说明这个bug。

Null-terminated string problems - These bugs occur when string library functions that expect null-terminated strings are fed character pointers that are not null terminated. The string library functions will continue to process characters, one at a time, until a null is found -- wherever that may be. A joke best illustrates this bug.

这篇关于最佳做法,以避免与指针的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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