Differences between char * and char [] in C ++

Source: Internet
Author: User

Program Demonstration:


Test Environment Devc ++

Code

# Include <iostream>

Using namespace std;

Main ()

{

Char * c1 = "abc ";

Char c2 [] = "abc ";

Char * c3 = (char *) malloc (3 );

C3 = "abc ";

Printf ("% d % s \ n", & c1, c1, c1 );

Printf ("% d % s \ n", & c2, c2, c2 );

Printf ("% d % s \ n", & c3, c3, c3 );

Getchar ();

}

Running result

2293628 4199056 abc

2293624 2293624 abc

2293620 4199056 abc

References:

First, you must understand the partition format of the memory occupied by the Compilation Program:

The memory occupied by a c/C ++ compiled program is divided into the following parts:

1. stack: the stack zone is automatically allocated and released by the compiler, and stores function parameter values and local variable values. The operation is similar

Stack in the data structure.

2. heap-generally assigned and released by the programmer. If the programmer does not release the heap, it may be recycled by the OS at the end of the program. Pay attention to it and Data

The heap in the structure is similar to the linked list.

3. Global (static)-the storage of global variables and static variables is put together. initialized global variables and static variables

Variables are in one area, and uninitialized global variables and uninitialized static variables are in another adjacent area. After the program ends

Release.

4. Text Constant Area-constant strings are placed here. The program is released by the system.

5. Code Area

// Main. cpp

Int a = 0; // global initialization Zone

Char * p1; // not initialized globally

Main ()

{

Int B; stack

Char s [] = "abc"; // Stack

Char * p2; // Stack

Char * p3 = "123456"; // 123456 \ 0 is in the constant zone, and p3 is in the stack.

Static int c = 0; // global (static) initialization Zone

P1 = (char *) malloc (10 );

P2 = (char *) malloc (20); // The allocated 10-byte and 20-byte areas are in the heap area.

Strcpy (p1, "123456"); // 123456 \ 0 is placed in the constant area, and the compiler may optimize it into

Location.

}

Ii. Theoretical knowledge of heap and stack

2.1 Application Method

Stack:

Automatically assigned by the system. For example, declare a local variable int B in the function; the system automatically opens up space for B in the stack.

Heap:

The programmer needs to apply and specify the size. In c, the malloc Function

For example, p1 = (char *) malloc (10 );

Use the new operator in C ++

For example, p2 = (char *) malloc (10 );

But note that p1 and p2 are in the stack.

2.2

System Response after application

STACK: as long as the remaining space of the stack exceeds the applied space, the system will provide the program with memory. Otherwise, an exception will be reported, prompting stack overflow.

Heap: First, you should know that the operating system has a linked list that records idle memory addresses. When the system receives a program application,

Traverse the linked list to find the heap node with the first space greater than the requested space, delete the node from the idle node linked list, and

The space of the node is allocated to the program. In addition, for most systems, the size of the allocation will be recorded at the first address in the memory space.

Small, so that the delete statement in the code can correctly release the memory space. In addition, the size of the heap node is not necessarily positive.

If the size is equal to the applied size, the system automatically places the excess part in the idle linked list.

2.3 Application size limit

STACK: in Windows, a stack is a data structure extended to a low address and a continuous memory area. This sentence refers to the place at the top of the stack.

The maximum address and stack capacity are pre-defined by the system. In WINDOWS, the stack size is 2 MB (OR 1 MB). In short, it is a compilation

If the applied space exceeds the remaining space of the stack, overflow is displayed. Therefore, the space that can be obtained from the stack

Small.

Heap: the heap is a data structure extended to the high address and a non-sequential memory area. This is because the system uses a linked list to store idle memory.

The link is not consecutive, And the traversal direction of the linked list is from the low address to the high address. The size of the heap is limited by

Virtual Memory. It can be seen that the space obtained by the heap is flexible and large.

2.4 comparison of application efficiency:

STACK: the stack is automatically allocated by the system, and the speed is fast. But programmers cannot control it.

Heap: Memory allocated by new. It is generally slow and prone to memory fragments. However, it is most convenient to use.

In addition, in WINDOWS, the best way is to use Virtual Alloc to allocate memory, instead of heap or stack, but directly

