size of pointer variable

#include<iostream>

using namespace std;

int main()
{
char *p;
cout<<"size of:"<<sizeof(*p)<<" and "<<sizeof(p);
cin.get();
return 0;
}

OUTPUT: size of:1 and 4
i don't understand please explain
sizeof(char) == 1 : by definition
sizeof(char*) == 4 : you're probably on a 32 machine (or at least it is a 32 bit executable)
the scale of sizeof() returns number of bytes that a variable takes up in memory. Open up your calculator in windows and switch the view to programmer.

Now in the bottom left you'll see 4 options: Qword, DWord, Word, Byte.

Byte = 1
Word = 2
(double)Dword = 4
(quad)QWord = 8

Since sizeof(char) is 1 click on the 'byte' option and thats how many bits are allocated in memory.

sizeof(char*) is 4 so click on DWord and thats the bit allocation.

R0mai says you're probably running on a 32bit machine because pointers(char*) are locations in memory. So if the size of that pointer is 4 then it can only address up to 32 bits. If you were running a 64bit machine/executable then it would be 8 so it could address all 64 bits.
One more interesting result:)

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>

int main()
{
	std::string s( "1234567890" );
	std::cout << "sizeof( s ) = " << sizeof( s ) << std::endl;
	std::cout << "s.size() = " << s.size() << std::endl;
}



Output is

sizeof( s ) = 32
s.size() = 10
@vlad is that because the class uses 32 bytes to store the 10 char long string?
@Script Coder
@vlad is that because the class uses 32 bytes to store the 10 char long string?


Only today I saw how many bytes the MS C++ realization of the std::string occupies.:)
Last edited on
Topic archived. No new replies allowed.