Stack around the variable 'total' is corrupted

This is a discussion on Stack around the variable 'total' is corrupted within the C++ Programming forums, part of the General Programming Boards category; Code: //before this, i calculate the various taxes float total[4]; //problem can be fixed by making this float total[5];, but ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    7

    Stack around the variable 'total' is corrupted

    Code:
    //before this, i calculate the various taxes
    
    	float total[4]; //problem can be fixed by making this float total[5];, but then
                                  //i have an empty slot. total[5].
    	float totrate;
    	int tothours;
    
    	for (x=0;x<5;x++)  //problem here
    	{
                   total[x]=0;
    	       for(y=0;y<size;y++) total[x]+=money[y][x];
    	}
    
    //later on, I cout<< the various money variables, and I cout<<the total variables manually
    //cout<<left<<setw(9)<<total[0].....<<setw(9)<<total[4]; Program ends here.
    Here's a small snippet of code, that includes ONLY where I'm having trouble. If anyone wants, I can post the full code.



    note:
    money[y][x]: various taxes that were calculated earlier on. x represents tax type [ie. 0 is federal, 1 is social security, etc]. y corresponds with the person.
    There are five taxes.
    0=federal
    1=ss
    2=fica
    3=health
    4=health2


    As it is right now, the program works 100%,except for the error at the end
    Stack around the variable 'total' is corrupted

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    But but but : 0 1 2 3 4 -- I count five things; so you need five elements in your total array; so you need to declare it as float total[5].

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> problem can be fixed by making this float total[5];, but then i have an empty slot. total[5]

    No you don't. total[5] doesn't exist if the array has five elements. Valid indexes for an array go from 0 to size-1. If you want five elements, change it to 5 in both the array declaration and the loop.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    7
    tabstop/daved:
    Err, yeah. I have 5 elements
    but total[5] allows you to edit

    total[0]
    total[1]
    total[2]
    total[3]
    total[4]
    total[5]

    which is actually 6 elements.

    I only need to go up to total[4].
    Also, pretty much the same thing works in a different segment of code, where

    float money[slot][4]
    money[][0]=blah
    money[][1]=blah
    money[][2]=blah
    money[][3]=blah
    money[][4]=blah
    and it works <i>just fine</i>


    oh well, if float total[5] works, it works, even if it doesn't match up completely with money[][4].



    However, I do have a new question which is pretty perplexing:
    Code:
    	float money[40][4]; //gross, fedtax, health, fica, net
    	for (i=0;i<slot;i++)
    	{
    		money[i][0]=c_gross(rate[i],hours[i]);
    		money[i][1]=c_fed(money[i][0]);
    		money[i][2]=c_health(money[i][0],dependents[i],health[i]);
    		money[i][3]=c_fica(money[i][0]);
    		money[i][4]=c_net(money[i][0],money[i][1],money[i][2],money[i][3]);
    
    		
    		cout<<"\n";
    		cout<<i<<": "<<money[i][0]<<"-"<<money[i][1]<<"-"<<money[i][2]<<"-"<<money[i][3]<<"="<<(money[i][0]-money[i][1]-money[i][2]-money[i][3]);	
    		cout<<"  ACTUAL: "<<money[i][4];
    		cout<<"   0th actual: "<<money[0][4];   
    	}
    I have a different problem in this code.
    The c_gross, c_fed etc is there just to calculate money[i][0],[1] etc.

    money[][0] is gross
    [1],[2],[3] is tax [I wasn't completely honest in my first post because you guys didn't need to know about gross/net]
    [4] is net.

    This is what my program outputs

    Code:
    Welcome to BLAG
    Name: 1
    Hours: 12
    Rate: 12
    Dependents: 1
    Health: 1
    
    New employee? (Y or N): y
    
    Name: 2
    Hours: 1
    Rate: 1
    Dependents: 1
    Health: 1
    
    New employee? (Y or N): y
    
    Name: 4
    Hours: 4
    Rate: 4
    Dependents: 4
    Health: 1
    
    New employee? (Y or N): n
    
    //THIS IS FROM THE CODE SNIPPET
    0: 144.00-0.00-18.75-12.24=113.01  ACTUAL: 113.01   0th actual: 113.01     //Correct!!
    1: 1.00-0.00-18.75-0.09=-17.83  ACTUAL: -17.83   0th actual: 1.00                //0th net equals gross[1][0] now!
    2: 16.00-0.00-40.80-1.36=-26.16  ACTUAL: -26.16   0th actual: 1.00
    //END CODE SNIPPET
    
                   HOURS      PAY    GROSS      FED            HEALTH      NET
    NAME          WORKED     RATE      PAY      TAX     FICA     PLAN      PAY
    1                 12    12.00   144.00     0.00    18.75    12.24     1.00
    2                  1     1.00     1.00     0.00    18.75     0.09    16.00
    4                  4     4.00    16.00     0.00    40.80     1.36   -26.16
    
    TOTAL             17    17.00   161.00     0.00    78.30    13.68    -9.16
    Press any key to continue . . .
    The problem is, money[0][4] (which represents the first guy's net) is changed into money[1][0] (which represents the second guy's gross).

    0th person's net becomes 1st person's gross
    1st person's net becomes 2nd person's gross
    2nd person's net remains the same. //good!

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> but total[5] allows you to edit ... total[5]
    This is incorrect. If you declare an array like this:
    Code:
    float total[5];
    You are not allowed to edit or look at an element total[5]. That is illegal and will do bad things in your program, including possibly corrupting the stack and/or changing values elsewhere in your code.

    The rule is simple. When you declare the array, you specify the size (in this case 5). When you access the elements in the array, you are only allowed to access the elements at positions 0 through size-1 (in this case 0, 1, 2, 3 and 4).

    If you want 5 elements, declare the array with the number 5 and iterate while the index is less than 5 and don't use index 5 to access the array.

    >> However, I do have a new question which is pretty perplexing
    That is the same problem. You declare your array to have 4 elements, but you access index 4 which is out of bounds so it modifies something else.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    7
    Thanks Daved
    I guess I'm still getting used to arrays
    It works perfectly #.#

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 11-03-2008, 08:48 PM
  2. stack around the variable corrupted
    By chintugavali in forum C++ Programming
    Replies: 2
    Last Post: 01-09-2008, 12:01 PM
  3. Confused about Memory
    By gL_nEwB in forum C++ Programming
    Replies: 22
    Last Post: 06-20-2006, 07:32 PM
  4. Finished Stack
    By the pooper in forum C Programming
    Replies: 11
    Last Post: 02-02-2005, 09:52 AM
  5. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 10:13 AM

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