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:

 

8, Making changes in Code segment

Will collapse under Q: code at run time, you can say why?  

#include<stdio.h>

int main(void)
{
char *ptr = "Linux";
*ptr = 'T';

printf("\n [%s] \n", ptr);

return 0;
}

This is because the A:, *ptr ='T', the code attempts to change the read-only memory for the string'Linux'. This of course not so would cause crashes.

Answer: This is because, through *ptr = ‘T’, the code is trying to change the first byte of the string ‘Linux’ kept in the code (or the read-only) segment in the memory. This operation is invalid and hence causes a seg-fault or a crash.

 

Comment on:

  It's hard to say whether this code must collapse (crash) or wrong(seg-fault).

  In the C standard, modified String literal (The program attempts to modify a string literal) is a kind of UB (undefined behavior), namely the undefined behavior. Use the words of popular, is the C language not stipulated that write meaningful. In this case, anything can happen, no matter what happens, is the error code, it is the programmer's responsibility.

  In fact, modify the String literal may lead to various outcomes, may also perform not resulting in the collapse or wrong in some cases.

  K&R believes that modify the String literal should be prescribed for unspecified behavior (unlike undefined behavior, unspecified behavior may have a variety of outcomes, but the code itself there is no semantic errors, while the undefined behavior is no semantic), but the standard committee did not adopt the K& amp; R's advice.

  Check out the original, it is not difficult to find the missing in the code segment.

  But the string literal stored in the code segment or read-only section, is not correct. This argument has no basis. The C language does not require the implementation should put string literal stored somewhere, it is to realize their own arrangements.

 

9, Process that changes its own name Q: you can write a program to change its name in its operation? (Can you write a program that changes its own name when run?)

The following A: code can be completed :

Answer: Following piece of code tries to do the required : 

#include<stdio.h>

int main(int argc, char *argv[])
{
int i = 0;
char buff[100];

memset(buff,0,sizeof(buff));

strncpy(buff, argv[0], sizeof(buff));
memset(argv[0],0,strlen(buff));

strncpy(argv[0], "NewName", 7);

// Simulate a wait. Check the process
// name at this point.
for(;i<0xffffffff;i++);

return 0;
}

 

Comment on:

  This question and answer more shocking.

  First, the title is to change the name of Process, the problem is Process name?

  But Question said to Program at run time name. If Program has a name, then it can only be the corresponding executable file name. Executable file name and the Process name is not equal to.

  Process is usually through PCB (Process Control Block) management, method of identification is usually ID, of course, PCB also has the so-called external identifier. If the external identifier PCB is interpreted as the name of the Process, although very far fetched, but there also seems to be unable to make any other explanation.

  Look, seem to have the concept of Process Name in some languages, but also the related function. But the C language does not have this concept.

  Look at the code, is nothing more than a modified string pointed to by argv[0]. Although the string pointed to by the argv[0] is the name of the program, but the change of no practical significance of — — next time you start the program had to use the original name, because the executable file name or did not change.

  Notes in the code and referred to the Process Name, so a Process Name, then Program Name, the designer himself was unclear concept.

  Note that the simulation waiting here, wondering what to wait here. Seem is affectation.

  Look at the code style.  

int i = 0;
  //……
for(;i<0xffffffff;i++);

  The for statement is divided into two parts, writing style, perverse.

  The empty statement in the loop body part of writing at the end of the line, is also a kind of habit. Compare the following notation will know: 

for ( i = 0 ; i <0xffffffff ; i ++ )
;

  The semantics of for statement itself under certain conditions is also a problem. If the maximum value of type int to 0x7fffffff, when the I value reaches int type maximum value, I and self will overflow. Overflow, is an undefined behavior(UB).

  That is to say, the statement said it may not have I values from 0 successive plus 1 has been changed to 0xffffffff-1. What is the author of code in mind, maybe he doesn't know. By the way, the maximum value of type int is 0x7fffffff, 0xFFFFFFFF is not int, but unsigned int types.

char buff[100];

memset(buff,0,sizeof(buff));

strncpy(buff, argv[0], sizeof(buff));
memset(argv[0],0,strlen(buff));

strncpy(argv[0], "NewName", 7);

  This code, is still very stupid. First, be rather baffling to buff 0:  

  memset(buff,0,sizeof(buff));

  In fact, remove it without any problems. Because behind 

  strncpy(buff, argv[0], sizeof(buff));

  Again filled  For example, the following code snippet 

char s[4];
strncpy( s , "1" , 4);
printf("%d %d %d %d\n",s[0],s[1],s[2],s[3]);

Output, must be 

49 0 0 0

  This shows that, memset the original code (0) filled with meaningless, because then have to fill a. I'm not saying memset () is useless, but most of the memset up to now I see () is muddleheaded in vain, is in the abuse of memset (). Again a lot of use memset () those who do not know their own code what do. The so-called “ large commercial program, this is a good code style of ” and so on, is poppycock.

  The back of a sentence

memset(argv[0],0,strlen(buff));

Not only useless, logic is also a bit absurd. Should

memset(argv[0],0,strlen(argv[0]));

 

 

Only reasonable. Although we know, where strlen (argv[0]) and strlen (buff) is the same value.  

strncpy(argv[0], "NewName", 7);

  This sentence is smatter, make such a fuss about. The author uses strncpy () I hope it won't copy out of bounds, but the 7 is "NewName" length, rather than the length of the string pointed to by argv[0]. If strlen (argv[0]) is less than 7, it will happen out of bounds.  

  In summary, this code is not only impossible to change the Program Name, even correctly modify command-line parameters are not changed, not even Process Name.   

  So, the question of “ ”, is false a curry favour by claptrap. “ a ” is a drag, make over building solutions.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Connie at November 13, 2013 - 4:28 PM