C language question 1

Source: Internet
Author: User
C language question 1

From: huangyaoshifog's column

Http://blog.csdn.net/huangyaoshifog

First, let's look at the following program
Char * xyzstring = "Hello, world! ";
Int main (void)
{
Xyzstring [0] = 'a ';
Printf ("% s/n", xyzstring );
Return 0;
}

Is there a problem with this program? Yes, xyzstring data should be static data and cannot be modified. Yes, this is also true in C language standards.

Is that true? Let's take a test and use four compilers to check the actual running conditions.

Codewarrior 8.0, Visual C ++ 6.0, and Borland C ++ 5.6 are compiled and run successfully! ", GCC 3.2 stops running the program after compilation.

The answer to this question is simple. GCC will "Hello, world! "Put in the. text section, while the other three compilers are" Hello, world! "To the. Data Segment. In the program, the. Text Segment is used to store the code, so it cannot be modified. Therefore, when we use xyzstring [0] to modify the content, the program will of course be replaced. The. Data Segment can be modified because it is a data segment, so the program will not be replaced.

To verify the above arguments, we use GCC to compile the source code into test. S.

Run the following command gcc-s test. c
. File "test. c"
. Globl _ xyzstring
. Text
Lc0:
. ASCII "Hello, world! /0"
. Data
. Align 4
_ Xyzstring:
. Long lc0
. Def ___ main;. SCL 2;. Type 32;. endef
. Text
LC1:
. ASCII "% S/12/0"
. Align 2
. Globl _ main
. Def _ main;. SCL 2;. Type 32;. endef
_ Main:
Pushl % EBP
Movl % ESP, % EBP
Subl $8, % ESP
Andl $-16, % ESP
Movl $0, % eax
Movl % eax,-4 (% EBP)
Movl-4 (% EBP), % eax
Call _ alloca
Call ___ main
Movl _ xyzstring, % eax
Movb $97, (% eax)
Subl $8, % ESP
Pushl _ xyzstring
Pushl $ LC1
Call _ printf
Addl $16, % ESP
Movl $0, % eax
Leave
RET
. Def _ printf;. SCL 2;. Type 32;. endef

The above is the output result. We can see that the first two rows of test. s are
. Globl _ xyzstring
. Text
Note _ xyzstring is defined in the. text section. Now, we rename it as follows:
. Globl _ xyzstring
. Data
Then compile gcc-O test.exe test.s and execute test.exe. What is going on? haha "aello, world! ", The execution is OK.

The above is the solution for different compilers for this type of code, but for compatibility, we should never write the above Code.

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.