Solved

sizeof question.

Posted on 2002-07-17
4
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Author Comment

by:jfz139
ID: 7159650
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
ID: 7159676
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
ID: 7159681
>>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
ID: 7159724
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

726 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