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)
Related
This question already has answers here:
can't modify char* - Memory access violation
(4 answers)
Closed 2 years ago.
int main()
{
char *p = "I like C++";
strcpy(p, "John Smith");
std::cout << p << std::endl;
EXIT_SUCCESS;
}
As the title stated, why does this results in segmentation fault
Speaking in the terms used by strcpy() documentation, you are trying to copy "John Smith" (the source argument) into p (the destination argument).
Although p is a pointer of type char *, it resides in the read-only data section (.rodata probably).
Trying to copy a new string into it, means trying to write over read-only memory.
Changing the declaration to char p[] = "I like C++"; fixes the issue as p now resides on the stach which is both readable and writeable memory.
By the way, the last line is missing a return and should be return EXIT_SUCCESS.
This question already has answers here:
Why is address of char data not displayed?
(8 answers)
Closed 5 years ago.
Here in this code, cout<<q<<endl; is returning string "mani"?. q contain the address of first character 'm' so it should print address not string.please explain.
int main(){
char *q;
char b[5]={'m','a','n','i'};
q=&b[0];
cout<<b<<endl;
cout<<q<<endl;
std::cout has a special overload for const char*, which outputs the memory as an array of chars starting at the pointer passed up to the next NUL terminator (it's your job to make sure that the appropriate memory is available for that).
If you want to switch off this behaviour and output the pointer address then use a cast:
std::cout << (const void*)b << endl;
This question already has answers here:
Two different values at the same memory address
(7 answers)
Closed 5 years ago.
Someone showed me the following code snippet and asked what it should output
#include <iostream>
using namespace std;
int main() {
const int value = 10;
int* p = (int*)&value;
*p = 20;
cout << value << " " << *p << endl
<< &value << " " << p << endl;
return 0;
}
As you can see, there's a constant value which is 10, and there's a pointer p which points to the address of value, then the address pointed gets a different value.
I expected the program to print 20 20 but it actually prints 10 20.
It also shows that the two valuables have the same address. Can someone explain what's going on behind the scenes here?
Undefined behavior and an optimizing compiler. The compiler knows from value's declaration that value's value will never change in a well-formed program, so it optimizes out the bits where value's value would be checked and just takes the value it knows value has.
As for the addresses, you never take the address of p, and that p is the same as &value is not surprising because you assigned it that way a few lines earlier.
This question already has answers here:
Why is address of char data not displayed?
(8 answers)
Closed 3 years ago.
char *m_chString="asd";
cout<<m_chString;//prints asd
cout<<*m_chString;//prints a
int nValue = 7;
int *pnPtr = &nValue;
cout<<*pnPtr;//prints 7
cout<<pnPtr;//prints the address of nValue
I gave two examples, in the first the pointer points to a string, and in the second, the pointer prints to an int value.
My question is, why cout<<m_chString; from the first example doesn't print the address of my string as it would do in the second example if I were to print pnPtr without dereferencing it?
Doesn't pnPtr point to an address?
The reason for that is that std::cout will treat a char * as a pointer to (the first character of) a C-style string and print it as such.
You can print the address by:-
cout << (void *) m_chString;
OR if you are great C++ fan then
cout << static_cast <const void *> (m_chString);
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.