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

9
  • 5
    Assuming available 4GB of memory for local variables does seem quite optimistic. Commented Apr 21, 2022 at 5: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. Commented Apr 21, 2022 at 5:18
  • 2
    on linux, you can find your stack size with ulimit -a. Actually, ulimit -s displays only the stack size for me on my Fedora box, which is 8MB. Commented Apr 21, 2022 at 5: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. Commented Apr 21, 2022 at 5: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). Commented Apr 21, 2022 at 5:24

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.