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

  • Context: C/C++ 
  • Thread starter Thread starter yungman
  • Start date Start date
  • Tags Tags
    C++ Error
Click For Summary

Discussion Overview

The discussion revolves around a C++ error message related to stack corruption in a program that uses a two-dimensional array to store sales data. Participants explore the causes of the error, the implications of array indexing, and the use of namespaces in C++. The conversation includes technical explanations, personal experiences, and inquiries about compiler compatibility.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Meta-discussion

Main Points Raised

  • One participant reports a stack corruption error when accessing a two-dimensional array and seeks help in understanding the issue.
  • Another participant explains that C++ arrays are zero-indexed, suggesting that the error arises from indexing the array starting at 1 instead of 0.
  • A participant confirms that changing the loop indices to start at 0 resolves the issue.
  • Discussion includes a comment on the use of the 'count' identifier, which may conflict with the standard library due to the use of 'using namespace std;'.
  • Another participant provides an example of potential ambiguity caused by naming conflicts in the standard namespace.
  • There is a suggestion that the original poster may have habits from programming in Fortran, which uses 1-based indexing.
  • Participants share personal experiences with programming and the challenges of learning C++, including the usefulness of good reference materials.
  • A question is raised about the compatibility of the provided C++ code with different compilers, leading to a discussion about file extensions and specific compiler features.

Areas of Agreement / Disagreement

Participants generally agree on the issue of array indexing and the potential for naming conflicts with the standard library. However, there are varying opinions on the use of namespaces and the implications of programming habits from other languages. The discussion about compiler compatibility remains open-ended.

Contextual Notes

Participants note that the original code contains potential issues with variable naming and array indexing that could lead to runtime errors. The discussion highlights the importance of understanding compiler-specific features and the standard library in C++.

Who May Find This Useful

This discussion may be useful for C++ programmers, especially those transitioning from other programming languages, as well as those interested in understanding common pitfalls related to array usage and compiler compatibility.

yungman
Messages
5,741
Reaction score
291
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;

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

    }

    for (div = 1; div <= numDiv; div++)
    {
        for (qtr = 1; qtr <= numQtr; qtr++)
        {
            totalSales += sales[div][qtr];
        }
    }
    count << " 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   Reactions: 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   Reactions: 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 count 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() {
    count << 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++:
 count << ::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   Reactions: 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   Reactions: yungman

Similar threads

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