C 语言编程中的 5 个常见错误及对应解决方案
扫描二维码
随时随地手机看文章
https://linux.cn/article-13894-1.html
作者:Jim Hall
译者:unigeorge
即使是最好的程序员也无法完全避免错误。这些错误可能会引入安全漏洞、导致程序崩溃或产生意外操作,具体影响要取决于程序的运行逻辑。
- #include <stdio.h>
- #include <stdlib.h>
- int
- main()
- {
- int i, j, k;
- int numbers[5];
- int *array;
- puts("These variables are not initialized:");
- printf(" i = %d\n", i);
- printf(" j = %d\n", j);
- printf(" k = %d\n", k);
- puts("This array is not initialized:");
- for (i = 0; i < 5; i ) {
- printf(" numbers[%d] = %d\n", i, numbers[i]);
- }
- puts("malloc an array ...");
- array = malloc(sizeof(int) * 5);
- if (array) {
- puts("This malloc'ed array is not initialized:");
- for (i = 0; i < 5; i ) {
- printf(" array[%d] = %d\n", i, array[i]);
- }
- free(array);
- }
- /* done */
- puts("Ok");
- return 0;
- }
- These variables are not initialized:
- i = 0
- j = 0
- k = 32766
- This array is not initialized:
- numbers[0] = 0
- numbers[1] = 0
- numbers[2] = 4199024
- numbers[3] = 0
- numbers[4] = 0
- malloc an array ...
- This malloc'ed array is not initialized:
- array[0] = 0
- array[1] = 0
- array[2] = 0
- array[3] = 0
- array[4] = 0
- Ok
- These variables are not initialized:
- i = 0
- j = 1074
- k = 3120
- This array is not initialized:
- numbers[0] = 3106
- numbers[1] = 1224
- numbers[2] = 784
- numbers[3] = 2926
- numbers[4] = 1224
- malloc an array ...
- This malloc'ed array is not initialized:
- array[0] = 3136
- array[1] = 3136
- array[2] = 14499
- array[3] = -5886
- array[4] = 219
- Ok
- #include <stdio.h>
- #include <stdlib.h>
- int
- main()
- {
- int i;
- int numbers[5];
- int *array;
- /* test 1 */
- puts("This array has five elements (0 to 4)");
- /* initalize the array */
- for (i = 0; i < 5; i ) {
- numbers[i] = i;
- }
- /* oops, this goes beyond the array bounds: */
- for (i = 0; i < 10; i ) {
- printf(" numbers[%d] = %d\n", i, numbers[i]);
- }
- /* test 2 */
- puts("malloc an array ...");
- array = malloc(sizeof(int) * 5);
- if (array) {
- puts("This malloc'ed array also has five elements (0 to 4)");
- /* initalize the array */
- for (i = 0; i < 5; i ) {
- array[i] = i;
- }
- /* oops, this goes beyond the array bounds: */
- for (i = 0; i < 10; i ) {
- printf(" array[%d] = %d\n", i, array[i]);
- }
- free(array);
- }
- /* done */
- puts("Ok");
- return 0;
- }
- This array has five elements (0 to 4)
- numbers[0] = 0
- numbers[1] = 1
- numbers[2] = 2
- numbers[3] = 3
- numbers[4] = 4
- numbers[5] = 0
- numbers[6] = 4198512
- numbers[7] = 0
- numbers[8] = 1326609712
- numbers[9] = 32764
- malloc an array ...
- This malloc'ed array also has five elements (0 to 4)
- array[0] = 0
- array[1] = 1
- array[2] = 2
- array[3] = 3
- array[4] = 4
- array[5] = 0
- array[6] = 133441
- array[7] = 0
- array[8] = 0
- array[9] = 0
- Ok
- #include <stdio.h>
- #include <string.h>
- int
- main()
- {
- char name[10]; /* Such as "Chicago" */
- int var1 = 1, var2 = 2;
- /* show initial values */
- printf("var1 = %d; var2 = %d\n", var1, var2);
- /* this is bad .. please don't use gets */
- puts("Where do you live?");
- gets(name);
- /* show ending values */
- printf("<%s> is length %d\n", name, strlen(name));
- printf("var1 = %d; var2 = %d\n", var1, var2);
- /* done */
- puts("Ok");
- return 0;
- }
- var1 = 1; var2 = 2
- Where do you live?
- Raleigh
- <Raleigh> is length 7
- var1 = 1; var2 = 2
- Ok
- var1 = 1; var2 = 2
- Where do you live?
- Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch
- <Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch> is length 58
- var1 = 2036821625; var2 = 2003266668
- Ok
- Segmentation fault (core dumped)
- #include <stdio.h>
- #include <stdlib.h>
- int
- main()
- {
- int *array;
- puts("malloc an array ...");
- array = malloc(sizeof(int) * 5);
- if (array) {
- puts("malloc succeeded");
- puts("Free the array...");
- free(array);
- }
- puts("Free the array...");
- free(array);
- puts("Ok");
- }
- malloc an array ...
- malloc succeeded
- Free the array...
- Free the array...
- free(): double free detected in tcache 2
- Aborted (core dumped)
- #include <stdio.h>
- int
- main()
- {
- FILE *pfile;
- int ch;
- puts("Open the FILE.TXT file ...");
- pfile = fopen("FILE.TXT", "r");
- /* you should check if the file pointer is valid, but we skipped that */
- puts("Now display the contents of FILE.TXT ...");
- while ((ch = fgetc(pfile)) != EOF) {
- printf("<%c>", ch);
- }
- fclose(pfile);
- /* done */
- puts("Ok");
- return 0;
- }
- Open the FILE.TXT file ...
- Now display the contents of FILE.TXT ...
- Segmentation fault (core dumped)