[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

sizeof question.

Posted on 2002-07-17
4
Medium Priority
?
322 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 150 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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
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 how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

650 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