Skip to main content
gamedev.net
🔒 Locked

structs and objects 101

Started by BlackLiquid 7 replies 700+ views
Original Post
BlackLiquid
BlackLiquid
Hi all, Been trying to make a c++ project more object oriented, but I've come across a problem of insurmountable proportions :) I'll try not to bore you with to many details right of the bat, but ask a more general question regarding structs and objects. The original project only uses structs and I've now converted a few of them to classes. I then went through the program an replaced any reference to the struct with a reference to the class so structName* myVar = new structName[size]; became className* myVar = new className[size]; and keeping the deletes as they were (hope the above example doesn't reveal my severely lacking c++ knowledge :) ) Anyways ... when I run the project now I get this: Debug Assertion Failed! ----------------------- Program: ... File: dbgheap.c Expression: _CrtCheckMemory() DAMAGE: after Normal block (#57) at 0x003C46A0. Normal located at 0x003C46A0 is 1 bytes long. I've googled a bit and from what I could gather this error occurs when writing beyond the memory that has been allocated for a given array, but since the project works with the structs I don't really understand how that could be a problem here, so I'd like to think that I'm not fully understanding how classes are different from structs ... am I right? Phew .. guess I didn't keep this as short as I promised :) Sorry about that. I'd appreciate any pointers that could resolve this. I checked the call stack when the error occurs and the last part of my code that is used is a new operator before the program heads into malloc, alloc and whatnot country. The offending new operator is inside a for loop and the error pops up after a bunch of loops ( 40th to be exact ).
Fruny
Fruny
a) There is no difference between a struct and a class, except default access controls (public for struct, private for class).

b) Congrats for having googled for the error, it's not often that people do.

b) Show the relevant code.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Seriema
Seriema
Are you deallocating correctly? using delete [] and not just delete?

Well I can't give any specifics about why you're getting your error. But I have 2 pointers for you.
A) Are you sure you have to allocate/deallocate inside the loop, can't you reuse the memory?
B) You should use std::vector if you need a dynamic array
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made c
Qw3r7yU10p!
Qw3r7yU10p!
When you're naming your classes/structs you don't need to put that into the name:

struct Renderer {};//becomesclass Renderer {};//andRenderer* rend = new Renderer;//doesn't have to change.
Seriema
Seriema
petewood: I think that was a example for us to understan what was going on, I don't think that's real code ;)
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made c
BlackLiquid
BlackLiquid
The arrayes are deleted using delete[]. I'll check out the std::vector though I'd like to get it working without it.

Let's see now ... the code. The project is fairly big as far as I'm concerned so this very small part probably won't tell you anything but here goes ...

    for (int i=0; i<o1.vn; i++)    {	  /* This is where the debugger points to */      o2.an = new CVector[o1.sn];      for (int j=0; j<o1.sn; j++)	o2.an[j] = o1.an[j];    }


The method on top of the call stack is _heap_alloc_dbg (dgbheap.c) which is where I assume the error happens. The above code is the last part of my code that is used.

o1 and o2 are polyhedrons with a bunch of variables (its defined as a struct), 'an' being one of them. 'an' is defined as

CVector **an; (CVector is one of the classes I've converted from a struct)

'an' stores average normal vectors for use with illuminating a 3d object (sn contains the face count of the object).

The above code snipet is from a small method, copy_an(...), that copies the 'an' variable from one polyhedron struct to another. The copy_an method is called from a method fill_polyhedron that draws the object to screen and is called in the main program's loop. The program draws the object correctly and if I press Ignore when the assertion pops up the object rotates as it should for a couple of milliseconds until the error pops up again.

This is probably not much to go on ... but then again I don't wan't to dump all the code here and wait for someone to fix it. I'll try and go through the project and check all the new and delete operators.

[Edited by - BlackLiquid on November 18, 2004 10:45:20 AM]
Seriema
Seriema
*looks at the code snipped*

Are you sure that o1.sn is always valid? So it doesn't become -1 or something somtime? Because that could explain it. Just check by printing out o1.sn right before calling new.
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made c
BlackLiquid
BlackLiquid
Hmm .. checked out the o1.sn value and it's never -1 ... always 2 actually. A weird thing is that the program runs without errors in Visual C++ 6.0 (tested on another machine) but the error pops up when I exit the program. In .NET (on this machine)the error appears immediately. Still haven't looked at all the allocating/deallocating yet ... debugging makes me cry but I think I'll be more motivated after a little nap :D. I'll try to attack this thing tomorrow. Thanks for any time spent reading and/or replying to this thread.
flangazor
flangazor
If on.an's type has pointers in it, make sure it has an operator= that does what you want and is actually being called.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.