On the relationship between defining order and memory allocation--a non-rigorous C-language problem

Source: Internet
Author: User

Include<stdio.h> #include <iostream>int main () {char a[] = "123"; char b[] = "ABCD"; if (a > B) {printf ("a>b \ n ");} elseprintf ("a<b\n");p rintf ("%p\n%p\n", A, b); system ("Pause");}

Is such a topic, ask you the final output is a>b or a<b, the answer is a>b.

I see, this A and B ratio is what ah, if it is according to strcmp () method to compare, that certainly a<b ah, that obviously is not, then should be compared to the pointer address size, but this thing is not randomly assigned, is not my posture level is not enough? So I started trying.

The first piece of code in the Vs2017debug mode is actually a>b, and the answer to the question is the same.

, but once we change the order of the definitions of A and B,

#include <stdio.h> #include <iostream>int main () {char b[] = "ABCD"; char a[] = "123";//change the position of a/b definition if (a > {printf ("a>b \ n");} elseprintf ("a<b\n");p rintf ("%p\n%p\n", A, b); system ("Pause");}

The answer becomes a<b, is it metaphysics? What is the larger address defined first? Then I can't spray the subject.

Because this is not the case, it is also the original code, and when we replace debug with release, we find that the result is the opposite and becomes a<b.

This problem is not rigorous, in fact, C language to the compiler for memory address allocation order does not have a hard rule, from the compiler's point of view, the variable address can be randomly arranged, and does not affect the use of addressing, not necessarily in order to arrange. Different compilers are assigned in different ways. For example, in the debug mode of VS, the defined address is larger, the address defined is smaller, and the release mode is the opposite. Today is also a rise in posture, learning a.

On the relationship between defining order and memory allocation--a non-rigorous C-language problem

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.