1,105,169 Community Members

Segmentation Fault

Member Avatar
ricky_125
Newbie Poster
3 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello,

Any ideas why this is giving a segmentation fault?:

#include <iostream>
#include <new>

using namespace std;

struct List {

int A;
int B;
List *next;

};


int main(void) {

int llength;

llength=1111111111;

List *L;

L= new List[llength];

L[37370197].B=-37370197;

delete [] L;

return 0;

}


Cheers

Member Avatar
arkoenig
Practically a Master Poster
691 posts since Jun 2010
Reputation Points: 340 [?]
Q&As Helped to Solve: 110 [?]
Skill Endorsements: 9 [?]
 
1
 

Yes.

More seriously, if you're running this program on a 32-bit machine, you're probably asking for much more memory than the system is capable of providing; and it would not be surprising to learn that an overflow is taking place as part of computing how much memory you need.

Member Avatar
ricky_125
Newbie Poster
3 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I thought it should crash out with a bad_alloc exception though?

Member Avatar
arkoenig
Practically a Master Poster
691 posts since Jun 2010
Reputation Points: 340 [?]
Q&As Helped to Solve: 110 [?]
Skill Endorsements: 9 [?]
 
0
 

In a perfect world, it would. In practice, it is not uncommon for implementations to fail to catch integer overflow.

Member Avatar
ricky_125
Newbie Poster
3 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

So, am I right in saying that if the g++ compiler had the correct implementation, then the above would through a bad_alloc exception instead of a segmentation fault? (and if so, is this something which is likely to be fixed on future releases of the g++ compiler?)

Cheers

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: