-1

If I have the following code, could a segmentation error ever be caused?

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

int main() {
    int i;
    int n = 30;

    while(i < n) {
            printf("%d ", i);
            if(i % 3 == 0) {  
            n--;
            } else {
            n = n -2;
            }
   i = i + 2;

    }
return 0;
}

When I ran it, I don't get any segmentation errors but I also don't get any output. And are we always to assume that integer i could be any number in memory? I understand that it will not be initialized to 0, correct?

Vimzy
  • 1,871
  • 8
  • 30
  • 56
  • Why don't you print `i` before the loop.. – Thiyagu Feb 17 '15 at 04:11
  • Where you have initialized `i` before `while` loop? write `i=0;` before `while(i – Himanshu Feb 17 '15 at 04:14
  • You aren't getting any output because output is generally line buffered, and your program doesn't print any newline characters. So you won't get any output until the program exits. To see your output, use `fflush(stdout)` after each `printf`. – JS1 Feb 17 '15 at 04:19

2 Answers2

2

Yes for local variable (which is allocated in the stack, the value is undefined) See answer: What happens to a declared, uninitialized variable in C? Does it have a value?

if "int i" was declared outside the main (as a global or static variable, it would be zero and your output would be as follows "0 2 4 6 8 10 12 14 16"

So this code is unlikely to cause seg fault, though compilers can chose to crash. But you are not really accessing out of bounds memory, just some value on the stack. In the case i was a pointer, and you tried to dereference it, then it most likely could cause a seg fault.

Community
  • 1
  • 1
  • So that's what I'm asking, can this ever cause a segmentation fault error? I don't think it will because the i will always contain some integer value, no? Or can it contain "other" garbage stuff as well? – Vimzy Feb 17 '15 at 04:51
  • Well, according to the C standard, and all questions will ultimately have to boil down to the standard, the behavior is undefined. That means yes it is possible your program can crash and result in seg fault (though like i said most compilers and comp architecture you won't have a problem). It would have been ok only if i was of type "unsigned char" I encourage you to go through the part of the standard 6.2.6.1: http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf – embeddedninja Feb 17 '15 at 05:20
1

When I ran it, I don't get any segmentation errors but I also don't get any output. And are we always to assume that integer i could be any number in memory during that time?

The value of i is indeterminate. So using uninitialized variables leads to undefined behavior.So there is a possibility of a crash because of undefined behavior. Yes the value of i can be anything

I understand that it will not be initialized to 0, correct?

Yes an unintialized variable is a unintialized one it doesn't have any assured value until you set it explicitly. (There are exceptions like global/static which are intilialized to 0)

The below link might help: (Why) is using an uninitialized variable undefined behavior?

Community
  • 1
  • 1
Gopi
  • 19,784
  • 4
  • 24
  • 36