Simple C++ problem

bigal40

Senior member
Sep 7, 2004
849
0
0
for(io=ref; io<i; io++){
mObject[io].setRadius(mObject[(io+1)].getRadius());
mObject[io].setType(mObject[(io+1)].getType());
mObject[io].setName(mObject[(io+1)].getName());
}

I am a beginner at programming, and my program keeps crashing everytime it gets to this part of code, but it compiles fine.
I have an array of objects, and I want to delete one of them. So basically I am trying to set the name, radius and type variables of the object that I want to delete to be equal to name, radius and type variables of the next object in the array, and then move all the other objects down one space in the array.

Thanks for your help
 

bigal40

Senior member
Sep 7, 2004
849
0
0
ref is an int variable, it comes from a function that searches for the mObject that the user wants deleted and returns the index of that object in the array.
 

nickbits

Diamond Member
Mar 10, 2008
4,122
1
81
I'm going to assume i is the length of your array. If that is true then your io variable is referencing undefined memory during the last pass since oi will equal the last index + 1.
 

bigal40

Senior member
Sep 7, 2004
849
0
0
Originally posted by: nickbits
I'm going to assume i is the length of your array. If that is true then your io variable is referencing undefined memory during the last pass since oi will equal the last index + 1.

That's a great point, I thought that would do it. I fixed it, but it still crashes.
I appreciate the help though.
 

bigal40

Senior member
Sep 7, 2004
849
0
0
I just ran a debug on it. I don't know how to read this:

#0 0x0040bdf9 __gnu_cxx::__exchange_and_add (??:??)
#1 0x004310f9 std::string::assign (??:??)
#2 0x00432fbd std::string::eek:perator= (??:??)
#3 0x00401c4d Mirror::setType (??:??)
#4 0x00404a10 deletemirror (??:??)
#5 0x004023ac mirrormenu (??:??)
#6 0x00401fba main (??:??)


mirrormenu is the menu that I call up deletemirror from. Mirror is the class of the mObjects
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: bigal40
Originally posted by: nickbits
I'm going to assume i is the length of your array. If that is true then your io variable is referencing undefined memory during the last pass since oi will equal the last index + 1.

That's a great point, I thought that would do it. I fixed it, but it still crashes.
I appreciate the help though.

How did you fix it? Your above stack trace suggests that your setType function is crashing because of an invalid memory address. This often happens when an array is over-indexed...
 

bigal40

Senior member
Sep 7, 2004
849
0
0
I changed the for loop to
for(io=ref;io<i-1;io++)

Isn't it strange that setType is crashing, but setRadius works ok. If i move up setName before setType, the debugger still shows the problem is with setType.
I just tried the program with the
"mObject[io].setType(mObject[(io+1)].getType()); "
line removed and it doesn't crash.

I guess this means there is something wrong with my setType, I don't know what the problem would be because it is basically identical to the setName
Thanks for your help, I'm gonna sleep on it and see what i can come up with.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Lack of crashing does not imply correctness. C and C++ are not guaranteed to crash if you touch memory that you're not supposed to touch.

Try running the whole thing under valgrind. For debugging purposes, you may find it useful to break some of those statements into multiple lines. e.g.:
mObject[io].setType(mObject[(io+1)].getType()); ->

Foo foo = mObject[io+1].getType();
mObject[io].setType(foo);
 
Sep 29, 2004
18,656
68
91
Why are you doing this?
mObject[(io+1)]

Arrays are base 0, not 1. You might just be overflowing the arrays buffer at run time.

what IDE are you using? I recommend using debug mode and stepping through the code. This tool is more useful than you realize. learn it now to save headaches laters.
 

bigal40

Senior member
Sep 7, 2004
849
0
0
Originally posted by: IHateMyJob2004
Why are you doing this?
mObject[(io+1)]

Arrays are base 0, not 1. You might just be overflowing the arrays buffer at run time.

what IDE are you using? I recommend using debug mode and stepping through the code. This tool is more useful than you realize. learn it now to save headaches laters.

I am trying to delete one member of the array, so I want to move all the members in the array above the one that is being removed down one space.
I'm using CodeBlocks, I did run the debug mode, I posted the results of it above, I'm not sure what it means.

Originally posted by: degibson
Lack of crashing does not imply correctness. C and C++ are not guaranteed to crash if you touch memory that you're not supposed to touch.

Try running the whole thing under valgrind. For debugging purposes, you may find it useful to break some of those statements into multiple lines. e.g.:
mObject[io].setType(mObject[(io+1)].getType()); ->

Foo foo = mObject[io+1].getType();
mObject[io].setType(foo);

Is there some type of windows port for Valgrind?
 

bigal40

Senior member
Sep 7, 2004
849
0
0
Thanks for the help everyone, I figured it out. The problem was with the ref variable, the function that set ref was setting it to -1, not the index of the array like it should have been.