Please help on a C++ error message I don't understand (stack corruption)

  • C/C++
  • Thread starter yungman
  • Start date
  • Tags
    C++ Error
In summary, the error message says that the stack was corrupted around the 2D array of sales, and the programmer does not understand why.
  • #1
yungman
5,718
241
TL;DR Summary
I have an error message I don't understand.
I have an error on this program I don't understand, it said stack corrupted around the 2 dimension array sales.
C++:
//2D sales in divisions in quarters.
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    const int numDiv = 3;// 3 divisions
    const int numQtr = 4; // 4 quarters
    double sales[numDiv][numQtr];//2D array for number of divisions and number of quarters
    double totalSales = 0;
    int div=1, qtr=1;

    cout << " This program calculate the total sales of all divisions.\n";
    cout << " Enter the following sales information: \n\n";
    for (div = 1; div <= numDiv; div++)
    {
        for (qtr = 1; qtr <= 4; qtr++)
        {
            cout << " Enter sales figure of division " << div << ", quarter " << qtr << "= $";
            cin >> sales[div][qtr]; cout << "\n";
        }
        cout << endl;

    }

    for (div = 1; div <= numDiv; div++)
    {
        for (qtr = 1; qtr <= numQtr; qtr++)
        {
            totalSales += sales[div][qtr];
        }
    }
    cout << " The total sales for 3 divisions in 4 quarters is $" << totalSales << ". \n\n";
    return 0;
}

The error message is:
Compile error file write.jpg


I don't understand why, it's a fixed array of sales[3][4] only. Please help.

Thanks
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
Because it’s defined inside main() it’s placed on the stack.

C arrays index from zero to their length-1

your two for loops are indexing from 1 to length meaning you’ve indexed outside the array bounds.
 
  • Like
Likes DaveE, FactChecker, berkeman and 2 others
  • #3
jedishrfu said:
Because it’s defined inside main() it’s placed on the stack.

C arrays index from zero to their length-1

your two for loops are indexing from 1 to length meaning you’ve indexed outside the array bounds.
Thanks. I was experimenting, if I go from 0 to 3, it's fine.

Now I know.

thanks
 
  • Like
Likes berkeman
  • #4
The initial reply was spot on. I'm impressed that the runtime environment was able to name the array near the corruption.

It occurs to me that I've been programming in C++ for almost 20 years and I've never used cout or ever included iostream for that matter. I wonder what else I've never used.
 
  • #5
Halc said:
I wonder what else [in C++] I've never used.
I'd guess you've never used this. Try it -- you'll like it.
 
  • #6
strangerep said:
I'd guess you've never used this. Try it -- you'll like it.
I have the book of course. It's the user-space libraries I don't have. For instance, in the program in the OP, endl in line 23 seems undeclared. It compiled, so it must be something global from the libraries, but I don't know that because I've never used them.

I program for file servers at the multi-thread OS level, so there are no standard libraries, no user space from which other processes might be run, not even a main(). There's just the one process, ~10,000 threads with very limited stacks, always competing with itself.
 
  • #7
Halc said:
It's the user-space libraries I don't have. For instance, in the program in the OP, endl in line 23 seems undeclared. It compiled, so it must be something global from the libraries, but I don't know that because I've never used them.
The line in @yungman's code near the top, using namespace std; allows everything in that namespace to be used without having to be qualified. Lots of C++ textbooks do this, which is OK for small examples, but it's considered a very bad practice to do so, especially at global scope. The std namespace is very large, so there's the potential of a collision if you happen to choose a variable name that's also declared in the std namespace.

Here's an example I found that highlights this problem, showing code that will not compile.
C++:
#include <iostream>
using namespace std;

int count = 1;
int main() {
    cout << count << endl;
}
The symbol count, as declared, is ambiguous, since count is also a symbol declared in std., so in the output statement, the compiler can't determine which count is intended, the one at file scope here or the one in the std namespace. Two possible fixes:
1) move the definition of count inside main(), making it local to main()
2) use the scope operator, ::, in the output statement, like so.
C++:
 cout << ::count << endl;
 
  • #8
@yungman, this is a habit you need to break, possibly left over from writing Fortran code.
for (div = 1; div <= numDiv; div++)
C and C++ arrays are indexed starting from 0, as already mentioned.

This is what you should have had for both nested loops.
C++:
for (div = 0; div < numDiv; div++)
{
    for (qtr = 0; qtr < numQtr; qtr++)
 
  • Like
Likes yungman
  • #9
Mark44 said:
@yungman, this is a habit you need to break, possibly left over from writing Fortran code.
C and C++ arrays are indexed starting from 0, as already mentioned.

This is what you should have had for both nested loops.
C++:
for (div = 0; div < numDiv; div++)
{
    for (qtr = 0; qtr < numQtr; qtr++)
Yes, did I learn my lesson this time. Caused me over an hour!

I am glad this thread is open up again, I hate to keep starting new threads all the time. I don't want to be a pest here....At least not as obvious!o0)😊
 
  • #10
yungman said:
I am glad this thread is open up again, I hate to keep starting new threads all the time.
This is a different thread from your earlier one that went to almost 400 posts. For different questions, we prefer that they go in separate threads.
 
  • #11
Mark44 said:
This is a different thread from your earlier one that went to almost 400 posts. For different questions, we prefer that they go in separate threads.
Hopefully I won't have as many questions as before, a good book really helps, also, when I google now, some of them actually feels like it's written in English, not like before that they all looked Russian to me. It helps when I know more about C++. Also, I try to keep good notes, now I even put pages on the notes so I can go back to the book if there are still question.
 

Attachments

  • Gaddis notes.docx
    58.4 KB · Views: 148
  • #12
I have a very simple question. Does the source.cpp file work with other compilers? I know a lot of people here use VS, so we can post our .cpp here. How about someone with other compilers?
 
  • #13
yungman said:
Does the source.cpp file work with other compilers?
Are you asking about the file extension or the contents of the file?
If it's the extension, the gcc and g++ compilers work with that extension and a few others. If you're asking about the contents of the file, for what you're doing, pretty much any c++ compiler should accept it. However, there are some Microsoft-specific extensions that Visual Studio will accept that other compilers won't or probably won't accept, including such keywords as __int16, __int32, __asm, and others.
 
  • Like
Likes yungman

1. What does the error message "stack corruption" mean?

The error message "stack corruption" in C++ typically means that there is a problem with the program's memory allocation on the stack. This can occur when there is a mismatch between the amount of memory allocated for a function and the actual amount of data being used.

2. How does stack corruption happen?

Stack corruption can happen due to a variety of reasons, such as using uninitialized variables, accessing memory out of bounds, or not properly managing memory allocation and deallocation.

3. How can I fix a stack corruption error?

Fixing a stack corruption error usually involves carefully reviewing your code and identifying the source of the problem. This may involve checking for uninitialized variables, ensuring proper memory allocation and deallocation, or using debugging tools to identify the cause of the error.

4. Can stack corruption errors be prevented?

While it's impossible to completely prevent stack corruption errors, there are several best practices that can help reduce the likelihood of encountering them. This includes properly managing memory, avoiding uninitialized variables, and using debugging tools to catch errors early on.

5. Is stack corruption a serious issue?

Stack corruption can be a serious issue as it can lead to unexpected behavior or crashes in your program. It's important to address these errors as they can indicate larger problems with your code and can also impact the performance and reliability of your program.

Similar threads

  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
5
Views
884
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
23
Views
2K
Replies
10
Views
960
Back
Top