search

Home  >  Q&A  >  body text

c++ - 程序中常量的定义

#define SIZE 100;
const int size = 100;

他们有什么区别

    char b[4] ;
    char (*pb)[4] = &b;//正常

b 是一个指针常量 ,也可以说是一个整形常量,可不可以理解是第二种情况,可以取地址,但不能赋值

b在内存中,有没有自己的空间

PHP中文网PHP中文网2803 days ago556

reply all(4)I'll reply

  • 巴扎黑

    巴扎黑2017-04-17 13:56:08

    1. define macro is expanded during the preprocessing stage, const is expanded during compilation and runtime

    2. define does not have a type and does not perform security checks, const does have a type, and security checks will be performed during the compilation phase

    3. define occupies code space; const is a variable definition, which occupies data segment space and is more efficient

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:56:08

    define macro definition is just a simple text replacement in the preprocessing stage of the compiler. That is, when compilation officially starts, SIZE in your code will be replaced with 100. If there is a compilation error or a problem occurs during runtime, you can only know that there is a problem with 100, and SIZE is invisible to you, which is not conducive to debugging. Using constants will not have such problems.

    Your code below may look awkward, but it’s fine. b is a variable allocated on the stack, which naturally occupies memory space.

    reply
    0
  • 迷茫

    迷茫2017-04-17 13:56:08

    @老老哥不了Assignment uses lea
    to open up 16 bytes on the stack, 8 for arrays, 4 for pa, and 4 unused

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 13:56:08

    I’m not very good at learning C language. Please correct me if I’m wrong^_^

    char b[4] ;
    char (*pb)[4] = &b;//正常

    b is an array of 4B;
    pb is an array pointer pointing to the memory space of b, so you can get the address of pb by reading the content of b. But pb+1 points to after the last byte of b.

    reply
    0
  • Cancelreply