|
-
January 24th, 2007, 09:51 AM
#1
[RESOLVED] Stack Overflow
Ok, it seems I have made a beginners error. I have the following structures defined in my h file
Code:
typedef struct {
unsigned char B;
unsigned char G;
unsigned char R;
unsigned char A;
} pix32;
struct FinalTGAFile
{
char IDLen; //length of ID field
char MapType; //0=none, 1=present
char ImgType; //0=none, 1=c-map, 2=true,
short FirstColor; //where to start in colormap
short MapLen; //number of colors to set
char EntrySize; //number of bits per color entry
short Xorigin;
short Width;
short Height;
char Depth;
char Descriptor;
pix32 Pixel[];
};
The cpp code extract is as follows
Code:
//Create a new TGA header
FinalTGAFile tgaCell;
memset(&tgaCell,0, 18);
tgaCell.ImgType = 2;//true-color
tgaCell.Height = 100;
tgaCell.Width = 4800;
tgaCell.Depth = 32;//has alpha
tgaCell.Descriptor = 32;
//extract of code that moves the pixels around
int j=0;
int p;
int count=0;
for(int y=0; y<100;y++)
{
if(y<256)
{
int j=y*1024;
for(int h=0;h<18;h++)
{
for (p=j; p<j+1024;p=p+4)
{
//Eventually this will bomb out when count gets too large
tgaCell.Pixel[count].B = final].finalimage[p];
tgaCell.Pixel[count].G = final[h].finalimage[p+1];
tgaCell.Pixel[count].R = final[h].finalimage[p+2];
tgaCell.Pixel[count].A = final[h].finalimage[p+3];
count++;
I then basically take a whole series of tga files and stitch them together by filling up the pix32 structure with pixels.
Based on reading I have done it appears this is because this array is getting put on the stack (rather than heap) and so at some point in time it is larger than the max allowed memory.
However I am unsure of the method used to allocate this to the heap using the structures I have created. Can someone please provide some guidance on how to best do this..
Regards, Andrew
-
January 24th, 2007, 10:01 AM
#2
Re: Stack Overflow
FinalTGAFile* tgaCell = new FinalTGAFile;
-
January 24th, 2007, 10:08 AM
#3
Re: Stack Overflow
First of all thanks for replying...
Replacing
FinalTGAFile tgaCell;
with
FinalTGAFile* tgaCell = new FinalTGAFile;
renders all my code using for example tgaCell.ImgType invalid as left of '.ImgType' must have class/struct/union
In this situation would I have to use tgaCell->ImgType?
Or is there something else I should do...
PS If I do this....
following the call to the new FinalTGAFile gives the following error at
tgaCell->ImgType = 2;
Unhandled exception at 0x004212bf in Links Extender.exe: 0xC0000005: Access violation writing location 0x00000002
Last edited by Mogulbasher; January 24th, 2007 at 10:16 AM.
-
January 24th, 2007, 10:11 AM
#4
Re: Stack Overflow
What compiler allows this? This is illegal C++.
Regards,
Paul McKenzie
-
January 24th, 2007, 10:12 AM
#5
Re: Stack Overflow
In this situation would I have to use tgaCell->ImgType?
yes. . it's a pointer now .. so you have to use '->'.
btw : don't forget do delete tgaCell when you're done with it.
-
January 24th, 2007, 10:17 AM
#6
Re: Stack Overflow
Unhandled exception at 0x004212bf in Links Extender.exe: 0xC0000005: Access violation writing location 0x00000002
Code:
memset(&tgaCell,0, 18);
tgaCell is a pointer now.
Code:
FinalTGAFile* tgaCell = new FinalTGAFile;
memset(tgaCell, 0, 18);
-
January 24th, 2007, 10:17 AM
#7
Re: Stack Overflow
 Originally Posted by Paul McKenzie
What compiler allows this? This is illegal C++.
Regards,
Paul McKenzie
VS2005 allowed it... I changed it to [1536000]
-
January 24th, 2007, 10:24 AM
#8
Re: Stack Overflow
Great worked a treat, thank you very much
Amdrew
-
January 24th, 2007, 01:12 PM
#9
Re: Stack Overflow
 Originally Posted by Mogulbasher
VS2005 allowed it...
If it's a compiler extension, you should be aware of what that syntax really means. Otherwise it is a compiler bug allowing that syntax to be accepted.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|