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

  • Thread starter Thread starter yungman
  • Start date Start date
  • Tags Tags
    C++ Error
Click For Summary
The discussion centers on a C++ program encountering a stack corruption error due to incorrect array indexing. The user mistakenly indexed a 2D array starting from 1 instead of 0, leading to out-of-bounds access. Participants clarify that C++ arrays are zero-indexed and suggest correcting the loop indices to avoid such errors. Additionally, there are comments on the use of the `using namespace std;` directive, highlighting potential naming conflicts. The conversation concludes with a note on the compatibility of C++ source files across different compilers, emphasizing that most should accept standard C++ code.
yungman
Messages
5,741
Reaction score
294
TL;DR
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
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
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
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.
 
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.
 
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.
 
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;
 
@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
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

  • #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

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
Replies
20
Views
2K
  • · Replies 39 ·
2
Replies
39
Views
5K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 19 ·
Replies
19
Views
2K
Replies
5
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
3K