{
P1=(char *) malloc (100);
}
Void fun2 ()
{
Char * p2=nullptr;
Fun1 (p2);
Strcpy (p2, "wind up xi clouds float in the sky");
The printf (p2);
}
Int main ()
{
Fun2 ();
return 0;
}
CodePudding user response:
Is sure to crash,There are two other
The first:
Void fun1 (char * & amp; (p1)
The second
Void fun1 (char * * (p1)
CodePudding user response:
Collapse, change the pointer value parameter, but didn't change the pointer value of the argument, and assign nullptr collapsesCodePudding user response:
Please refer to the comments:Void fun1 (char * (p1)
{
P1=(char *) malloc (100);//memory leak will happen here, in addition, the parameter variables p1 and local variables, so you don't output
}
Void fun2 ()
{
Char * p2=nullptr;
Fun1 (p2);//after perform this function, the p2 is still pointing nullptr
Strcpy (p2, "wind up xi clouds float in the sky");//do to nullptr address string copy, so the crash
The printf (p2);//crash has occurred, the p2 address is still pointing nullptr
}
Int main ()
{
Fun2 ();
return 0;
}
=================================================================================
The right to modify the following:
Void fun1 (char * * (p1)//in order to output address, need to use the secondary pointer
{
* p=(char *) malloc (100);//plus * is to save the address of the malloc to address
}
Void fun2 ()
{
Char * p2=nullptr;
Fun1 (& amp; P2);//holds the address of passed as a parameter
Strcpy (p2, "wind up xi clouds float in the sky");
The printf (p2);
Free (p2);//in order to prevent a memory leak, there needs to be free, please good coding habits oh.
}
Int main ()
{
Fun2 ();
return 0;
}
CodePudding user response:
You this kind of behavior, is similar to the reality, at the intersection of traffic through a red light, one is not killed, two is not killed, as through a red light is safe.Everything has its rules, while occasionally don't obey the rules will not necessarily be punished, but can't think don't keep it's natural rule
CodePudding user response:
Looks like said: a virtual pointer is assigned the address space, so make a mistake; If only 2 statement pointer, and don't assign to nullptr, so they make mistakes?CodePudding user response:
This shouldn't be a compiler errorCodePudding user response:
Value of C is essentially "pass"Char * p1 is p1, pointing to the list of address
Char * * p1 is the address of the p1 itself
CodePudding user response:
?????????CodePudding user response:
Please refer to "function parameter type of pointer in c + + pointer"Wish I could help you!
CodePudding user response:
Value of C is essentially "pass"Char * p1 is p1, pointing to the list of addresses// the value of "pass"
Char * * p1 is the address of the p1 itself//" value "
CodePudding user response:
Simple positive - Soft solution on the second floorCodePudding user response:
I am a little white, squatting a great godCodePudding user response:
Why do they don't have to type string string?CodePudding user response:
Will appear as mentioned in the second floor and the fourth floor, but the most fundamental I would recommendYou understand value, preach Pointers and references to the difference between
This programming to yourself can make a big help, later met and problems can also be spread fast positioning
CodePudding user response:
Learning, seem to remember the memory in several areas, some return will have unpredictableCodePudding user response:
Learning how to not snow backCodePudding user response:
Generally in such must be func (char * * p) or func (char * & amp; p)CodePudding user response:
Compile error, look againCodePudding user response:
Learning to learnCodePudding user response:
Learning, bosses is requestedCodePudding user response:
Value of C is essentially "pass"Char * p1 is p1, pointing to the list of address
Char * * p1 is the address of the p1 itself
CodePudding user response:
Good good study!!!!!!!!!!!!!!!!!!!!!!!CodePudding user response:
Firm involved in the relationship between the parameter value is a one-way transmissionCodePudding user response:
123CodePudding user response:
Efforts to learn c + +!!!!!CodePudding user response:
Learn to learn