Getting problem in char* dynamic memory allocation

I am using Visual C++ 6.0. When I am trying to allocate memory dynamically for a char*, it is not allocated correctly.

For example:
//Example Code
char* charp;
charp = new char[1];
cout<<"Size: "<<sizeof(charp)<<endl;

Size: 4

I can not understand this problem. I am trying to allocate 1 byte. But I am getting size as 4.

Please help me to understand this issue. Thanks in advance.
charp is a pointer.

A pointer is commonly 4 bytes long.

Therefore, the size of the pointer named charp is 4.

If this makes no sense to you, or you don't understand how a pointer can always be 4 bytes long, please read about pointers:
Last edited on
Hi Moschops,

Thanks for your help. Now I can understand the reason.

Actually I am trying to read binary file using char*. If the size of char* is 4 means then how can I read 1 byte or 2 bytes data?

Is there any simple way to do that? or do I need to read all bytes and then split that as per my need?

Thanks again.
Reading a single byte:

#include <iostream>
#include <fstream>
int main()
    std::ifstream infile("filename");
    unsigned char b;
    infile >> b;  
Thanks for the explanation.
Topic archived. No new replies allowed.