0

I've made the code above and I've used 2 different compilers, one says "Segmentation Fault" and the other one says "handle_exceptions: Exception: STATUS_STACK_OVERFLOW". Here is the code.

int main() {
    int k, x, f, i, j, tmp, ct;
    scanf("%d %d", &k, &x);
    int w[k];
    for( f = 0; f<k; f++){
        scanf(" %d", &w[f]);
    }
    while(x--){
        scanf("%d %d", &i, &j);
        tmp = 3;
        for(ct = j; i<=ct<=j; ct--){
            if(tmp > w[ct]){
                tmp = w[ct];
            }
        }
        printf("%d\n", tmp);
    }
    return 0;
}

I've commented this code, line by line, and testing and I've the error here:

int w[k];

I don't know what I can do, because the k variable is already defined when the declaration of this vector happens. Can somebody help me?

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
DMaxter
  • 178
  • 5
  • 19
  • 2
    if (k > 9000000) perror("I'm sorry Dave, I'm afraid I can't do that"); – Hans Passant Sep 15 '15 at 23:05
  • 3
    What is the value of `k`? That is, what is your input? The stack size is limited so a large value of `k` will overflow the stack. Also, should check the return value of `scanf` before using `k`. And finally, probably better to use dynamic memory rather than allocate on the stack when allocating memory based on user input. – kaylum Sep 15 '15 at 23:06
  • 1
    `for(ct = j; i<=ct<=j; ct--){` just doesn't look good. – wildplasser Sep 15 '15 at 23:11
  • Yes, this probably has your `ct` variable going negative. You should learn how to use a debugger or some tool like valgrind. – Jens Gustedt Sep 15 '15 at 23:41
  • @wildplasser but it is for a challenge and decreasing integers is faster than increasing integers. – DMaxter Sep 17 '15 at 17:23

2 Answers2

3

If you defined an array like this:

int w[k];

It will be stored in the stack, which has a limited size.

If you initialize with malloc the data will be store in the heap (which also has a limited size, but it will allow you to store more data)

int *w = malloc(sizeof(int) * k);

Have you tried this?

Check this for a difference between stack vs heap.

Always checked the returned value from malloc(), this is just an example, but it is a bad practice to not the returned value of malloc().

Community
  • 1
  • 1
Cacho Santa
  • 6,846
  • 6
  • 41
  • 73
  • Do not cast `void *` as returned by `malloc` & friends in C! And `malloc` does not initialize anything. That would be `calloc`. Also, these are different semantics: try `sizeof(w)` for both. – too honest for this site Sep 16 '15 at 01:37
  • @Olaf what is the issue of casting what malloc is returning? it's part of the documentation: http://www.cplusplus.com/reference/cstdlib/malloc/ – Cacho Santa Sep 16 '15 at 02:02
  • @cacho C is not c++. If you get errors and you need to cast malloc in C, well probably because you are using a c++ compiler. – Michi Sep 16 '15 at 02:46
  • @cacho: As Michi stated. C and C++ are different languages. (You do not think C# is the same either, don't you?). Do not use C++ resources to learn C programming! Perhaps a good C book and a good C++ book might give you hints. – too honest for this site Sep 16 '15 at 11:00
  • that link is from the C LIBRARY, and they DO cast the malloc return value. You don't think that because the domain name is "cplusplus.com" then all of its content MUST be C++ right? @Olaf – Cacho Santa Sep 16 '15 at 17:06
  • @cacho: Point is, there will be no warning if you do, because you are expected to know what you do in C. Heck, a cast does actually **instruct** the compiler to shut up. I will not discuss this further, there are enough answers here already about this subject. Just as you post refer to **some** C library, that is not authoritative. The only such source for C does [**not** cast](http://port70.net/~nsz/c/c11/n1570.html#6.7.2.1p23) (just search the text for `malloc`, there are many other examples)! – too honest for this site Sep 16 '15 at 17:54
  • Kernighan and Ritchie, who wrote C are casting from malloc (http://www.iups.org/media/meeting_minutes/C.pdf) @Olaf – Cacho Santa Sep 16 '15 at 17:57
  • @cacho: You might have noticed C has evolved quite much since K&R C. In K&R you did not have prototypes and _more important_ `malloc` actually returned `char *`, not `void *`, so you **did** have to cast! Please read the normative reference, not some ancient books or C++ references. – too honest for this site Sep 16 '15 at 17:59
  • 1
    @Olaf The opinion of stackoverflow users is not authoritative either, no matter how high their 'reputation' is. Casting it is legal C code. That's a fact. Everything else is opinion, which by definition cannot be proven wrong or right. – John Hammond Sep 16 '15 at 18:13
  • @LarsFriedrich: Where did I mention reputation here? Did you even read the standard or at least the paragraphs I linked? However, If any of my students does add an unnecessary cast, he will get a malus. You seem to hate your compiler as you keep it from helping typechecking your source. No wonder C has such a bad reputation for some (sad enogh: many of them use it exactly as you propose: as some kind of assembler). There is, however, no use in trying to talk someone out of ignorance, so I leave here. – too honest for this site Sep 16 '15 at 18:31
  • @Olaf Feel free to assign grades to your students by checking the size of the source code file. The statement that a non-existent cast helps the compiler in type-checking is ridiculous. There is a difference in `x = 4 * sizeof(int) + 8 * sizeof(int);` versus `x = 12 * sizeof(int);`. Not for the compiler or the result. But for the human. And it is the human error you need to eliminate, his errors about the code, the intention, the semantics - not optimize code by file size. And that's not called ignorance, but experience. – John Hammond Sep 16 '15 at 18:51
  • @LarsFriedrich: Last posting: Please discuss this with the standard commitee members. They apparently share my position (or I do share their position), but apparently you refuse to read the links I posted. Read what the word "malus" means before talking nonsense. And there ist nothing gained, but much to be lost if you do cast if you forget to `#include , at least for pre-C99 (which enforces prototypes). – too honest for this site Sep 16 '15 at 19:01
  • @cacho what should I modify in this code? I don't know how to do it. I've studied pointers but I don't understand how can I make it. – DMaxter Sep 16 '15 at 19:14
-2

You cannot define a variable size during run-time unless you use dynamic memory allocation.

As I can see, you are trying to set the variable size based on the user's input.

To do this, you must use dynamic memory allocation.

scanf("%d %d", &k, &x);
if(k > MAX_SIZE) {
    return -1;
}
int *w = malloc(sizeof(int) * k);

Now you have a memory allocated based on the size entered by user's input. But make sure that you are imposing some limits, because the heap memory is limited.

campescassiano
  • 809
  • 5
  • 18