Home > Back-end >  C o a great god
C o a great god

Time:09-26

C + + struct when declaring variables automatically assigns the memory? Is there new pointer is special?

CodePudding user response:

Statement structure variables are allocated memory, declare the structure pointer variable is the pointer variable itself occupy memory (4 bytes), when the pointer variable=new structure distribution when the structure of the body,

CodePudding user response:

So when using structure variables (no pointer), also need a new, new words and play what role? Using a pointer in the middle of the class also need new?

CodePudding user response:

What is new, well, you should see any material from a c + +,

CodePudding user response:

I mean now that statement structure variables have allocated memory space, and then, and it's meaningless to use new?

CodePudding user response:

Struct Person
{
Int no;
char sex;
char name[20];
};

Person p1;//declare structure variables are allocated memory
P1. No=1001;//p1 directly use, do not need nor p1=new Person (error)

The Person * (p2);//declare the structure pointer variable is a pointer variable p2 itself occupy memory (4 bytes)
The p2=new Person;//this time is assigned the structure of the body (20 bytes), and will be the first memory address is assigned to a pointer variable p2
The p2 - & gt; No=1002;

CodePudding user response:

Pointer variable p2 itself occupy memory allocated by the new (4 bytes) is used to store the first address of the memory block,

CodePudding user response:

Oh oh! Amazing!! thank you

CodePudding user response:

C + + data have, heap, stack, and static data area... Using the stack for local variables, such as defined in the code int I; Some pile is using malloc or like new, with static or define CPP file outside a function, is a static data area
  • Related