PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

debugging another First-chance exception

Having added a new struct of particles & created an array of these via NEW
Code:
particles = new particle_data[MAX_PARTICLE];//up to MAX_PARTICLE active bullets etc
I'm now getting problems similar to previous ones which are either allignment of memory allocation related.
No errors on PC VC6 build only PPC ARM release build. (trying to get a full debug version working but have a release version with debug info currentlyas PF is not playing ball)

Question 1:- Does NEW use stack or heap & if heap how do I know if this has enough space. I've changed project link settings but error still happens. nb: I made stack allocation much bigger to no avail & I can still get error even with a few particles so it may be allignment & not allocation.

Question 2:- How / where do I force allignment. I saw a Pragma command but no example.

thanks in advance
new should always use the heap - items that aren't local should never be assigned on the stack as they'll get trashed as the stack unwinds to calling functions. It should return a null pointer if the memory requested by new was not assigned, I believe (can't test it right now, but you can try it very easily).

#pragma pack
is the command you want to use to align elements withing a structure.

It should be used something like this:

#pragma pack (push) <-- store the current alignment
#pragma pack (4) <-- set the alignment to 4 byte boundary per element

struct ...
{
}

#pragma pack (pop) <-- restore the default alignment

By my (brief) reading of the help, "full" structures/classes are padded to an 8-byte boundary (set by the /Zp command line option, defaulting to 8), but you may need to use the pragma pack on the individual elements within the structure to get it to behave.

Personally I always try and put integers, etc, at the beginning of the structure, and then individual bytes at the end (only when I really have to use bytes/chars).
Ok, so far so good (but confusing).
I used the pragma pack 4 feature for the particles struct and now the game gets much further. The particles are initialised & the guns fire the bullets etc however now I get the error with trade goods which were previously working fine. They come after the particles in the .h file where I define the structs but before them in the series of new's. I will try a few more pack's to see what happens.
Pejo Software - Per · 01 Mar 2004, 16:55 UTC
Here is a wild guess:
It sound like you are overwriting memory somewhere in your code, The problem is probably not where you expect it to be!
yep overwriting memory, or running out of memory. Do you have any very large structures in your game ? I ran into almost the exact same type of problem back when I was doing a malloc(1024*10). I would get a first chance exception at some point in the code, and if I were to change the code it would shift to a different location.