Solved

sizeof question.

Posted on 2002-07-17
4
293 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
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cross compile release version of c++ program for linux 2 133
Getting IP address 8 79
Would like to move button in a function 3 77
How to gracefully close the c++ 11 thread? 3 95
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

809 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