-1

The following source code compiles and runs fine

#include <cstdio>
#include <iostream>

using namespace std;

int main() {
    int t;
    cout << t << endl; // the seg fault / rte is for this
    cout << t+10 << endl;
    int mat[0];
    int mat2[t+10];
    for (int i = 0 ; i<100 ; i++) {
        //cout << mat[i] << endl;
    }

    cout << sizeof(mat)/sizeof(int) << endl;
    cout << sizeof(mat2)/sizeof(int) << endl;

    //cin >> t;


    return 0;
}

But if I uncomment the cin >> t it causes a Segmentation Fault.

#include <cstdio>
#include <iostream>

using namespace std;

int main() {
    int t;
    cout << t << endl; // the seg fault / rte is for this
    cout << t+10 << endl;
    int mat[0];
    int mat2[t+10];
    for (int i = 0 ; i<100 ; i++) {
        //cout << mat[i] << endl;
    }

    cout << sizeof(mat)/sizeof(int) << endl;
    cout << sizeof(mat2)/sizeof(int) << endl;

    cin >> t;


    return 0;
}

What is happening in it?

Unrealist
  • 525
  • 4
  • 14
  • **UB** using a unintialized variable (line 2 and 3 of the main), the second `cout` too. – NetVipeC Sep 16 '14 at 17:10
  • Compile with all warnings and debug info (e.g. `g++ -Wall -g`). Then **use a debugger** (`gdb`) – Basile Starynkevitch Sep 16 '14 at 17:11
  • Please note that array declarations without constant expressions is a GCC extension. You should use C++ container classes instead. Also, make sure you initialize variables. You also seem to think you have 100 elements in `mat`, when you declare it with 0. – crashmstr Sep 16 '14 at 17:12
  • I have intentionally done that since if I do not use the `cin` at the end the array is successfully initialized with `t`, I am trying to understand this behavior. Since this is not an isolated case. The same behavior was observed on a Windows PC. – Unrealist Sep 16 '14 at 17:15
  • 1
    For a more detailed explanation, see [this question](http://stackoverflow.com/questions/11962457/why-is-using-an-uninitialized-variable-undefined-behavior-in-c). – Beta Sep 16 '14 at 17:19
  • @Unrealist The undefined behavior (and the segfault) happens here (to be precise): `int mat2[t+10];` – πάντα ῥεῖ Sep 16 '14 at 17:26
  • 1
    @Beta, Thanks. A totally unknown face of this language has exposed itself with this to me :) – Unrealist Sep 16 '14 at 17:36

1 Answers1

1

I found the problem

#include 
#include 

using namespace std;

int main() {
    int t;
    cout << t << endl; // the seg fault / rte is for this
    cout << t+10 << endl;
    int mat[0];
    int mat2[t+10]; // <=== HERE IS THE PROBLEM
    for (int i = 0 ; i<100 ; i++) {
        //cout << mat[i] << endl;
    }

    cout << sizeof(mat)/sizeof(int) << endl;
    cout << sizeof(mat2)/sizeof(int) << endl;

    cin >> t;


    return 0;
}

You are creating an array of variable size, there is no problem with this, but you are defining the size based on an uninitialized variable. Uninitialized local variables on C/C++, has NO default value, it can have ANY value it get from stack garbage

To test this, I compiled the program with strange (but possible) values for t to get Segementation Faults:

int main() {
    int t = -22;
    ...
    int mat[0];
    int mat2[t+10];
int main() {
    int t = 0xdeadbeef;
    ...
    int mat[0];
    int mat2[t+10];

You can get any of the values, and it would fail at random, my be using cin >> t changes certain variables on compiler optimization leading to change stack values or something like that, but it is totally unpredictable you can consider it almost random

dseminara
  • 11,665
  • 2
  • 20
  • 22
  • I'm pretty sure you did not understand the core question. I know it's Undefined behavior and all of that you said is true. But the original question has 2 code samples, the first of which runs without a Segmentation Fault (on multiple environments) while the other does not. Usually problems caused by uninitialized variables persists through the code but in this case the problems seemed to disappear when a certain section of code was missing. Anyway, I found the possible cause here http://stackoverflow.com/questions/11962457/why-is-using-an-uninitialized-variable-undefined-behavior-in-c – Unrealist Sep 16 '14 at 18:37