0

I try to understand pointers. Question is: should'nt I get a segmentation fault when compiling the second for loop? If no why not? I could not prevent any access to elements which are outside of y[0][dim].

int main(){                                                                                                   
        int dim = 3;                                                                                          
        int ordnung = 2;                                                                                      
        double** y = new double*[ordnung];                                                                    

        for(int i = 0; i<ordnung; i++){                                                                       
                y[i] = new double[dim];                                                                       
        }                                                                                                     

        for(int i = 0; i<=100; i++){                                                                          
                cout << y[0][i] << endl;                                                                      
        }                                                                                                     

        delete[] y;                                                                                           
        return 0;                                                                                             
}   

The output is also confusing me:

0
0
0
1.63042e-332
0
0
0
6.520933e-319

and ongoing zeros. What does that mean?

  • Also related: http://stackoverflow.com/questions/12553154/initial-value-of-dynamically-allocated-memory-in-c. – Martin R Aug 28 '16 at 11:07

1 Answers1

1

When you allocate memory using new[], it doesn't initialize the memory in any specific way. Its content is indeterminate and accessing it, even for reading, leads to undefined behavior.

You also go out of bounds of the allocated memory which also leads undefined behavior. C++ have no built-in bounds-checking.

And then you don't free all the memory you allocate. For each new there should be a matching delete, and for every new[] a matching delete[].


Lastly a few notes: First of all if you ever think you need a dynamic array, then your next thought should be std::vector.

And about the memory being uninitialized when using new or new[], that of course depends on what you allocate. If you allocate an object (or an array of objects) with a constructor, that will of course cause the constructor to be called. The constructor may, or may not, initialize the object.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • so if I work with these uninitialized elements I will always run into undefined behavior in further code so it does not make sense to use them? So I have to explicitly prevent users from accessing them? – vl_stackoverflow Aug 28 '16 at 11:16