The address space of the Process retains a piece of memory, although it is the most inconvenient to use. However, it is fast and flexible.

Storage content in 2.5 heap and stack

STACK: when calling a function, the first entry to the stack is the next instruction of the main function (the next executable statement of the function call Statement ).

Address, followed by the parameters of the function. In most C compilers, the parameters are from right to left and then local changes in the function.

Quantity. Note that static variables are not included in the stack.

When the function call ends, the local variable first goes out of the stack, then the parameter, and the top pointer of the stack points to the address of the initial storage, that is, the master

The next instruction in the function, where the program continues to run.

Heap: Generally, the heap size is stored in one byte in the heap header. The specific content in the heap is arranged by the programmer.

2.6 comparison of access efficiency

Char s1 [] = "aaaaaaaaaaaaa ";

Char * s2 = "bbbbbbbbbbbbbbbbb ";

Aaaaaaaaaaa is assigned a value at the runtime;

Bbbbbbbbbbbbb is determined during compilation;

However, in future access, the array on the stack is faster than the string pointed to by the pointer (such as the heap.

For example:

# Include

Voidmain ()

{

Char a = 1;

Char c [] = "1234567890 ";

Char * p = "1234567890 ";

A = c [1];

A = p [1];

Return;

}

Corresponding assembly code

10: a = c [1];

004010678A4DF1movcl, byteptr [ebp-0Fh]

0040da-a884dfcmovbyteptr [ebp-4], cl

11: a = p [1];

00401_d8b55ecmovedx, dwordptr [ebp-14h]

004010708A4201moval, byteptr [edx + 1]

004010738845 FCmovbyteptr [ebp-4], al

The first method reads the elements in the string directly to the cl register, while the second method reads the pointer value to the edx.

Reading characters by edx is obviously slow.

2.7 summary:

The difference between stack and stack can be seen in the following metaphor:

Using Stacks is like eating at a restaurant, just ordering food (sending an application), paying for it, and eating (using it). If you are full, you can leave without worrying.

Cooking, washing, and other preparation work, as well as washing dishes, flushing pots and so on, has the advantage of being quick, but has little freedom.

Using heap is like making your favorite dishes. It is troublesome, but it suits your taste and has a high degree of freedom.

Self-summary:

Char * c1 = "abc"; in fact, a piece of memory is allocated in the text constant area to put "abc", and then an address is allocated to c1 on the stack and points

This address, and changing the constant "abc" will naturally crash.

However, char c2 [] = "abc", in fact, the memory allocated by abc is not the same as that of the upper user.

4199056

2293624 we can see that there are two parts. It is inferred that 4199056 is in the constant zone, and 2293624 is in the stack zone.

2293628

2293624

2293620 this output shows that the areas allocated by the three pointers are Stack zones, from high address to low address.

2293620 4199056 abc indicates that the compiler points c3 optimization to the "abc" of the constant zone"

Continue Thinking:

Code:

# Include <iostream>

Using namespace std;

Main ()

{

Char * c1 = "abc ";

Char c2 [] = "abc ";

Char * c3 = (char *) malloc (3 );

// * C3 = "abc" // error

Strcpy (c3, "abc ");

C3 [0] = 'G ';

Printf ("% d % s \ n", & c1, c1, c1 );

Printf ("% d % s \ n", & c2, c2, c2 );

Printf ("% d % s \ n", & c3, c3, c3 );

Getchar ();

}

Output:

2293628 4199056 abc

2293624 2293624 abc

2293620 4012976 gbc

Write comments, and subsequent changes will crash.

It can be seen that strcpy (c3, "abc"); abc is allocated in another place and can be changed, which is somewhat different from the reference document above.

* C3 = "abc ";

This sentence is inherently problematic and compilation is not successful.

C3 is a pointer variable and refers to the char type. You want to convert the content it refers to into a string, which is obviously incorrect.

C3 = "abc ";

So there is no problem.

But in this case, c3 stores the address of the String constant, so you cannot assign a value to the content indicated by c3: c3 [0] = 'G'; it is invalid.

In addition, when a function is on the stack, the parameter (arguments) is usually first pushed to the stack, and then the return address is


Author: sunshinewave

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.