I am wondered about the memory usage of variables and I tried this :
#include <iostream>
int main()
{
char* testChar1 = "Hi";
char* testChar2 = "This is a test variable";
char* testChar3 = "";
std::cout <<sizeof(testChar1)<<std::endl;
std::cout <<sizeof (testChar2) <<std::endl;
std::cout <<sizeof(testChar3)<<std::endl;
}
output is :
4
4
4
I think I am not doing the right thing. I want to know how much memory every variable uses in stack .
EDIT 1
At the same time if I does char* testChar3 = NULL; the program crashes. So does it mean there is no memory usage for the same?
char* testChar3 = NULLand then "reading"/accessing it later while it is stillnullwill result in a null pointer exception - which is why your program crashes.charpointer. If you set thechar*(charpointer) to null then you have still have achar*; it just does not point to anything.4when you are printing out the size of the pointers is because a pointer consists of 32-bits (4 bytes) for 32-bit executables.