Solved

sizeof question.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

895 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

13 Experts available now in Live!

Get 1:1 Help Now