Home > Back-end >  A job interview questions, solving?
A job interview questions, solving?

Time:09-27

What function run results and explain why
Void malloc (char * p)
{
P=(char *) malloc (100);
}

Int main ()
{
Char * p=NULL;
Malloc (p);
If (P!=NULL)
{
Cout & lt; <"Ok" & lt; }
The else
{
Cout & lt; <"Error" & lt; }
}

CodePudding user response:

The output error, if this is the malloc (& amp; P); Output, then ok, want to look for the preach to participate as address or content,,,

CodePudding user response:

Establish a char type of memory space, succeeded is ok is not successful, it error

CodePudding user response:

May output ok may output error, because malloc (p) is applied to memory for a period of address space, not necessarily will be successful, if successful it outputs ok, failure is the output error

CodePudding user response:

Should be compiled by not.
Void malloc char * p
{
P=(char *) malloc 100);
}

CodePudding user response:

As DelphiGuy said, the compiler will go wrong, but the error is not malloc,

Void malloc (char * p)//name the same as the library function, it is not a good behavior,
{
//malloc zhangs function, if you have # include & lt; Stdlib. H> Here is on the basis of overloading rules, matching the right function,
P=(char *) malloc (100);
//allocated memory is not released, function after the address will be lost, cause memory cannot be released, that's the beauty of memory leaks,
}

Int main ()
{
Char * p=NULL;
Malloc (p);//this is called, p will not be changed, is still NULL,
If (P!=NULL)//the uppercase P didn't define not lowercase P ah. A compiler error,
{
Cout & lt; <"Ok" & lt; }
The else
{
Cout & lt; <"Error" & lt; }
}
As questions, much attention should be paid to where, the first by the naked eye, to see all the compiler error, then consider logic errors and run-time errors,

CodePudding user response:

Neglect one place: int main () function has no return value, it will cause a warning, its running result is the function will return the value of an uninitialized (ambiguity)
Don't test is such, don't want to be too easy to be got, fortunately, a student has in the past,