Home > Back-end >  Execution of the function
Execution of the function

Time:09-25

# include "stdio.h"
# include "string. H"
H # include "Windows."
Void GetMemory (char * cpStr)
{
CpStr=new char [100].
return;
}

Void Test (void)
{
Char * cp_str=NULL;
GetMemory (cp_str);
Strcpy (cp_str, "Hello, world!" );
The printf (cp_str);

return;
}

Void main ()
{
The Test ();
}

Excuse me program of what would happen? Causes of the situation and detailed explanation?

CodePudding user response:

Void GetMemory (char * & amp;
cpStr){
CpStr=new char [100].
return;
}

CodePudding user response:

Test function (char *) cp_str means GetMemory function arguments and GetMemory function parameter (char *) cpStr are two different variables, they are the values of (pass the NULL argument to the parameter cpStr), then the parameter cpStr got allocated buffer first address (GetMemory function returns cpStr variable invalid buffer's lost cause memory leaks), did not affect the arguments cp_str (NULL), then a strcpy (cp_str, "Hello, world!" ); Failure,

The GetMemory function parameter to reference, void GetMemory (char * & amp; CpStr), other unchanged,

CodePudding user response:

There will be a memory leak, the reason is you only allocates memory and not release it,

CodePudding user response:

Program will collapse because of 2, 3 floor have been given, as to modify, in view of the fact that your program is a classic C header file reference form, rather than the C + +, your compiler may not be able to meet reference grammar, you changed the value to reference
  • Related