1

I don't know how to ask this question as it was little confusing to me. i was having a problem with this code

#include <stdio.h>
#include <stdlib.h>

#define ull unsigned long long

#define SIZE 1000000001

#define min(a,b) ((a<b?a:b))
#define max(a,b) ((a>b?a:b))

int solve(void) {

//    unsigned *count = malloc(sizeof(unsigned) * SIZE);
    int k;
    scanf("%d", &k);
    unsigned count[SIZE];
    for (ull i = 0; i < SIZE; i++){
        count[i] = 0;
    }

    return 0;
}

int main(void){
    unsigned t;
    if (scanf("%u", &t) != 1) return 1;
    while (t-- > 0){
        if (solve() != 0) return 1;
    }
}

This code for me is giving segfault.

What my observation is

  1. it is running fine until it is in solve function.
  2. on calling solve function it is giving segfault.
  3. It has nothing to do with scanf("%d", &k) as by removing this line gives the same error
  4. But if we decrease the SIZE value it will run fine.
  5. Other thing which i can do is instead of creating an array on stack i can use heap and this is working fine.
  6. If i only declare array count in solve function instead of taking k as input and initializing all the values of array count to 0. i am not getting any segfault

So i have some questions regarding this.

  • Is this due to memory limitation to array or because of memory limitation for a stack frame for the function solve (or possibly another reason which i can't find).
  • If this is due to any kind of memory limitation than isn't it is too low for a program?
  • How compiler checks for such errors as adding any kind of print statement won't run before array declaration as i am getting segfault when program reaches solve. So compiler somehow knows that their is a problem with code without even getting there.
  • and specifically for the 6th point, as per my knowledge when declaring array it reserves memory for the array. So by initializing it i am doing nothing which will increase the size of array. So why i am not getting any kind of error when declaring array while i am getting segfault when i am initializing all those values in array

Maybe i am seeing it in totally wrong way but this is how i think it is, So please if you know any reason for this please answer me about that too

tycoon
  • 121
  • 9
  • 5
    Assuming available 4GB of memory for local variables does seem quite optimistic. – Yunnosch Apr 21 '22 at 05:17
  • 1
    The stack is rather limited. On Windows the default stack size per process is only a single MiB, while on Linux it's 8 MiB. If you need large amounts of data, allocate dynamically. Of use different data-structures. – Some programmer dude Apr 21 '22 at 05:18
  • @Yunnosch i completely ignored that i was using 1000 mbs just for an array. :-) – tycoon Apr 21 '22 at 05:20
  • @Someprogrammerdude So if i am using more memory than i should i will get SEGFAULT? – tycoon Apr 21 '22 at 05:21
  • 1
    on linux, you can find your stack size with [`ulimit -a`](https://stackoverflow.com/questions/7535994/how-do-i-find-the-maximum-stack-size). Actually, `ulimit -s` displays only the stack size for me on my Fedora box, which is 8MB. – yano Apr 21 '22 at 05:21
  • 2
    On another note, you seem to be coding for some kind of so-called "competition" or "online judge" site. Don't use such for any kid of learning or teaching, that's not their purpose. They don't teach you programming languages properly, they don't teach any kind of computer science or the math behind it, they don't teach any other useful techniques that are mandatory knowledge for programmers to be successful. All they teach as bad (and sometimes even invalid) code, and how to be "clever" (which is almost never a good trait for writing good programs). Get books, take classes, stay in school. – Some programmer dude Apr 21 '22 at 05:22
  • 1
    @yano for me stack size is 8192. thanks for helping – tycoon Apr 21 '22 at 05:22
  • 2
    You will get undefined behavior. Which might lead to crashes, or might lead to your house catching fire, or might seem to work just fine (only to break when you least expect it). – Some programmer dude Apr 21 '22 at 05:24
  • @Someprogrammerdude i know that these kind of coding competitions are problematic as i posted on one of my questions someone told me i was writing very vulnerable c code, but eveyone was writing similar code. So competetive programming doesn't teach you good programming but i just use it to make myself comfortable with programming because i don't know what else can make me comfortable with programming – tycoon Apr 21 '22 at 05:26

1 Answers1

4

It depends on your operating system. On Windows, the typical maximum size for a stack is 1MB, whereas it is 8MB on a typical modern Linux, although those values are adjustable in various ways. For me it's working properly check with other platform or other system.