This question already has answers here:
Undefined, unspecified and implementation-defined behavior
(9 answers)
Closed 2 years ago.
Is this a segmentation error or not? Why not? Please answer me, why this doesn't give me segmentation error?
#include <iostream>
using namespace std;
int main()
{
int ** T;
T = new int*[5];
for(size_t i=0; i<5; i++){
T[i] = new int[5];
}
T[4][7] = 10;
return 0;
}
Looks OK to me except for the 7 at the end. T is a pointer to pointer to ints, and it's assigned to an array of 5 pointers to ints (OK so far). Then each member of that array is assigned to a pointer to 5 ints. Still fine. Lastly, T[4] is a pointer to ints, but only 5 are assigned. So T[4][7] goes off the end of the array.
Now, will this actually segfault? That depends on memory alignment. If that address, &T[4][7] is on a different memory page than the actual allocated array, you'll get a segfault. Otherwise, you're just reading unallocated memory and you get whatever's there.
Related
This question already has answers here:
Accessing an array out of bounds gives no error, why?
(18 answers)
Closed 2 years ago.
I had declared a array as
int* arr = new int[10];
Then I filled up the first 10 element and also did
arr[10] = 100;
arr[11] = 200;
When I do
cout << arr[10] << '\t' << arr[11];
and the output was
100 200
I wonder why the code is working. Is this valid or am I missing some information on this topic.
Shouldn't the arr[10] and arr[11] be holding any garbage value.
Reading/Writing out-of-bounds on an array is undefined behaviour, so anything can happen. It might work, it might not work, it might work every second full moon, anything goes.
This question already has answers here:
Accessing an array out of bounds gives no error, why?
(18 answers)
Closed 4 years ago.
int main()
{
int n[] = {2};
cout <<n[1] << endl;
return 0;
}
In this code, I get some numbers,also for n[2],n[3] etc. I got ë for a string array and # for a char array. I don't think it is the memory location so what is this?
int n[] = {2};
Here you are declaring an array with one element. So now n[0] is 2. That's the only element, so everything else is out of bounds. Therefore this here:
cout <<n[1] << endl;
Is undefined behavior, because you're accessing the second element, which doesn't exist. Remember, array indexing starts at 0, so element 0 is the first one and 1 is the second one.
So why does it print a rubbish value, then? Since it's undefined behavior, there's no guarantee for what it does, but what's most likely going to happen is that it's calculating the respective memory location and prints the data at the resulting address, interpreted as int. That usually results in some rubbish value.
This question already has answers here:
Dereferencing pointers without pointing them at a variable
(4 answers)
Closed 6 years ago.
Consider the following code:
#include <iostream>
int main()
{
// ok by itself
int *ptr1;
int a = 3;
*ptr1 = a;
// ok by itself
int *ptr2 = new int(4);
delete ptr2;
}
This results in a seg fault, and I can't figure out why. If either of the blocks are commented, it's okay. If the second block is placed above the first block, it's also okay. What's going on here?
You cannot dereference ptr1 because it's uninitialized.
With that said, *ptr1 = a is incorrect, and you're getting undefined behavior, which means that this code may or may not work as one expects.
This question already has answers here:
Accessing an array out of bounds gives no error, why?
(18 answers)
Closed 6 years ago.
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?
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.
This question already has answers here:
Two different values at the same memory address
(7 answers)
Closed 5 years ago.
How could the same memory location hold different values in the following program?
I am using g++ compiler.
code:
#include<iostream>
using namespace std;
int main()
{
const int i=100;
int *j = const_cast<int*>(&i);
*j=1;
cout<<"The value of i is:"<<i<<endl;
cout<<"The value j holds:"<<*j<<endl;
cout<<"The address of i is:"<<&i<<endl;
cout<<"The address of j is:"< <j<<endl;
}
output:
The value of i is:100
The value j holds:1
The address of i is:0xbffbe79c
The address of j is:0xbffbe79c
You've got undefined behavior, so anything can happen. In this case, the compiler knows that i cannot change value, and probably just uses the value directly.
The memory cannot hold different values at the same time. But, modifying an object that was declared const is undefined-behavior so anything can happen.
Anything includes making it look like the same memory location is holding different values at the same time. What happens is that since i cannot be changed, it is a candidate for constant folding, so there isn't any memory involved when i appears in an expression.