English 中文(简体)
教科书中的点名例使我感到困惑。
原标题:Pointer Example in Textbook is confusing me
  • 时间:2011-11-05 03:53:01
  •  标签:
  • c++
  • pointers

目前,我正在上班,我们的教科书使我感到困惑。 他们首先指出,以下例子将钱款所指明的地方的价值复制到我的金钱所指明的地方:

*myMoney = *money;

然后,下一个例子将金钱价值复制成我的货币。

myMoney = money;

第二个例子造成记忆泄露,因为*my money指出的原有地点已不复存在。 之所以如此,是因为用来持有点人的记忆现在是一种实际的浮动价值,而不是一个记忆地址?

现在,把我混为一谈的那部分是在他们发表不同声明的下一个部分。 实例:

char alpha[20];
char *alphaPtr;
char *letterPtr;
vod Process(char []);
.
.
alphaPtr = alpha;
letterPtr = &alpha[0];
Process(alpha);

该书说:

myMoney = money;

will create a memory leak because it severs the link between the pointer and it s pointed to address, will

alphaPtr = alpha;

造成记忆联系? 他们已经宣布,

char *alphaPtr = *alpha;
最佳回答

记得:

  • myMoney and money are pointers, so myMoney = money; copies one pointer over another. Now you have two pointers pointing at the same thing (what money pointed to), and nothing pointing at what myMoney used to point at (this is a memory leak).
  • *myMoney and *money are the values pointed to by the pointers (because * dereferences a pointer to get what it s pointing to), so *myMoney = *money; copies what money points to over what myMoney points to; the pointers themselves did not change.
  • alpha is an array, which can be degraded into a pointer to the first element in the array.
  • There s no leak with alphaPtr because it wasn t pointing to anything in the first place.
  • Yes, char *alphaPtr = *alpha; Would have been a much better way to write it, because then alphaPtr doesn t spend any time uninitialized. Some compilers will even warn you when you declare uninitialized pointers like the textbook example.
  • Using (dereferencing) an uninitialized pointer is a bad thing as it results in dreaded undefined behavior. Your program may crash immediately, or it may crash much later, or it may simply corrupt your data without telling you anything, and you ll only find it months later, if ever.
问题回答

第二个例子造成记忆泄露,因为*my money指出的原有地点已不复存在。 之所以如此,是因为用来持有点人的记忆现在是一种实际的浮动价值,而不是一个记忆地址?

储存在我的货币中的记忆点现已消失。 如果分配这笔钱,将无法收回其地址,以清理其位置。

在你的第二个例子中,阵列具有特殊性,称呼它们的讲话。

指定联络点的问题

myMoney = money;

只是,如果<条码>米....../条码>是价值的唯一点,那么你就不再有任何途径获得。

my money。 <代码>my money正被指定为新的记忆地址,因此,如果再不提及它,它原本会漏掉。

alphaPtr = alpha is not a leak. alpha is the actual memory, alphaPtr just points to it. Even if alphaPtris assigned to point at something else, alpha exists on the stack and will be reclaimed automatically when it goes out of scope.

<代码>char *alphaPtr = *alpha 将不予汇编。 您试图将<代码>char指定至char*,该编码无效。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

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->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签