English 中文(简体)
哪一个点子在C++的另一端会造成物体丧失?
原标题:What may cause losing object at the other end of a pointer in c++?

EDIT:我发现这一错误:我没有初步确定一个大小的阵列。 问题可以结束。


I have a class V, and another class N. An object of N will have an array of pointers to objects of class V (say V **vList). So, N has a function like V **getList(); Now in some function of other classes or simply a driver function, if I say V **theList = (N)n.getList(); Q1: theList would be pointing at the 1st element of the array? Given that the size of array is known, can I loop through with index i and say V *oneV = *vList[i]? Please correct me if what I m doing above is wrong.


我一直在使用夸张来追踪我整个方案运行过程,我发现的是,在使用<编码>V *oneV = vList[i]之后,阵列中的点人数值(vList)与当初制定时的数值相同,但如果我跟随点人,则该标的已经去。 我猜测,这或许是我为什么会错过或大错。 情况如何? WHY在点人的另一端没有放下物体? 我有什么错误?


是, 我正致力于一项学校任务,这就是为什么我不想印刷我的法典,我希望自己完成,但我需要帮助找到问题。 我认为,我仍然需要解释各种要点。 谢谢。

最佳回答

问题1是正确的。 在第二部分,<代码>V*oneV = vList[i]将是正确的辛加。 在您的同学中,你又一次回避了(将一类标的作为此类物体的指点)这一显然正在破坏你的代码。

EDIT: Since you are using the correct syntax, the reason of segfaults would depend on your memory management of the objects of type V. If you have inserted addresses of objects created on the stack (automatic vars, not by new or malloc) inside a function and are trying to access them outside of it, then the pointers would be dangling and your code will crash.

问题回答

Class N has to manage the number of elements in a list somehow. The usual approaches are to make a public function which returns the number of elements in the array, or to provide an iterator function which loops over all the list s elements.


An array with N elements are stored at array[0] through array[N-1]. You re accessing one past the end of the array.

最初的一条规则是:

  • you are initializing correctly (new instead of automatic/local variables)
  • you are accessing the elements correctly (not like in the typo you posted in the question - based on your comment)
  • you are using the right size

如果你走到所有正常的轨道上,一切都是k,那么就能够确保特别注意你的休息时间/面积的计算,以及可能给你写上意想不到的地址的任何东西。

有可能在意外地点和营地书写垃圾;然后在意外地点发生错误......我认为,最糟糕的是,有些档案描述者变数由于这些变数在变数之前就错了——在文件相关功能上消失,而这种功能似乎与幻觉有关。

theList would be pointing at the 1st element of the array? Given that the size of array is known, can I loop through with index i and say V *oneV = *vList[i]?

是的,这是正确的。

I m guessing that might be the reason why I am getting seg fault or bus error. Could it be the case?

是的,如果你有一位无效的点子,并试图回避,你就会遭到ault。

WHY did I loose the object at the other end of a pointer? What did I do wrong?

如果不看到实际守则,就难以预测。 很可能的原因是,要么你没有填写<代码>。 V** 正确或在将V*点名放在<代码>后 五** 阵列,你正在从其他地点删除该物体。 BTW,我假定,你正在使用<条码>新分配记忆,这一假设是否正确?





相关问题
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?

热门标签