This question already has answers here:
Does "Undefined Behavior" really permit *anything* to happen? [duplicate]
(9 answers)
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 1 year ago.
This is a simple program I wrote to induce segfault
#include<iostream>
using namespace std;
char* func(){
char ch='a';
char *ptr=&ch;
return ptr;
}
int main()
{
char *ptr=func();
cout<<*ptr;
}
As variable ch must've been freed after func() scope ends, I thought that this program would give segmentation fault.
But it shows output as 'a'. What am i missing ?
Related
This question already has answers here:
displaying address of char variable in c++ using pointers?
(1 answer)
Why does streaming a char pointer to cout not print an address?
(4 answers)
Closed 9 months ago.
As far as my knowledge in c++, the & character can act as an address of operator(finding the address of a variable in memory) or as a bitwise AND operator or declaring references.
However if I run this code:
#include<iostream>
#include<string>
using namespace std;
int main() {
string s = "Stackoverflow";
cout<<&s[0]<<endl<<&s[1]<<endl;
return 0;
}
Output is
Stackoverflow
tackoverflow
I expected it to print the addresses of the first 2 characters of the string, however, I got the string itself starting from a different index. How does it work?
This question already has answers here:
What is the difference between char s[] and char *s?
(14 answers)
Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?
(19 answers)
Closed 1 year ago.
I am trying to assign a single inside char *ptr and then print the changed value. But it is giving segmentation fault. But don't see the issue if the same is declared as ptr[32] instead of *ptr;
int main () {
char *ptr = "abcdefghijklm";
ptr[2] = "q";
printf("%s",ptr);
}
Any guess what is going wrong here?
This question already has answers here:
Accessing an array out of bounds gives no error, why?
(18 answers)
Closed 2 years ago.
I have written a small program but couldn't understand why it does't crash when a is accessed out of bounds?
#include <iostream>
using namespace std;
int main()
{
double *a = new double[4];
a[0]=1;
a[1]=2;
a[2]=3;
a[3]=4;
cout <<"size of double:"<<sizeof(double)<<endl;
a[100]=100; // why is it not crashing here?
return 0;
}
Could someone explain me whats happening here? Shouldn't this crash at a[100]=100?
Why doesn't the below program crash?
From perspective of C++: Because the behaviour of the program is undefined. The program is neither guaranteed to crash nor to not crash. Nor are there any other guarantees about the behaviour.
From perspective of the operating system: Assuming the compiler did not remove the access due to having detected undefined behaviour, it probably didn't crash because that memory happened to be mapped for the process. In general, it is not safe to assume that all buffer overflows could be detected.
This question already has answers here:
Are data members allocated in the same memory space as their objects in C++?
(6 answers)
Closed 4 years ago.
how does tmp get memory from the machine, from heap or stack?
I thought it was from the stack, but it seem that the code can run properly
#include<bits/stdc++.h>
using namespace std;
struct node {
int a[1000000];
};
int main() {
node tmp;
memset(tmp.a, -1, sizeof(tmp.a));
cout << tmp.a[0];
return 0;
}
In stack, since it's an automatic variable to the main function.
PS: This code doesn't compile, for example with this error: error: type 'node' does not provide a subscript operator: cout << tmp[0];.
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.