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

  • Thread starter yungman
  • Start date
  • #1
4,743
102

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:

Answers and Replies

  • #2
12,019
5,671
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
4,743
102
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
116
64
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
strangerep
Science Advisor
3,156
972
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
116
64
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
33,955
5,611
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
33,955
5,611
@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
4,743
102
@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
33,955
5,611
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
4,743
102
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

  • #12
4,743
102
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
33,955
5,611
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

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

Replies
8
Views
2K
Replies
5
Views
999
Replies
1
Views
2K
Replies
1
Views
3K
Replies
16
Views
233
Replies
3
Views
9K
Replies
3
Views
677
Replies
11
Views
2K
Replies
1
Views
1K
Top