How to increase limit of array?

本秂侑毒 提交于 2019-12-13 07:52:49

问题


// C Program to find average of numbers given by user
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
    double sum = 0;
    int ii = 0,c;
    char buf[256], *token;
    printf("Enter the numbers to average on a single line, separated by space and press enter when done\n");
    fgets(buf, 255, stdin);
    token = strtok(buf, " ");
    while (token != NULL)
    {
        sum += atof(token);
        ii++;
        token = strtok(NULL, " ");  //Get next number
    }
    printf("Average is %lf", sum / (double)ii);
}

On line 8:

char buf[256], *token;

when I change the array limit to any 8 or more digit numbers like 11111111, 68297907 (and so on...) then the program gets complied but on output it shows Segmention Error.

How can I increase array limit? I am using a UNIX based system.


回答1:


char buf[11111111];

This is over 11 megabytes. It's allocated on the stack. The stack has a finite size, often 8 or 10 megabytes. You're getting a stack overflow, which normally results in a segfault if you exceed that limit.

You can:

  • Increase the stack limit, if your system supports it. You don't tell us what kind of system you're on. This is normally done through the shell. For bash, run e.g.

    ulimit -s 12000
    

    to set the max stack size to 12000 kilobytes (120 megabytes). There might be a limit set by an administrator that prevents you from using this much stack space. You must run your program in the same shell as you ran the above ulimit command.

  • Dynamically allocate the memory:

    char *buf = malloc(11111111);
    
  • Allocate the space somewhere else besides the stack:

    static char buf[11111111];
    

I would question the need for allowing someone to input 11 megabytes of data on one line though.




回答2:


You probably need to increase the stack size allowed:

http://www.ss64.com/bash/ulimit.html

or you could try allocating memory dynamically rather than on the stack using malloc:

char *buf = malloc(A_BIG_NUM);



回答3:


Stack size on *nix is usually set at 8MB by default. A char array of 11111111 elements is ~11MB, which is much larger than the stack. As a result it'll cause a stack overflow

You can increase stack size with ulimit, but increasing it too much is also not good. For big arrays you should use heap allocation instead




回答4:


Be reasonable. The array buf is used to read a number in textual form. Nobody needs to enter 1 billion digit numbers (except those trying buffer overflow attacks), no known C implementation supports exponents or mantissas for 1 billion digit float or double numbers.

The excessive and unnecessary array sizes exceed the implementation limit for stack sizes and result in a segmentation violation in your case.




回答5:


Everything has been written above. Program gets signal SIGSEGV when starting because of overcoming of allowed stack size.
Below is just one another way to determine the actual stack size (both soft and hard limits):

#include <stdio.h>
#include <sys/time.h>
#include <sys/resource.h>
void main() {
  struct rlimit rl;
  int ss = getrlimit(RLIMIT_STACK, &rl);
  printf("soft = %lu, hard = %lu\n", rl.rlim_cur, rl.rlim_max);
}


来源:https://stackoverflow.com/questions/19269985/how-to-increase-limit-of-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!