Problem with linked list

Started by
33 comments, last by SiCrane 14 years, 10 months ago
Quote:Original post by tnutty
But,since my program is such a catastrophe what guidelines would you provide for implementing a decent linked-list?


Well, first off, why do you want to implement one? The things that you need to know in order to do it properly can be learned just as well in many other ways.
Advertisement
Quote:Original post by Zahlman
Quote:Original post by tnutty
But,since my program is such a catastrophe what guidelines would you provide for implementing a decent linked-list?


Well, first off, why do you want to implement one? The things that you need to know in order to do it properly can be learned just as well in many other ways.


For practice, as other of my recent comment suggests.

You know, I think I picked up some bad habits from online tuts, like from this
one : http://richardbowles.tripod.com/cpp/linklist/linklist.htm


Our whole life is a opengl application.
Tripod is a site that lets the general public create small webpages. You shouldn't really trust it for quality, for that reason: you need to verify that the author knows what s/he is talking about. And I would guess, without even reading the page, from that colour scheme and background choice that the author of the tutorial in question is not exactly a professional. :)
I will think as I write.

In the first box of code, I cannot find any instance where you actually define and set the variable "Exercise", hence it is a NULL pointer. The size of a NULL pointer is also undefined, so the loop accesses an undefined variable to determine how many times to loop and has no clue.

*****
//Add the type of workout
//on the unique body part
//and the number of sets
//each with certain weight
for(i =0; i <= Exercise.size(); i++) //<--- PROBLEM LIES WITHIN "Exercise.size(). While degugging it crashes my program.
*****

At this point you have "i" and "j" defined, "Temp" is a pointer to a workOutRoutine structure as also is the variable "EndList". You also have a string "dummy", (no I am not calling you a dummy),but no where is any variable mentioned "Exercise" until you access it in the loop. The program does not know if it is a string, integer or whatever. The address to this variable can be any number. It happens that it is near zero, which is protected memory where the operating system resides. That is why the access violation. The location, ie: memory address is 4C, that is a low number in the area of BIOS and other system memory.

I see in the full code that it is defined as an array of strings I think, but I have not used the vector function. I do know that when you setup some arrays, especially with strings it starts out with an undefined address until you first assign a value of some sort. I use a listing program in addition to debugger. It tells you a lot more. If you step through the debugger and look at what value "Exercise" is as it progresses, have the debugger stop just before the loop, check to see what "Exercise" is, the address and the value.

I have also never encountered a program that uses a for loop command with a variable in the loop command that is modified by the loop itself. I have always had the range, 1 to 6 or 1 to 10, set before starting the loop. If I needed to vary the number of iterations, I would use a repeat, or a while loop instead. These commands are designed to be variable, whereas the for loop is usually set before the iterations begin. At least that is the conformity that I stick to.

It looks like you are having the arrays set to 3 and 4, and the string initialized to "NONE" to start off with, if I understand the program. So, it looks like the array is an array of arrays, Exercise[3][4]. I think that the problem is with the size of the array. You really do wish to progress through the exercises, which is 3 and then set the string variable in each of the sets within each of the exercises. Problem? Does size return 12, or does it return 3 when you ask for the size of Exercise? You use the similar statement for each loop, in the "i" portion of the loop, ie: number of exercises and the "j" portion of the loop, the sets. If it returns 12 each time for the whole array, and you want to get 3, then.....

So, it will step through 12 times for each of the loops.

loop 1 = Exercise[0][0] (This is the name of the workout?)
loop 2 = Exercise[0][1] (This is the weight of each set?)
loop 3 = Exercise[0][2]
loop 4 = Exercise[0][3] Oops, Crash, not defined

it even wants to go all the way to:

loop 3 = Exercise[0][11]

I am not sure what the size function will return on the whole set of arrays, whether it returns the number of arrays, or the total number of elements. I have not been programming for a while. I think this is the problem. You need to run the debugger and ask to view the size of the variable Exercise, to see if it is 3, 4 or 12.

In the documentation it states that the size function "Returns the number of elements in the vector container". This would tell me that this number is 12.

Conclusion, set a stop just before the loop and investigate what the size value of the array is. If it is 12, then your loop is going too far.

I hope that I have been clear as mud. :)

Thank you.

John Barradale










Quote:Original post by Scharfschuetze
In the first box of code, I cannot find any instance where you actually define and set the variable "Exercise", hence it is a NULL pointer.

Incorrect: it's a vector of string vectors. See the code in his second post. There's nothing wrong with that - both vectors and strings have default constructors. Besides, pointers need to be dereferenced - but he's using the dot operator instead. So, no pointers there. As others already pointed out, he's probably calling that AddNode function on a bad pointer.

A vector is not a function, bytheway - it's a container class, and part of C++' standard library.
Create-ivity - a game development blog Mouseover for more information.
Quote:Original post by Captain P

A vector is not a function, bytheway - it's a container class, and part of C++' standard library.


I think that might be the problem, because in a another test program,
I made a simple class that uses just a vector of string and a *next variable.
When I try to access the vector of string, it gives me an error, invalid reading
of memory (something similar).

When I use the debugger, First I call the constructor, and the vector.resize()
func is called, and it is correctly sized. But when I call the addNode func,
The vector,exercise has no values, in the debugger it looks like this :


name Value type

Exercise [...]() std::vector<std::vector<std::basic_string ....and some more things

I tried using this->Exercise but its size invalid.

Also note that I created an Init Function, which inits the vector size
but It did not let me initialize it. It gives me another violation error.
I am thinking that the -> operator cannot be used with vectors correctly
without doing something different or complex.
Our whole life is a opengl application.
If you are encountering difficulty with the standard containers, it is almost certain that it is something you are doing incorrectly. I actually don't really understand your post so I am not exactly sure of what problem you are having.

If you post your current code that might help.
Quote:Original post by tnutty
I tried using this->Exercise but its size invalid.

Exercise is not a pointer, so you can't dereference it. Vectors do not behave differently in that regard compared to other types.

Now, as others already pointed out, the object that contains that vector is probably non-existent. A simple example would be this:
workOutRoutine* object;object->AddNode(); // Bwam: object does not point to an actual workOutRoutine instance!

Whereas the following code would work (assuming that AddNode does not contain bugs):
workOutRoutine* object = new workOutRoutine();object->AddNode();


Why does it crash at that specific line? Well, since that pointer isn't pointing at a valid workOutRoutine instance, that memory could contain anything. So when you request the size of Exercise, then the program is actually looking at some random memory content and treating it as if it's a vector. Anything could happen. Be happy that it's crashing, because now at least you know that something is going wrong.
Create-ivity - a game development blog Mouseover for more information.
I get what you mean, but the problem is this :
workOutRoutine *Temp = new workOutRoutine;	/* snippet removed */


In the function Addnode this code lies within. As you can see, I am allocating
a new memory for Temp, then trying to access the members with this object.

I thought this code was doing what Captin P comment was suggesting, no?
Our whole life is a opengl application.
I've taken a more careful look at your code, and it turns out that your understanding of classes is somewhat flawed.

In main(), you're creating a temporary instance of your class. Temporary, because you never bind it to an actual variable. Now, your class' constructor sets two global pointers to NULL. What makes you believe that they will magically point to a valid instance?


I'd say, drop the use of global variables here. They're nothing but confusing. Then, in main, create a local instance (workOutRoutine routine;) and operate on that instance (routine.AddNode();).
Create-ivity - a game development blog Mouseover for more information.

This topic is closed to new replies.

Advertisement