public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Miguel A. Nuñez" <miguel.nunez@terra.es>
To: Bob Rossi <bob@brasko.net>
Cc: Ian Lance Taylor <ian@airs.com>,
	Lexington Luthor <lexington.luthor@gmail.com>,
	 <gcc-help@gcc.gnu.org>
Subject: Re[2]: maximum number of variables?
Date: Thu, 01 Sep 2005 08:20:00 -0000	[thread overview]
Message-ID: <198603206671.20050901102029@terra.es> (raw)
In-Reply-To: <20050831111723.GA5325@white>

>> 
>> 
>> ILT> Bob Rossi <bob@brasko.net> writes:
>> 
>> >> On Tue, Aug 30, 2005 at 10:54:02PM +0100, Lexington Luthor wrote:
>> >> 
>> >> > I think for large arrays, its best to allocate them from the heap. Use
>> >> > malloc() or new (if using C++).
>> >> 
>> >> I've always been told that the heap and stack grow towards each other.
>> >> If this is true, why would it be OK to create the item on the heap, vs
>> >> on the stack? If it's not true, could someone simply explain how this
>> >> works?
>> 
>> ILT> You are correct in theory.  In practice the heap and stack have
>> ILT> different limits, and the limit on the heap is much larger than the
>> ILT> stack (if running bash, compare ulimit -s and ulimit -v).  And if you
>> ILT> worry about portability, on some platforms allocating a large stack
>> ILT> frame will simply fail, and on some other platforms it will require
>> ILT> extra work to emit stack probes to tell the OS that you are
>> ILT> intentionally extending the stack rather than just referencing a
>> ILT> random memory address.
>> 
>> ILT> Ian
>> 
>> 
>> Ian,
>> 
>>   I did ulimit -s and ulimit -v in a solaris 9 sun machine.
>> ulimit -s returns 8192, ok, but ulimit -s returns unlimited. Does
>> this make sense?

BR> Assumming you mean ulimit -s = 8192 and ulimit -v  = unlimited, then
BR> yes, I suppose that makes sense. On my linux machine, ulimit -s and
BR> ulimit -v = unlimited. In this case, I suppose it's possible that if the
BR> array was on the stack and things borked, it would also bork when on the
BR> heap. However, in your case, I know understand why things would work for
BR> you if you moved from the stack to the heap.

BR> Bob Rossi


Yes, I meant that Bob. Anyway, remember it was Nicolas who originally had this problem. I'm running into a similar problem, so I tried the commands Ian suggested, and the value 'unlimited' was strange to me.

Let's see if anyone can help me with my problem. My application creates a new thread for a receiving infite loop. pthread_create runs the function containing the loop, and this functions defines a local variable that is a huge struct (sizeof returns 984KB). I initialize it using 'mystruct_t mystruct={0}'.

Up to this point everything is ok. When something is received, the loop calls a new function. This function creates a new local variable, the same struct. Here the program crashes.

I have read the default stack size when creating a thread in solaris 32 bits is 1MB, so I thought the crash would be due to allocating more memory than the stack size.

I put the initialization in a separate line, with a for loop giving value 0 byte per byte, from 0 to sizeof(mystruct_t)-1. Strangely (at least to me) the second struct is allocated, and the crash is always produced when writing to the bytes of the struct.

Is it possible that the local variable is allocated partly into the stack and partly beyond the limits of the stack, so that when I write to it I get my segmentation fault?? How could I know where the stack begins and ends?

I will allocate the structs at the heap, instead of the stack, but I would like to understand this situation.




  reply	other threads:[~2005-09-01  8:20 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-08-30 21:01 Nicolas Ouellette
2005-08-30 21:08 ` Bob Rossi
2005-08-30 21:41   ` Nicolas Ouellette
2005-08-30 22:13     ` Lexington Luthor
2005-08-31  1:01       ` Bob Rossi
2005-08-31  4:20         ` Ian Lance Taylor
2005-08-31  9:13           ` Re[2]: " Miguel A. Nuñez
2005-08-31 11:17             ` Bob Rossi
2005-09-01  8:20               ` Miguel A. Nuñez [this message]
2005-08-31 11:19           ` Bob Rossi
2005-08-30 23:01 ` corey taylor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=198603206671.20050901102029@terra.es \
    --to=miguel.nunez@terra.es \
    --cc=bob@brasko.net \
    --cc=gcc-help@gcc.gnu.org \
    --cc=ian@airs.com \
    --cc=lexington.luthor@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).