EDIT: I have found the error: I did not initialize an array with a size. question can be closed.
I have a class V
, and another class N
. An object of N
will have an array of po开发者_开发百科inters 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.
I have been using debugger to trace through the whole process of my program running, the thing I found was that after using V *oneV = vList[i]
, the value of the pointers in the array, vList
, were the same as when they were created, but if I follow the pointer to where it is pointing at, the object was gone. I'm guessing that might be the reason why I am getting seg fault or bus error. Could it be the case? WHY did I 'loose' the object at the other end of a pointer? What did I do wrong?
and yes, I am working on a school assignment, that's why I do not want to print out my codes, I want to finish it myself, but I need help finding a problem. I think I still need explanation on array of pointers. Thank you
Q1 is right. For the second part, V *oneV = vList[i]
would be the correct syntax. In your syntax you are dereferencing one more time (treating an object of type V
as a pointer to such an object) which obviously is crashing your code.
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.
First rule out the initial ones:
- 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
If you go through all the normal ones and everything is k, then make sure to pay special attention to your loops / size calculations / and anything else that could be causing you to write to unintended addresses.
It is possible to write garbage at unintended locations & then get the error in unexpected places ... the worst I saw like that, was some file descriptors's variables being corrupted because of an array gone wrong right before those variables - it broke on file related functions, which seemed v. crazy.
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]?
Yes, that is correct.
I'm guessing that might be the reason why I am getting seg fault or bus error. Could it be the case?
Yes, if you have an invalid pointer and try to dereference it you'll get a segfault.
WHY did I 'loose' the object at the other end of a pointer? What did I do wrong?
That is difficult to predict without seeing the actual code. Most probable causes are that either you are not filling the V**
correctly or after putting a V* pointer inside V**
array you are deleting that object from some other place. BTW, I am assuming that you are allocating memory using new
, is this assumption correct?
精彩评论