12 a funny C language interview questions -- "12 interesting C language question

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
The above link:
(I didn't save, encounter overheat protection. At least 4 Q & a comment saying. Silence.)

2, main() The return type of the method
Q: does this code will compile? If so, then in this piece of code containing what problem?
#include<stdio.h>

void main(void)
{
    char *ptr = (char*)malloc(10);

    if(NULL == ptr)
    {
        printf("\n Malloc failed \n");
        return;
    }
    else
    {
        // Do some processing

        free(ptr);
    }

    return;
}


The answer is the A: code can be compiled, but would leave for the main () method returns the type of warning. Main () to the return type of the method should be'int'instead of'void'. This is because the'int'return type can make the program returns a status value. Especially when the procedure as an affiliated program for other applications when the state values will be more important.
Answer: The code will compile error free but with a warning (by most compilers) regarding the return type of main()function. Return type of main() should be ‘int’ rather than ‘void’. This is because the ‘int’ return type lets the program to return a status value. This becomes important especially when the program is being run as a part of a script which relies on the success of the program execution.

Comment on:

  It's hard to say whether this code through the compiler, because the void main (void) the basic form is not specified in the standard C program. Indeed, in many compilers this code can be “ in &rdquo through the compiler, however;
Can not guarantee that all can pass in all compiler. The C language is not given by the compiler commitment, through the compiler can only be understood as compiler to drain.
  Wrote here, what has this meaning (“ by most compilers”), but he is Jerry, distorted the original meaning.
  Under the style:
  In the code to write a if statement is a return statement, strange words if and else words no, two return statements kicked the door kicked in the door. The following two kinds of writing is much better:
int main(void)
{
    char *ptr = (char*)malloc(10);

    if( NULL == ptr)
    {
        printf("\n Malloc failed \n");
        return 1;
    }

    // Do some processing
    free(ptr);

    return 0;

}


int main(void)
{
    char *ptr = (char*)malloc(10);

    if(NULL == ptr)
    {
        printf("\n Malloc failed \n");
        return 1;
    }
    else
    {
        // Do some processing

        free(ptr);
        return 0;
    }
}



3, Memory leak
The following code Q: will have a memory leak it?
#include<stdio.h>

void main(void)
{
    char *ptr = (char*)malloc(10);

    if(NULL == ptr)
    {
        printf("\n Malloc failed \n");
        return;
    }
    else
    {
        // Do some processing
    }

    return;
}


A: well, although the above code without releasing memory pointer to PTR, but actually even the end of program will not cause memory leaks, because when the program has finished all the beginning occupied memory on all empty. But if the code above is a serious problem in that will while loops.
Answer: Well, Though the above code is not freeing up the memory allocated to ‘ptr’ but still this would not cause a memory leak as after the processing is done the program exits. Since the program terminates so all the memory allocated by the program is automatically freed as part of cleanup. But if the above code was all inside a while loop then this would have caused serious memory leaks.
Note: if you need to know more about the memory leak problem and how to use the tools to detect memory leaks, you can refer to this article Valgrind .

Comment on:

  This question and answer the first problem is, main () function is not standard. Before a question and answer in the void main (void) are negative, the answer is you write this, since the palm mouth.
  Admittedly, this code does not have a memory leak, but the code itself is not free, but this activity by the programmer to the system, it is very undesirable. Although there is no memory leaks, but free should write.
  Another problem is that the return statement in the code kicked the door kicked in the door of the problem, said before, no longer.

4, free() Method
The following Q: code will collapse when the user input'freeze', and if the input'zebra' is operating normally, this is why?
#include<stdio.h>

int main(int argc, char *argv[])
{
    char *ptr = (char*)malloc(10);

    if(NULL == ptr)
    {
        printf("\n Malloc failed \n");
        return -1;
    }
    else if(argc == 1)
    {
        printf("\n Usage  \n");
    }
    else
    {
        memset(ptr, 0, 10);

        strncpy(ptr, argv[1], 9);

        while(*ptr != 'z')
        {
            if(*ptr == '')
                break;
            else
                ptr++;
        }

        if(*ptr == 'z')
        {
            printf("\n String contains 'z'\n");
            // Do some more processing
        }

       free(ptr);
    }

    return 0;
}


The cause of A: problem is because the code to change the PTR pointer to the address in the while cycle. When the input is'zebra', while even in the implementation of the first time before the end, so the free (release) memory address is malloc (start) distribution. But when the input'freeze', PTR records the address has been changed in the while loop, because it will be the wrong address is passed to the free () method in the crash.
Answer: The problem here is that the code changes the address in ‘ptr’ (by incrementing the ‘ptr’) inside the while loop. Now when ‘zebra’ is supplied as input, the while loop terminates before executing even once and so the argument passed to free() is the same address as given by malloc(). But in case of ‘freeze’ the address held by ptr is updated inside the while loop and hence incorrect address is passed to free() which causes the seg-fault or crash.

Comment on:

  Explained in Answer no problem. But the more important reason is to put all the code is written in main (in). If the
        memset(ptr, 0, 10);

        strncpy(ptr, argv[1], 9);

        while(*ptr != 'z')
        {
            if(*ptr == '')
                break;
            else
                ptr++;
        }

        if(*ptr == 'z')
        {
            printf("\n String contains 'z'\n");
            // Do some more processing
        }

Part of the function is completed by a function, it will not cause this problem.
  The translation of a lot of problems:
  “Change the PTR pointer to the address ” in fact, to change the PTR address (storage).
  “Because it will be the wrong address is passed to the free (&rdquo) caused by the collapse method;, that's simply not Chinese words, in addition to leak over“seg-fault”.
  Style remains the return statement kicked the door kicked in the door. In fact, the presence of multiple return statements, if-else as many if often seems more relaxed.
return -1;

Non standard, should be the compiler dialect.
memset(ptr, 0, 10);
A word, long winded, confused. But in this case, because it is through the malloc () for the memory, so when strlen (argv[1]) is greater than or equal to 9, memset (PTR, 0, 10); will play set in advance for the effect of 一句, 啰嗦, 头脑不清. 但在这个例子里, 由于是通过malloc()申请的内存, 所以当strlen(argv[1])大于等于9时, memset(ptr, 0, 10);会起到提前设置ptr[9]为\0的效果. 但要实现这点其实简单地 ptr[9]. But to achieve this simple fact.
ptr[9]='\0'
Can, need not have mobilized their forces to call memset () function.
  If the application for each data object in memory of 0, actually can through
char *ptr = (char*)calloc(10,1);
Simple to implement.
  And malloc (calloc) (different) initialization data application in memory 0. Moreover, if is the application space as an array, calloc (malloc) () is more than a little more formally.
(to be continued)
The link
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Z at November 16, 2013 - 12:39 PM