Highlights of C language problems

Source: Internet
Author: User

[Reference Source: http://www.csdn.net/article/2012-09-06/2809604-12-c-interview-questions]

1. Gets () function:
The gets () function receives a string from stdin without checking the volume of the cache it copies. This may cause cache overflow. We recommend that you use the standard function fgets () instead.

2. line feed:
Change Behavior in the window operating system '\ r \ n', and in Linux and UNIX operating systems' \ N'

3. strcpy () function:
When using strcpy (), check whether the capacity of the receiving variable is sufficient. We recommend using strncpy () or memcpy ().

4. Return Value of main () function:
The return type of main () should be "int" rather than "Void", because "int" will let the program return the status value. This is very important, especially when a program is running as part of a script dependent on the successful running of the program.

5. Memory leakage:
When dynamically applying for memory in the program module, it must be released immediately when it is used up. Although it will be released automatically when the program ends, if the applied memory is in the while statement, this will cause serious memory leakage.

6. Free error:
The pointer value is modified in the program, leading to an error in the address passed to free (), resulting in seg-fault or crash.

7. atexit () function:
If you use atexit (), you should use exit () or "return" in combination with it, instead of using the _ exit () function.

8. functions that can accept any type of parameters and return interger (integer) results:
Int func (void * PTR)

9. operators:
"++" And "*" have the same priority. Therefore, "* PTR ++" is equivalent to "* (PTR ++ )". That is, you should first execute PTR ++, and then * PTR

10. troubleshooting:
Char * PTR = "Linux"; * PTR = 'T ';
* PTR = 'T' will change the first letter of the code segment (read-only code) "Linux" in the memory. This operation is invalid and may cause seg-fault or crash.

11. The process will change its name:
# 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 <0 xffffffff; I ++ );

Return 0;
}

12. Return the address of the local variable:
# Include <stdio. h>

Int * Inc (INT Val)
{
Int A = val;
A ++;
Return &;
}

Int main (void)
{
Int A = 10;
Int * val = Inc ();
Printf ("\ n incremented value is equal to [% d] \ n", * val );

Return 0;
}
Although the above programs sometimes run normally, there are serious vulnerabilities in "Inc. This function returns the address of the local variable. Because the life cycle of the local variable is the life cycle of "Inc ()", it is difficult to use the local variable after Inc ends. This can be avoided by the address of the variable "A" in main (), so that the stored value of this address can be modified later.
Printf function:

 

13. function parameters in C language are processed from right to left by default, and output is from left to right.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.