Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8798501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:06:42+00:00 2026-06-14T00:06:42+00:00

It seems that the recommended way to set stack size for a C program

  • 0

It seems that the recommended way to set stack size for a C program or Ruby program (which uses the C stack), is by using ulimit in the Bash shell. But

$ ulimit -s
8192

$ ulimit -s 16384
-bash: ulimit: stack size: cannot modify limit: Operation not permitted

and sudo doesn’t help either. Is there a way to set it to 16MB, 32MB, or 64MB? I thought there should be a way to set it per program invocation instead of setting a system wide parameter as well?

Right now 8192 probably means 8MB which is quite small, if that is compared to how much a process can be using, sometimes as much as 2GB of RAM.

(updated note: ulimit -a can show its current values).

(update 2: it actually seems like ulimit -s <value> is per shell, and that if you set it the first time, it usually works. The problem is when you set it the second time, then it may return an error)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:06:43+00:00Added an answer on June 14, 2026 at 12:06 am

    Apparently there is a hard limit on the stack size for mac os x, taken from http://lists.apple.com/archives/scitech/2004/Oct/msg00124.html granted this is quite old, and Im not sure if its still true anymore, but to set it simply call ulimit -s hard, its 65532. or about 65 megs.

    I did some tests on snow leopard, 10.6.8, and it does seem to be true.

    $ ulimit -a
    ...
    stack size              (kbytes, -s) 8192
    ...
    $ ulimit -s 65533
    -bash: ulimit: stack size: cannot modify limit: Operation not permitted
    $ ulimit -s 65532
    $
    

    I also found this http://linuxtoosx.blogspot.com/2010/10/stack-overflow-increasing-stack-limit.html though I haven’t test it, so can’t really say much about it.

    When applications consume gigs of memory thats usually taken from the heap, the stack is usually reserve for local automatic variables that exist for a relatively small amount of time equivalent to the lifespan of the function call, the heap is where most of the persistent data lives.

    here is a quick tutorial:

    #include <stdlib.h>
    
    #define NUMBER_OF_BYTES 10000000 // about 10 megs
    void test()
    {
       char stack_data[NUMBER_OF_BYTES];          // allocating on the stack.
       char *heap_data = malloc(NUMBER_OF_BYTES); // pointer (heap_data) lives on the stack, the actual data lives on the heap.
    }
    
    int main()
    {   
        test(); 
        // at this point stack_data[NUMBER_OF_BYTES] and *heap_data have being removed, but malloc(NUMBER_OF_BYTES) persists.
        // depending on the calling convention either main or test are responssible for resetting the stack.
        // on most compilers including gcc, the caller (main) is responssible.
    
        return 0;
    }
    
    $ ulimit -a
    ...
    stack size              (kbytes, -s) 8192
    ...
    $ gcc m.c
    $ ./a.out
    Segmentation fault
    $ ulimit -s hard
    $ ./a.out
    $
    

    ulimit is only temporary you would have to update it every time, or update your corresponding bash script to set it automatically.

    Once ulimit is set it can only be lowered never raised.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to set up virtualenv with numpy. I've found that the recommended way
What is the recommended way of allowing an ASP.NET application to know which environment
I have an application that uses disabled JTextFields in several places which are intended
It seems that jQuery.browser is deprecated in the latest jQuery. The doc recommends that
Seems that This will be an easy question for you but this problem is
Seems that even after unchecking the option in the PyDev/Debug preferenecs pane to launch
It seems that we can show layers and even use a different zPosition for
It seems that runClasses() doesn't terminate the code being tested even after the test
It seems that JavaScript's Date() function can only return local date and time. Is
It seems that often I find that my code when moving either from one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.