CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Aug 2005
    Posts
    59

    [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

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Stack Overflow

    FinalTGAFile* tgaCell = new FinalTGAFile;

  3. #3
    Join Date
    Aug 2005
    Posts
    59

    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.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Stack Overflow

    Code:
    pix32  Pixel[];
    What compiler allows this? This is illegal C++.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    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.

  6. #6
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    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);

  7. #7
    Join Date
    Aug 2005
    Posts
    59

    Re: Stack Overflow

    Quote Originally Posted by Paul McKenzie
    Code:
    pix32  Pixel[];
    What compiler allows this? This is illegal C++.

    Regards,

    Paul McKenzie
    VS2005 allowed it... I changed it to [1536000]

  8. #8
    Join Date
    Aug 2005
    Posts
    59

    Re: Stack Overflow

    Great worked a treat, thank you very much

    Amdrew

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Stack Overflow

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured