Reputation: 27
Additional question (1 point): Assume we are using 32-bit Windows operating systems and C/C++ programs. Please estimate the sizeof() for the follows (unit: byte)
• char str[] = “Hello” ;
• char *p = str ;
• int n = 10;
Please calculate:
• sizeof ( str ) = __________
• sizeof ( p ) = __________
• sizeof ( n ) = __________
Hello All,
I am trying to wrap my mind around this fairly fundamental concept in C++. I will tell you what I think the correct answers are to see if I am on the right track. For the first one, sizeof(str), I believe it is 6 bytes total. I noticed a pattern from other problems that there is always 1 byte added to these types of strings. (5 letters +1). My question is, what is this "+1" from? As for the second one down, this is just referring to the size of a pointer,p, correct? which is always 4 bytes in size? Finally, the third one, I believe is just referring to the size of an int, n. From what I know all ints are of size 4 bytes correct? Does this mean all ints are 4 bytes regardless if its 10 or 10000, or any other number. Any other important info on this topic is also greatly appreciated and accepted with open arms! Thanks!
Upvotes: 2
Views: 762
Reputation: 5528
In C++, if we pass an array to sizeof
we get its length; not the pointer it decays to's size. A string literal is an array of n
characters with a null terminator.
So for
const char str[] = "foobar";
sizeof(str) == 7
because in reality it will be set out in memory like this:
const char str[] = {'f', 'o', 'o', 'b', 'a', 'r', '\0'};
This is a compile-time constant and is known by the compiler as soon as the array is defined.
If you take a pointer to that array const char *strp = str
then sizeof(strp) == sizeof(char*)
.
As for the size of int
or T*
(where T
is any object type), it is implementation defined. But sizeof(T*) == sizeof(U*) == sizeof(void*)
where T
and U
are any object types.
The only guarantee you get is sizeof(char) == 1
. Anything else is up to the implementation.
From N4140
(C++14 draft standard)
§ 3.9.1 Fundamental types
2 - There are five standard signed integer types : “signed char”, “short int”, “int”, “long int”, and “long long int”. In this list, each type provides at least as much storage as those preceding it in the list. There may also be implementation-defined extended signed integer types. The standard and extended signed integer types are collectively called signed integer types. Plain ints have the natural size suggested by the architecture of the execution environment46; the other signed integer types are provided to meet special needs.
So the actual guarantee you get is:
sizeof(signed char) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
signed char
is the same size as a char
but is a distinct type and unsigned char
is the same size as both but another distinct type again.
On another note, with most systems it will be like this:
64-bit operating system, sizeof(T*) == 8
32-bit operating system, sizeof(T*) == 4
and on 64 and 32-bit systems sizeof(int)
usually == 4
.
where T
is any object type
Upvotes: 0
Reputation: 1495
I noticed a pattern from other problems that there is always 1 byte added to these types of strings. (5 letters +1). My question is, what is this "+1" from?
That 1 byte is for null termination ie '\0'. Think about it.
If you use strlen(calculates at run time) instead of sizeof(calculates at compile time), strlen() starts scanning the string at first character, increments the count,moves to next character, there has to be some character at which the string has to mark it's end,right?? that is what null termination is for.
From what I know all ints are of size 4 bytes correct?
The C++ standard does not specify the size of integral types in bytes, but it specifies minimum ranges they must adhere to.
This is already explained here. [What does the C++ standard state the size of int, long type to be?
Good practice is to always use standard defines to avoid such confusions.
Look into
"stdint.h " or "cstdint"
header files if you are on linux. Not sure about windows.
int8_t, int16_t, int32_t, uint8_t, uint16_t, uint32_t
these make the code more readable.
Upvotes: 0
Reputation: 550
Ok its my first answer on stackoverflow
First Question :
char str[] = "Hello";
in C/C++ character arrays need a null termination('\0') or (NULL) to identify the end of the character array, whenever we are reading character array the pointer is placed at the first index and it loops till we find null termination character.Hence its Size is 6.
Note the null termination character varies from compiler to compiler(as per my knowledge) in general they use '\0'. you can also use zero(0);
char s[] = {'H','E','L','L','O',0};
or 'NULL'
char s[] = {'H','E','L','L','O',NULL};
Second Question:
char *p = str
The character pointer 'p' stores the address of the character array 'str' it points to the first index of the character array and stores the address. which is of 4 bytes. hence you get
sizeof ( p ) = 4 // p holds the address of str
where as if you had used *p (value at p) then you would have received value as 1.
• sizeof ( *p ) = 1 // Now *p is value at p i.e first element in
character array = 'H' which is of size 1 byte
Third Question
int n = 10
This obviously is going to give you size as 4 bytes (8 bits) on 32 bit system.
Again the size of also varies from compiler to compiler.
talking about your last question
Does this mean all ints are 4 bytes regardless if its 10 or 10000, or any other number
The answer is yes and no For a 16 bit compiler of C/C++ the range for signed int is -32768 to 32767.
For 32 bit compiler the range is -2147483648 to +2147483647
any number (of type signed int) between this range is going to take 4 bytes on a 32 bit compiler like Turbo C
Any other important info on this topic is also greatly appreciated and accepted with open arms
Fun Fact For above if
int n = 2147483648 //note the extra 1
is still going to print or store
2147483647 in n
Try it out (on 32 bit compiler)
Secondly in C++ for strings i.e
string str = "Hello";
• str.size() = 5 // note: its not 6
I hope I answered your Question and provided additional information.
Upvotes: 1
Reputation: 206737
char str[] = "Hello" ;
is equivalent to:
char str[] = {'H', 'e', 'l', 'l', 'o', '\0'};
The standard guarantees that sizeof(char)
is 1
. Hence sizeof(str)
is 6
.
The size of a pointer and an int
are always platform dependent.
Upvotes: 1