I am trying to cout the address of the variable abc and the program just crash
int main()
{
int *app;
int abc = 2;
*app=3;
cout << *app << endl << &*app << endl << abc;
cout << &abc;
}
However, if I remove the address variable int *app , then it will cout the address of abc
int main()
{
int abc = 2;
cout << &abc;
}
I have no idea why would the existence of another unrelated address variable would affect it. Please advise.
The problem lies here:
*app=3;
this will probably cause a Segmentation Fault, Undefined Behavior.
If you remove it, it makes sense to see the expected output.
What are you trying to do?? This is undefined behavior.
int *app; // pointer is not initialized.
int abc = 2;
*app=3; // de-referencing an uninitialized pointer. Bad.
Related
Why do I have a memory fault in the below code? How do I fix it?
I want to read the progress of the outside function.
But I only get the output get_report_progress:100
#include <iostream>
int* int_get_progress = 0;
void get_progress(int* int_get_progress)
{
int n = 100;
int *report_progress = &n;
int_get_progress = report_progress;
std::cout << "get_report_progress:" << *int_get_progress <<std::endl;
}
int main()
{
get_progress(int_get_progress);
std::cout << "main get process:" << *int_get_progress << std::endl;
return 0;
}
Your global int_get_progress variable is a pointer that is initialized to null. You are passing it by value to the function, so a copy of it is made. As such, any new value the function assigns to that pointer is to the copy, not to the original. Thus, the global int_get_progress variable is left unchanged, and main() ends up deferencing a null pointer, which is undefined behavior and in this case is causing a memory fault.
Even if you fix the code to let the function update the caller's pointer, your code would still fail to work properly, because you are setting the pointer to point at a local variable that goes out of scope when the function exits, thus you would leave the pointer dangling, pointing at invalid memory, which is also undefined behavior when that pointer is then dereferenced.
Your global variable (which doesn't need to be global) should not be a pointer at all, but it can be passed around by pointer, eg:
#include <iostream>
void get_progress(int* p_progress)
{
int n = 100;
*p_progress = n;
std::cout << "get_report_progress:" << *p_progress << std::endl;
}
int main()
{
int progress = 0;
get_progress(&progress);
std::cout << "main get process:" << progress << std::endl;
return 0;
}
Alternatively, pass it by reference instead, eg:
#include <iostream>
void get_progress(int& ref_progress)
{
int n = 100;
ref_progress = n;
std::cout << "get_report_progress:" << ref_progress << std::endl;
}
int main()
{
int progress = 0;
get_progress(progress);
std::cout << "main get process:" << progress << std::endl;
return 0;
}
Alternatively, don't pass it around by parameter at all, but return it instead, eg:
#include <iostream>
int get_progress()
{
int n = 100;
std::cout << "get_report_progress:" << n << std::endl;
return n;
}
int main()
{
int progress = get_progress();
std::cout << "main get process:" << progress << std::endl;
return 0;
}
#include <iostream>
int main() {
int b = 3;
int* a;
*a = b;
std::cout << &a << a << *a;
}
When I execute a.out, Segmentation fault occurs
#include <iostream>
int main() {
int b = 3;
int* a;
*a = b;
std::cout << &a << a;
std::cout << *a;
}
While this code runs clearly with no error.
I'm pretty sure that max size of stdout buffer is bigger than couple of characters...
Why am I seeing Segmentation fault in same code with different new line?
Why am I seeing Segmentation fault ...?
Because the behaviour of the program is undefined. Here is the list of bugs in your programs:
*a = b;
^^
indirect through indeterminate pointer
std::cout << &a << a << *a;
^ ^^
| indirect through indeterminate pointer
read an indeterminate value
These bugs affect both example programs.
To fix the bugs, initialise a with a valid address of an int object.
This question already has answers here:
How to access a local variable from a different function using pointers?
(10 answers)
Closed 6 years ago.
I'm very new to c++ and programming with pointers. I was trying to pass an argument to a thread, add one to it, and thereafter return a pointer to this result. The main thread should just print the result pointed to by the returned pointer.
#include<stdio.h>
#include<pthread.h>
#include<iostream>
using namespace std;
void* calculator(void* _n) {
int* n = (int*) _n;
int* i;
int result = *n + 1;
i = &result;
return i;
}
int main(){
int input;
pthread_t calcThread;
void* exitStatus;
int* threadResult;
cout << "Input integer: " << endl;
cin >> input;
cout << "Init thread..." << endl;
pthread_create(&calcThread, NULL, calculator, &input);
pthread_join(calcThread, &exitStatus);
// Error around here?
threadResult = (int*) exitStatus;
cout << "Returned: " << *threadResult << endl;
}
The code compiles, but I get a segmentation fault when executing. My guess is that it has something to do with the cast i'm doing, but i can't figure out what.
Any help will be greatly appreciated!
i = &result;
You're returning a pointer to local variable. As soon as it goes out of scope, accessing it yields undefined behavior.
I have a problem. I want to operate with single element of an array, which is generated in member function, but it doesn´t work. Here is my code:
using namespace std;
class Example
{
public:
int *pole;
void generate_pole();
};
void Example::generate_pole()
{
int *pole = new int [10];
for (int i = 0; i < 10; i++)
{
pole[i] = i;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
Example reference;
reference.generate_pole();
cout << reference.pole[1] << endl; //there is the problem
system("pause");
return 0;
}
How can I get an access to the element? And where is the real problem? Thank you!
int *pole = new int [10]; is creating an identically named variable pole in local scope. This is shadowing the member variable.
A fix, drop the int* from the errant line: pole = new int [10];
That said, I'd be inclined to use a constructor to set the member variable in this case: certainly you should initialise pole to nullptr by default. This is so you can delete[] pole in a destructor when an instance of your class goes out of scope. Else your code will leak memory like a colander leaks water.
An other way would be to use std::vector<int> pole; and let the C++ standard library take care of all the memory for you.
The problem is, that you shadow pole's name in the scope of the function by redeclaring it. leave the int * in front of pole behind, in generate_pole, and it should work.
An example for shadowing:
int i = 0; // i is 0
std::cout << "before scope: " << i << std::endl; // prints 0
{
int i = 1;
std::cout << "inside scope: " << i << std::endl; // prints 1
}
std::cout << "behind scope: " << i << std::endl; // prints 0
I have the following code:
#include <iostream>
using namespace std;
int main ()
{
int myvar = 5;
int * p;
cout << "Hello2" << endl;
*p = myvar;
cout << "Hello" << endl;
cout << p << endl;
//cout << &myvar << endl;
}
I know I am not doing the right thing by not initializing the pointer. I was just playing with pointers and noticed this. The issue is when I comment out the last line, the program executes normally. But as soon as I uncomment the line, I get a segmentation fault. I don't know why printing address of myvar is causing this? Has myvar been modified in any way because of pointer dereferencing? I am using C++11.
int* p;
*p = myvar;
You are creating an uninitialized pointer and then derferencing that pointer. This has undefined behavior because p has to point to something for it to be derferenced correctly. Therefore your program's behavior can't be reasoned with.
Segmentation Fault occurs when trying to access a virtual memory address that has no read permissions.
In your case, the local variable p holds uninitialized garbage from the stack.
you are dereferencing a memory address that might not be readable(e.g no read permissions, hence the segmentation fault when trying to access it).
I'm not entirely sure the purpose of your snippet, but the following code will work, and perhaps it will help:
int myvar = 5;
int *p = nullptr;
p = &myvar;
cout << myvar << endl;
cout << &myvar << endl;
cout << p << endl;
cout << *p << endl;
(Note: I used two lines for setting 'p' because that is how you did it in your snippet. You could easily just use: int *p = &myvar; )
Anyway, there are scope issues here as p will only be valid as long as myvar is in scope; however, this does illustrate the basics of pointers. myvar and *p will return the same value (the value being pointed to), and &myvar and p will return the same value (the location of value in memory.)