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.
Related
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.
This question already has answers here:
C++ delete - It deletes my objects but I can still access the data?
(13 answers)
Closed 4 years ago.
In the below code, memory is allocated for an integer and later a shallow copy is being made and finally delete is being called on it. How does it still print 23 as the output and why doesn't the delete call on q, cause a run time exception.
#include <iostream>
using namespace std;
int main() {
int* p = new int(23);
int* q = p;
delete p;
cout << *p << endl;
delete q;
return 0;
}
Undefined behaviour means anything can happen.
It might crash.
It might crash your car.
It might crash your brain.
It might crash Sagittarius A* into your brain.
It might crash your brain into your car, then crash them both into Sagittarius A*.
It might appear to work.
But it's still undefined.
Don't expect results.
This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 5 years ago.
I'm trying to understand how to use functions in C++ to return arrays. I want to understand what's going on here. This is my code, that I compile with g++ -Wall return_array.cpp -o return_array
#include <iostream>
int * do_something(int number){
int viz[3] = {1,2,3};
viz[2] += number;
return &viz[0];
}
int main(){
int *viz;
viz = do_something(3);
std::cout << viz[2] << "\n";
return 0;
}
which gave me the following error:
return_array.cpp: In function ‘int* do_something(int)’:
return_array.cpp:4:6: warning: address of local variable ‘viz’ returned [-Wreturn-local-addr]
int viz[3] = {1,2,3};
By my research, the error is due to the fact that I'm trying to access a pointer that has been deleted from the Stack once I leave the function's scope. However, if I run the code with a workaround:
int * do_something(int number){
int viz[3] = {1,2,3};
int *pointer;
viz[2] += number;
pointer = &viz[0];
return pointer;
}
it compiles and runs just fine. Isn't this new pointer in the precise same situation? Once I leave the function, it is out of scope and should be deleted from the Stack, by the same argument as before. What am I missing?
EDIT:
My question here is not whether or not returning the pointer to a local array produces an error. It should and it does. The question is why the pointer that has been assigned to the pointer to the same array does not!
Once control leaves the function, the array is out of scope. It might be erased, it might not. That region of memory might still contain those numbers for a while, and it might not. Trying to read it might work, or it might do anything. This is known as undefined behavior, and it is a difficult thing to detect and a good thing to avoid.
This question already has answers here:
How to access a local variable from a different function using pointers?
(10 answers)
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 6 years ago.
This is my C++ code .
According to me , it should give output:
abc
Garbage
abc
But it is giving output:
abc
Garbage
Garbage
#include<bits/stdc++.h>
using namespace std;
char **func()
{
char* PA = new char[10];
PA[0]='a';
PA[1]='b';
PA[2]='c';
PA[3]='\0';
printf("%s\n",PA);
printf("Garbage\n");
char **PPA = &PA;
return PPA;
}
int main()
{
printf("%s\n",*func());
return 0;
}
Where am I doing wrong?
char **PPA = &PA;
Retrieves the address of the variable PA itsself, which is an automatic variable and goes out of scope as soon as the function terminates. That means you have undefined behavior here. The C standard doesn't guarantee any consistent behavior, so anything may happen, including what you experienced.
To fix that, you could change the function prototype to char* func() and return PA directly and remove PPA altogether.
This question already has answers here:
Code outside functions
(3 answers)
Closed 7 years ago.
I have a problem when using 'delete' operator. It is identified as a syntax error by VS2013 with Nov 2013 CTP compiler, giving the message: "expected a declaration". Here is the code:
int a = 1;
int* p = &a;
int* snj = new int[10];
delete p;
delete[] snj;
C++ doesn't let you write arbitrary expressions in the top-level like Python or other languages. You need to place your code in a function, probably main in this case:
int main()
{
int a = 1;
int* p = &a;
int* snj = new int[10];
delete p;
delete[] snj;
}
Note that using delete on a pointer which wasn't allocated using new is undefined behaviour.
Things like this are very basic and should be covered by your introductory book. If you don't have an introductory book, you should get one.
p points to the address of a. a is a local variable decalred on the stack. You can only call delete on dynalically allocated heap memory (Any variable created using the new keyword).
a is deleted automatically when it goes out of scope.
You should not delete p since it is not pointing to a heap element, it points to a stack element.