0

I am currently working in C++ and I face this challenge. Here is a code of my class in the header file:

class PID
{
   private:
   int PID;
   int total;

   public:
   PID(); // Constructor
   int returnPID(); // Returns PID.
};

Here is the code declaration in cpp file:

PID::PID()
{
    PID=INT_MAX;
    total=0;
}

int PID::returnPID()
{
    return PID;
}

And here is the declaration and the initialization in main of a table contaning pointers to objects of the class PID:

PID* table[1000000];

for (int i=0; i<1000000; i++)
{
    table[i]=new PID;
}

So I suppose this uses the constructor I have created above to set the PID to MAX_INT. When I try to access the content of table[i].PID using returnPID within the initialization for everything works great, something like this:

for (int i=0; i<1000000; i++)
{
    table[i]=new PID;
    int display=table[i]->returnPID();
    cout<<display<<endl;
}

The problem occurs when I am trying to access table[i] contents outside and after the initialization for. My main crashes and it returns a number (-1073741571) as an error. It seems like not even one command from the main is executed. Here is a sample of the code that seems to reproduce the problem:

for (int i=0; i<1000000; i++)
{
    table[i]=new PID;
}
for (int i=0; i<1000000; i++)
{
    int display=table[i]->returnPID();
    cout<<display<<endl;
}

I have been working on this for more than two hours without coming to any solution and it just doesn't seem logical. Anyone have any explanation for this?

EDIT: Any table with less than 1.000.000 spots will work correctly. Maybe it has something to do with this although I still don't see the connection.

7
  • 2
    "My main crashes for no reason" - Doubtful. I'm inclined to believe the OS. Commented Nov 5, 2012 at 0:43
  • That was a bad statement of me. There must be a reason and actually this is the reason I am looking for. Any ideas? Commented Nov 5, 2012 at 0:54
  • 1
    error: field ‘int PID::PID’ with same name as class...avoid it.... Commented Nov 5, 2012 at 0:55
  • @noleptr it is a convention to use the name of the class for the class constructor & destructor. You are the first guy to tell me something like this. Any tips on why I should avoid this? Commented Nov 5, 2012 at 0:57
  • 1
    @Aposperite.... I am talking about the PID private variable....and that by the way was my gcc compiler...I was trying to see whats wrong with your code.... Commented Nov 5, 2012 at 0:59

2 Answers 2

3

Anyone have any explanation for this?

It seems like you're running out of stack space.

Can your compiler handle a million integers, instead of a million PID*?

Any table with less than 1.000.000 spots will work correctly. Maybe it has something to do with this although I still don't see the connection.

It has everything to do with that.

I tried this:

int main(){
    int bec[10000000];
    for (int i=0; i<10000000;i++){
        bec[i] = i;
    }
    printf("%d\n",rand()%1000);
    return 0;

}

It segfaults for the same reason as yours.

The only way to solve this problem is to use less stack space. You can declare bec outside of main and not use stack space for that or you can use std::vector. You have plenty of options.

Sign up to request clarification or add additional context in comments.

3 Comments

You are right, I can confirm I got the same error. So that means it's a compiler error?
Nope, it is your error. You can declare bec outside of main and not use stack space or you can use std::vector. You have plenty of options.
Thank you very much for your guidance. I never thought about stack size in the first place. Thank you again. If you may add this comment to your main answer for feature viewers I will accept your answer. Thank you once again.
0
PID* table[1000000];

There's your problem. That's an automatically-allocated array of 1,000,000 pointers, or [up to] eight million bytes. Stack space is often fairly limited and you're using a lot of it. When I say "limited", I mean like 8KB, not 8MB.

When you go over this, often the results are not pretty.

8MB is a lot for automatic allocation (what you may call "on the stack"); so, look into adjusting your storage mechanism, or consider using dynamic allocation — how about a nice std::vector?

BTW.. having a member variable with the same name as the class it's in is silly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.