FAQ  •  Login

General question

Moderators: Dan Negrut, ME964 Spring 2012

<<

S12pyadav

Newbie
Newbie

Posts: 30

Joined: Mon Jan 23, 2012 9:07 pm

Unread post Sat Apr 14, 2012 9:00 pm

General question

I got an error message stating segmentation fault when I tried to pre allocate a character array of 2^30 elements, but the same thing worked fine when I used a pointer and malloc instead. Any idea why one works as opposed to other?
<<

S12zguo

Jr. Member
Jr. Member

Posts: 63

Joined: Mon Jan 23, 2012 9:07 pm

Unread post Sat Apr 14, 2012 10:00 pm

Re: General question

If you tried to do this in the body of a function, you can't, since the default stack of a function is only 4MB or something, I believe, and that would not be enough space to hold such a large array. If this was a global variable, then I'm not so sure. Don't recall if there's a limit to how big the .data section can be.

Edit: Default stack size can be variable between different OSes or even different Linux distros. I kinda pulled the 4MB number randomly, but it's not going to be significantly higher than that, certainly not enough to hold that big an array.
<<

Dan Negrut

Global Moderator
Global Moderator

Posts: 833

Joined: Wed Sep 03, 2008 12:24 pm

Unread post Sun Apr 15, 2012 9:47 pm

Re: General question

It would be helpful to see the piece of code that does what you call "pre allocate".
Dan
<<

S12pyadav

Newbie
Newbie

Posts: 30

Joined: Mon Jan 23, 2012 9:07 pm

Unread post Mon Apr 16, 2012 4:46 pm

Re: General question

When I pre allocated message pointer using the following it throws an error of seg fault
  Code:
 int memSize = pow(2,30); char message[memSize];


where as
  Code:
 char *message = (char*)malloc(memSize*size of(char));


works just fine.

-Praveen
<<

S12pyadav

Newbie
Newbie

Posts: 30

Joined: Mon Jan 23, 2012 9:07 pm

Unread post Mon Apr 16, 2012 4:52 pm

Re: General question

never mind
<<

S12zguo

Jr. Member
Jr. Member

Posts: 63

Joined: Mon Jan 23, 2012 9:07 pm

Unread post Mon Apr 16, 2012 8:59 pm

Re: General question

Uh, you can't do that. You can't attempt to use a dynamic variable to indicate the size of a static array, even ignoring that that array would not fit on the stack. Though I presume your never mind means you figured that out.
<<

S12pyadav

Newbie
Newbie

Posts: 30

Joined: Mon Jan 23, 2012 9:07 pm

Unread post Wed Apr 18, 2012 10:39 pm

Re: General question

Oh yes, thank you for the help though.
At the risk of sounding a complete idiot, I should say this made me realize the true nature of stack and heap.

Return to ME964 Spring 2012: High Performance Computing

Who is online

Users browsing this forum: No registered users and 1 guest

cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software.