简体   繁体   中英

Array with pointers to objects initialization

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM