Stack Overflow

This is a discussion on Stack Overflow within the C++ Programming forums, part of the General Programming Boards category; I wish to create a class for self-extracting setup files, but this code creates a stack overflow error, which I ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    23

    Stack Overflow

    I wish to create a class for self-extracting setup files, but this code creates a stack overflow error, which I realize indicates lack of memory; how should I remedy this problem w/out sacrificing functionality? (btw: I did not insert any smilies that may exist in this post)
    Code:
    File: game.h
    #include<func.h>/*<--Contains all relevant headers*/
    class field;
    class install;
    class field
    {
     public:
     field();
     ~field();
     int mn[24][80];
     int clr[24][80];
     int bkg[24][80];
     int rsv[24][80][128];
     void dis();
     void fill(int x1,int x2,int y1,int y2,int z1,int z2);
     void color(int x1,int x2,int y1,int y2,int z1,int z2);
     void rpc(int x1,int x2,int y1,int y2,int w,int z);
     void output(int x,int y);
     void outpt(int x,int y,int z);
     void save(char a[]);
     void load(char a[]);
     void graph(int t1,int t2);
    };
    field::field()
    {
     fill(0,0,23,79,32,32);
     color(0,0,23,79,7,0);
    }
    field::~field()
    {
    }
    void field::graph(int t1,int t2)
    {
     int t;
     for(t=t1;t<=t2;t++)
     {
      if(t>-1&&t<24)
      outpt(23-t,t,254);
     }
    }
    void field::save(char a[])
    {
     int x,y;
     ofstream file(a);
     for(x=0;x<24;x++)
     {
      for(y=0;y<80;y++)
      {
       file<<(char)mn[x][y];
       file<<(char)clr[x][y];
       file<<(char)bkg[x][y];
      }
     }
     file.close();
    }
    void field::load(char a[])
    {
     int x,y;
     char z;
     ifstream file(a);
     for(x=0;x<24;x++)
     {
      for(y=0;y<80;y++)
      {
       mn[x][y]=(int)file.get();
       clr[x][y]=(int)file.get();
       bkg[x][y]=(int)file.get();   
      }
     }
     file.close();
    }
    void field::color(int x1,int y1,int x2,int y2,int z1,int z2)
    {
     int x,y;
     for(x=x1;x<=x2;x++)
     {
      for(y=y1;y<=y2;y++)
      {
       if(z1!=-1)
       clr[x][y]=z1;
       if(z2!=-1)
       bkg[x][y]=z2;
      }
     }
    }
     
    void field::fill(int x1,int y1,int x2,int y2,int z1,int z2)
    {
     int x,y;
     for(x=x1;x<=x2;x++)
     {
      for(y=y1;y<=y2;y++)
      mn[x][y]=rand()%(z2-z1+1)+z1;
     }
    }
    void field::output(int x,int y)
    {
     textcolor(clr[x][y]);
     textbackground(bkg[x][y]);
     gotoxy(y+1,x+1);
     cout<<(char)mn[x][y];
    }
    void field::outpt(int x,int y,int z)
    {
     mn[x][y]=z;
     output(x,y);
    }
    void field::dis()
    {
     int x,y;
     for(x=0;x<24;x++)
     {
      for(y=0;y<80;y++)
      output(x,y);
     }
    }
    void field::rpc(int x1,int y1,int x2,int y2,int w,int z)
    {
     int x,y;
     for(x=x1;x<=x2;x++)
     {
      for(y=y1;y<=y2;y++)
      {
       if(mn[x][y]==w)
       mn[x][y]=z;
      }
     }
    }
    class install
    {
     public:
     install(int x);
     ~install();
     void fi(field a,char*b);
     int fn[2];
     field board;
    };
    install::install(int x)
    {
     fn[0]=x;fn[1]=0;
     board.fill(0,0,0,fn[0]-1,178,178);
     board.dis();
    }
    install::~install()
    {
     cout<<"Installation Complete";
     getch();
    }
    void install::fi(field a,char b[])/*<--Problem area*/
    {
     a.save(b);
     fn[1]++;
     board.outpt(0,fn[1],219);
    }
    File: krad.cpp
    #include<game.h>
    int main(int argc,char*argv[])
    {
     int x;
     install stuff(5);
     field yo;
     for(x=0;x<5;x++)
     {
      getch();
      stuff.fi(yo,"yo.txt");/*<--program is fine w/out calling install::fi and simply performing the same three functions manually*/
     }
     getch();
     return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >void install::fi(field a,char b[])/*<--Problem area*/

    field contains a lot of data. You might try passing it as a reference, instead of passing the whole class:

    void install::fi(field &a,char b[])

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    23
    I added the & and the code now works-thx.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack overflow errors in 3 areas
    By ulillillia in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 04:20 PM
  2. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-12-2006, 12:41 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 06:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21