sizeof (string) problem (very classic)

Source: Internet
Author: User

See a code today, just at the beginning did not understand, the code is as follows:

<span style= "FONT-SIZE:14PX;" > #include <iostream> #include <string>using namespace std;void main () {string a= "www.ok2002.com"; string b= "Study C + + program"; A.append (b,0,sizeof (b) +2);cout<<a<< "" <<sizeof (b) <<endl;} </span>
results showed: sizeof (b) =16

When string= "C + +", sizeof (b) =16

So it's best to write:
A.append (B,0,b.size ());

Cause Analysis:

With regard to sizeof (string), the interview book says sizeof (string) = 4; At the time, I was wondering, was it allocating 4 bytes of memory to a string? The paperconcludes that theimplementation of string may be different in each library, but the same thing in the same library is that no matter how long the string is placed in your string, its sizeof () is fixed, and the space occupied by the string is dynamically allocated from the heap. Not related to sizeof ().
sizeof (string) =4 is probably one of the most typical implementations, but there are also 12, 32-byte libraries implemented by sizeof (). But VC6.0 after the test, sizeof (string) =16. or the compiler.


The bool data type is the same as the int type, and the number of bytes used in memory is related to the compiled system, which is different from the number of bytes consumed by the various compilation systems , in vc++6.0, the number of bytes it occupies is 1 bytes

sizeof bool=1;

What is sizeof

First look at the definition of sizeof on MSDN:

The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type (including aggregate types) . This keyword returns a value of type size_t.

See the word return, do not think of the function? Wrong, sizeof is not a function, have you ever seen a function that passes arguments without parentheses? sizeof is possible, so sizeof is not a function. Online Some people say that sizeof is a unary operator, but I do not think so, because sizeof is more like a special macro, it is evaluated during the compilation phase. As an example:

cout<<sizeof (int) <<endl; An int length of 4 on a 32-bit machine

Cout<<sizeof (1==2) <<endl; the = = operator returns the bool type, equivalent to cout<<sizeof (BOOL) <<endl;

The compilation phase has been translated to:

cout<<4<<endl;

cout<<1<<endl;

Here is a trap, see the following program:

int a = 0;

Cout<<sizeof (a=3) <<endl;

cout<<a<<endl;

Why is the output 4,0 rather than the expected 4,3??? is the characteristics that sizeof handles during the compilation phase. Since sizeof cannot be compiled into a machine code, the contents of the sizeof scope, that is, (), are not compiled, but are replaced by the type. The = operator returns the type of the left operand, so a=3 is equivalent to int, and the code is replaced by:

int a = 0;

cout<<4<<endl;

cout<<a<<endl;

Therefore, it is impossible for sizeof to support chained expressions, which is not the same as unary operators.

Conclusion: Do not think of sizeof as a function, nor as a unary operator, as a special compilation preprocessing.




sizeof (string) problem (very classic)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.