Solved

sizeof question.

Posted on 2002-07-17
4
286 Views
Last Modified: 2010-04-01
Hi,

I am puzzled by sizeof for a while.
I did the following example:

char * a = "ABC";
char * b = new char[255];
strcpy(b, b);
cout<<sizeof(*b)<<" "<<sizeof(b)<<endl;
The print out is always: 1, 4.

My questions are:
1. What does 1 and 4 mean and why it is 1 and 4?
2. Can sizeof be overloaded?

Thank you very much.

Jennifer
0
Comment
Question by:jfz139
  • 2
4 Comments
 

Author Comment

by:jfz139
Comment Utility
Sorry, there is a typo in my code:
strcpy(b,b) should be strcpy(b,a).
0
 
LVL 16

Accepted Solution

by:
imladris earned 50 total points
Comment Utility
sizeof will return the size of the object. So for instance

sizeof(int)

is 4 since an integer is 4 bytes. For the variable:

int t[100];

the result you will get from sizeof(t) is 400.

The thing to remember (and what I think is the source of your uncertainty) is that sizeof is a compile time operation, so the result is based on what the compiler can know, which means that it runs off of declarations.

So in your example you have a variable "char *b". So sizeof(*b) is the number of bytes in what b points to, that is a character: 1. And sizeof(b) is the number of bytes in a pointer to a character: 4.
The compiler is not in a position to figure out what b is pointing to, it merely reports sizes based on declarations. If you declared b as:

char b[255];

Then sizeof(b) would be 255.

0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>cout<<sizeof(*b)<<" "<<sizeof(b)<<endl;
>>The print out is always: 1, 4.

>>My questions are:
>>1. What does 1 and 4 mean and why it is 1 and 4?
>>2. Can sizeof be overloaded?

1. 'sizeof(*b)' will return the size of a character, which is 1 byte. 'sizeof(b)' is the size of a pointer to a character, which is 4 bytes (pointer are 4 bytes in size on 32bit platforms in general)

2. Why would you want to overload it? Theorhetically, this is possible, but you'd have to overload it for every possible data type...
0
 

Author Comment

by:jfz139
Comment Utility
Thank you very much.
I think both of you are right.

Also, I want to add that I just saw on the internet that
sizeof can not be overloaded.

I would like to accept both answer; both I don't see the accept buttons here. How should I do to accept?
Jennifer
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now