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

    Total newbie problem.. corrupted stack around variable

    I've spent a long, long time trying to figure out what I'm doing wrong, and I still can't find it. I'm not used to Visual Studio's Debugger, and I hope you all won't mind me asking for help. I'm getting a stack around variable error, which I know is when something tries to write out of bounds. Anyway, here's the code (my first attempt at a C++ class)

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Studentgrade
    {
    private:
    	char student[24];
    	int quizzes[4];
    
    public:
    	void askName()
    {
    	cout << "Please enter the student's name: ";
    		cin.getline(student, 24);
    		quizzes[0] = 0;
    		quizzes[1] = 0;
    		quizzes[2] = 0;
    		quizzes[3] = 0;
    }
    
    	void setGrade(int sc, int x)
    {
    	if(x>=0 && x<4)
    	{
    		quizzes[x] = sc;
    	}
    }
    
    	void getName(char name[]) const
    {
    	strcpy_s(name, strlen(name), student);
    }
    
    	float getAve() const
    {
    	int tot = 0;
    	tot = (quizzes[0] + quizzes[1] + quizzes[2] + quizzes[3]);
    	return (tot/static_cast<float>(4.0));
    }
    };
    
    int main()
    {
    	Studentgrade sg;
    	char student[24];
    	sg.askName();
    	sg.setGrade(100,0);
    	sg.setGrade(90,1);
    	sg.setGrade(100,5);
    	sg.setGrade(100,3);
    	sg.getName(student);
    	cout << student << "'s quiz average is " << sg.getAve() << endl;
    	return 0;
    }
    If I debug with "john smith," the string never even makes it into the sg.student instance. I guess I don't understand how member functions are supposed to write to the instance rather than original class declaration (if that makes sense)

    Sigh.

  2. #2
    Join Date
    Feb 2009
    Posts
    2

    Re: Total newbie problem.. corrupted stack around variable

    Oh... I guess this should be in "C++ Programming" and not "Visual C++."

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Total newbie problem.. corrupted stack around variable

    Save yourself a great deal of time and trouble: Use std::strings rather than char arrays. You're already including the header for them anyway.

  4. #4
    Join Date
    Feb 2009
    Posts
    42

    Re: Total newbie problem.. corrupted stack around variable

    sg.setGrade(100,5);

    you don't have fifth element in int quizzes[4];

    should be:
    sg.setGrade(100,2);

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Total newbie problem.. corrupted stack around variable

    ^Not the problem.

  6. #6
    Join Date
    Feb 2009
    Posts
    42

    Re: Total newbie problem.. corrupted stack around variable

    Quote Originally Posted by Lindley View Post
    ^Not the problem.
    Its not. Its bug.

    void getName(char name[]) const

    also have a problem/bug as you wish. You should pass parameter as reference.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Total newbie problem.. corrupted stack around variable

    Quote Originally Posted by streamer View Post
    Its not. Its bug.
    Maybe; or maybe it's a test to make sure that the "if" statement within the function properly catches bad input.

    void getName(char name[]) const

    also have a problem/bug as you wish. You should pass parameter as reference.
    Reference passing is pretty much irrelevant in the case of arrays. If he were to substitute a std::string that would be true.

  8. #8
    Join Date
    Nov 2006
    Posts
    14

    Re: Total newbie problem.. corrupted stack around variable

    Quote Originally Posted by Georgie Fruit View Post
    I've spent a long, long time trying to figure out what I'm doing wrong, and I still can't find it. I'm not used to Visual Studio's Debugger, and I hope you all won't mind me asking for help. I'm getting a stack around variable error, which I know is when something tries to write out of bounds. Anyway, here's the code (my first attempt at a C++ class)

    Code:
    void getName(char name[]) const
    {
    	strcpy_s(name, strlen(name), student); //are you sure?
    }
    If I debug with "john smith," the string never even makes it into the sg.student instance. I guess I don't understand how member functions are supposed to write to the instance rather than original class declaration (if that makes sense)

    Sigh.
    Well your askName() method works fine for me, are you sure it gives you a problem? Regardless, you ought to look again at the way you're using strcpy_s, particularly the way you're telling it how long the source string is, hmm? Also, in a little project like this it's a good idea to not give a local variable the same name as a member (as you have with student in main()), the compiler will cope perfectly well but it can confuse the human.

    HTH

  9. #9
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: Total newbie problem.. corrupted stack around variable

    I believe GrimmRiffer is on the right track.

    You're strcpy_s function is incorrect...the second parameter is the size of the DESTINATION buffer - in this case it should be 24.

    By using
    Code:
    strcpy_s(name, strlen(name), student);
    you're telling it that the buffer is only the same length as the name - which doesn't leave room for the terminating NULL character.

    Hope that helps.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

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