C + + character array and string disclosure

Source: Internet
Author: User

First: Writing this article stems from my discussion on ' the ' and '

When I'm on char a []=];

int size_a=sizeof (a); Result is 2

I was wondering if the string ended with ' m ', and I tested it again:

Char a[]= "b\0";

Cout<<sizeof (a); Result is 3

Test again:

              

Char a[]= "\0\0\0";
Cout<<sizeof (a); Result is 4

Final Test:

              

Char a[]= "";
Cout<<sizeof (a); Result is 1

The test found in the string is the system automatically added, and regardless of whether you have the original string;

Second:

Example one: int main (int argc, char *argv[])
{char str[] = {' H ', ' e ', ' l ', ' l ', ' o '};
Char bb[]={
' A ', ' B ', ' C ', ' D '
};
cout<<bb<<endl;

The output is as follows: Note that the character after the last 0 of each run of the output will be different

Example two: int main (int argc, char *argv[])
{char str[] = {' H ', ' e ', ' l ', ' l ', ' o '};
Char bb[]={
' A ', ' B ', ' C ', ' D '
};
cout<<str<<endl;

Output results

Why is the result of graph one output abcdhello* (* number denotes random character)? We can extrapolate a stack-based storage structure, so the pre-stack test defined in the function is

Example three:

int main (int argc, char *argv[])
{char str[] = {' H ', ' e ', ' l ', ' l ', ' o '};
Char bb[]={
' A ', ' B ', ' C ', ' D '
};
Char cc[]={
' A ', ' B '
};
cout<<cc<<endl;

Output results such as:

And then we're going to talk about a random number at the end.

int main ()
{
Char str[] = {' H ', ' e ', ' l ', ' l ', ' o '};

Char bb[]={' A ', ' B ', ' C ', ' D '};
Char cc[]={' A ', ' B '};
bb[7]= ' + ';
cout<<bb<<endl;

As a result, if you have a b[7]= ' ", then the output ends at seventh after BB

The above test was tested in MinGW gcc version3.2, using cfree, note that I used VS2010 test results are not the same AH

Test

Address high place at the bottom of the stack, output, such as Output cc bar, AB hot iron ABCD hot a few, and then hello in the hot a few final output also ended, as for the end, I will explore ING

Hope Daniel Treatise!

C + + character array and string disclosure

Related Article

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.