Learn how to use malloc and assign values to pointers

Source: Internet
Author: User

First read oneProgramIn fact, this is a piece of a program I compiled. I wrote out the problems I encountered:

# Include <stdio. h>

Int main (void)
{
Char *;
// A = (char *) malloc (20 );
A = gets ();
// Scanf ("% s", );
// Getchar ();
Puts ();
Getchar ();
Return 0;
}

This is a program that I debug in windows. It uses Dev C ++ 4.9. The compilation is not problematic. During the runtime, the system will directly discover the specific garbage interface of windows. When the project 1.exe encounters a problem, it needs to be closed. We are sorry for the inconvenience .", You shut him down.

Analysis: in fact, when we get this program to GDB under Linux, we can know that when we run it to gets, we will tell you segments fault. It is very simple that there is a problem with the data segment used by the program, cplus has actually been used, that is, do not assign values to pointers without initialization, and the segment is overwritten because the pointer does not have a clear address when it is not initialized, if you assign values directly, the pointer address conflicts with the addresses occupied by other programs, and a segment error occurs.

Solution:

Convert the pointer into an array, and change * A to a []. I don't like to use arrays, so I use the second method to assign an address to the pointer:

Malloc can easily solve this problem.

I would like to add another note today:

Malloc is a dynamic allocation, the address is not fixed, and the array is fixed during definition and initialization. You can look at a program:

# Include <stdio. h>
# Include <malloc. h>
Int main (void)
{
Char * P = "ASDFASDF ";
Char * q;
Q = (char *) malloc (20 );
Q = P;
Printf ("% s/n", P );
}

The P address can still be assigned to Q. Even if there is no malloc allocation, we should know that the pointer address is actually dynamic during initialization, such as defining and changing the address:

# Include <stdio. h>
# Include <malloc. h>
Int main (void)
{
Char * P = "ASDFASDF ";
Char * q = "ddddddddd ";
// Q = (char *) malloc (20 );
P = Q;
Printf ("% s/n", P );
}
Still.

But the array won't work, because its address is fixed when it starts to declare and assign values, and cannot be changed.

# Include <stdio. h>
# Include <malloc. h>
Int main (void)
{
Char P [20] = "ASDFASDF ";
Char Q [20];
P = Q;
Printf ("% s/n", P );
}

An error is reported during compilation. D:/hehe/Zhi. cpp (8): Error c2106: '=': left operand must be L-value.

Just remember.

However, char * P can be directly defined as a value, such as char * P = "ASDF" or char * P; P = "ASDF "; this is actually a binding method, dynamic.

But the following won't work. char * P; scanf ("% s", P); because the position of P is uncertain, it must be in scanf ("% s", P ); before malloc or new.

Char-type pointers can be directly given to strings, but int-type pointers are not allowed. For example, int * P = 123; that's not the case. In this case, int * P; then malloc or new is the next fixed address, and then * P = 123.

The Int value is * P = 123, while the char type is P = "ASDF" for the first address.

 

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.