This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 8 years ago.
I have a function in my class like:
centrala* siec_telek::wylosuj_centrale()
{
int wylosowana = dowolna_liczba_do(ilosc_central);
centrala wylosowana_centrala = lista_central[wylosowana];
centrala* wsk = &wylosowana_centrala;
cout <<wsk->przepustowosc_central[0]<<endl<<wsk<<endl;
return wsk;
}
And cout gives me good result but when I call in other function:
centrala* wylosowana_centrala = wylosuj_centrale();
cout << wylosowana_centrala->przepustowosc_central[0]<<endl<<wylosowana_centrala<<endl;
przepustowosc_central[0] gives another result but the pointer is ok (for instance 0x28f9cc twice)
Its because you are assigning local value address (wylosowana_centrala) to return value.
Due to wylosowana_centrala being stored on stack it may got overwritten after exiting function.
As fix you might try changing
centrala* wsk = &wylosowana_centrala;
to:
centrala* wsk = &lista_central[wylosowana];
Related
This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 7 years ago.
Here is a simple of example of a question I have:
I create a pointer of integer (value 5), I print the pointer (hence the address) of the memory case it points to, as well as its content (5).
Then I delete that pointer, which is supposed to delete the content in the memory case that has the address of the pointer.
But then when I print a and its content, as expected the address still exists. Nevertheless the content remains the same (5) as if a was not deleted...
could you explain me? :-)
#include <iostream>
int main()
{
int * a = new int(5);
std::cout<< a << std::endl;
std::cout<< "--------" << std::endl;
delete a;
std::cout<< a << std::endl;
std::cout<< *a << std::endl;
// end of my main
return 0;
}
result:
0x7fff28403a90
--------
0x7fff28403a90
5
It may or may not print the same value after delete operation. So simply what you are observing is an undefined behavior.
This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 7 years ago.
The code is:
char* c;
if(c!=NULL)
{
cout << "c has an address the address is "<<c;
}
else
{
cout << "c is null";
}
The output is:
c has an address the address is [Finished in 0.3s]
If c is not NULL, why c doesn't be printed out as an adress something like "0x401dee"
The problem with pointers is, that they often have a value after creating it, but its not a NULL pointer (!). But because its a pointer it points to a random address in your memory. That can cause pretty big problems, that's why you should ALWAYS initialize a pointer with
TYPE * NAME = NULL
So it hasn't a value and so it can't point to something, that could cause problems and now you can test with
NAME == NULL
(Of course you can also initialize it with a real value like the address of one of your variables)
This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 8 years ago.
I guess this is a very basic question, but even after looking around on the internet for a while I can't seem to find a proper answer to this question.
I will refer to this tutorial on stack/heap:http://gribblelab.org/CBootcamp/7_Memory_Stack_vs_Heap.html
Let's say I write the following code:
void testVoid() {
int testVariable = 5;
}
If I'm not wrong, this function should create a variable located on the stack. As the functions exits, the allocated memory on the stack is deleted - so my variable should also have been deleted by then.
I have learned that pointers in c++ point to the memory location of a variable.
So if the variable pointed to by a pointer is located on the stack, and the stack then is cleared, I would expect not to be able to access original value through the pointer anymore, since the memory location pointed to is is cleared. However, I tested this:
int* pointer;
void testVoid() {
int num = 3;
pointer = # // Here I get the memory location of my num-variable
cout << pointer << " : " << *pointer << endl; // I would get the same result if i printed &num
}
int main(int args, char** argv) {
pointer = new int;
testVoid();
cout << pointer << " : " << *pointer << endl; // I can still access the memory of my num-variable
while (true) {}
return 0;
}
After exiting the testVoid()-function, where the variable is created, I can still get the value of the variable using my pointer. So obviously I have misunderstood something regarding how pointers work in c++. Printing &pointer and &num gives me the same memory location, even after testVoid() has finished. What is the reason for this? If the memory pointed to by the pointer were moved to the heap, shouldn't cout<<&num and cout<
And here's the output:
0024F904 : 3
0024F904 : 3
Just because the value goes out of scope does not mean the memory for the value has been overwritten. You just can't rely on it being stable at that point.
This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 8 years ago.
void foo(int** ptr) {
int value = 4;
*ptr = &value;
// **ptr = value;
}
int main(void) {
int value = 7;
int* ptr = &value;
foo(&ptr);
cout << *ptr << endl; // 4
return 0;
}
My Question is - as the value = 4 is no longer valid/out of scope after returning from foo, why *ptr is showing 4 instead of some garbage value?
Formal answer: undefined behavior.
Practical answer: no other operation on the stack has yet to override that value.
Because you're returning a pointer to a local variable, this is undefined behavior. This includes "appearing" to work, but it's a terrible idea to rely on it in the general case.
In this specific case, the value is left on the stack, and it appears the generated code fetches *ptr just after the call to foo, and before any other function calls. As such, the value has not been overwritten by any other function calls.
If you were to instead insert a function call between the foo(&ptr) and cout << ... statements, the value would more than likely be garbage.
This question already has answers here:
Returning a reference to a local or temporary variable [duplicate]
(5 answers)
Can a local variable's memory be accessed outside its scope?
(20 answers)
Return address of local variable in C
(5 answers)
Closed 9 years ago.
I saw some weird behavior with my C++ function whose work is to simply put 'n' ones(1).
char *getSpaces(int n) {
char s[50];
int i = 0;
for(i=0; i<n; i++) {
s[i] = '1';
}
s[i] = 0;
return s;
}
When I do fout<< getSpaces(20), I get following output in the file :-
1111111111SOME_WEIRD_CHARACTERS_HERE
Can anybody explain this?
P.S. I am using codeblocks IDE on windows platform.