本文针对c语言初学者总是出bug的萌新

有编译器反馈的

读反馈

这里来解释一下,方便大家理解

编译器输出:
        In function 'main':
        13:43: error: expected ';' before 'scanf'
        printf("Enter number and id for s2: ")
                                                                  ^

                                                                  ;
14    scanf("%d %d", &s2.number, &s2.id);
        ~~~~~

in function‘main’ 代表着是函数main里面的问题,当我们函数比较多的时候可以通过in function‘什么’快速锁定是哪个函数出的问题,尤其是在函数分装后(也就是函数所在文本和主函数所在文本不同时)也可以快速锁定出错位置。

c:13:43 代表着是第13行43列(列不重要)出现了问题,不同编译器给出出错的细节不太相同,但基本都在所给错误行附近几行。后面就是更人性化的帮助我们纠正了错误(少了;),这里也可以看出c语言是从上到下执行,因为他没有给printf那一句打上无法执行,反而是下一句scanf无法执行,揭示了c语言编译的底层基本逻辑。

关于其他反馈,会英文能读懂编译器报错基本都能解决。

没有编译器反馈的

相较于有反馈的,这种就是很难解决,往往都是内存溢出了,主要就是在指针、循环、链表、内存分配、数组会出现相关问题。

内存问题

int main() {
    int* arr = (int*)malloc(sizeof(int) * 1000);
    //...
    free(arr);
    return 0;
}

堆内存溢出:

当使用malloc、calloc或realloc等函数动态分配内存时,如果分配的内存超过了可用的堆空间,就会导致堆内存溢出。

再看一个字符串的例子

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *divString(char *src)
{
   char *s2;
   s2=(char*)malloc(strlen(src)*sizeof(char)+1);
   int j=0;
   int k=0;
   int i=0;
   while(src[i]!='\0')
   {
      if(src[i]>='a' && src[i]<='z')
         s2[j++] = src[i];
      else if(src[i]>='1' && src[i]<='Z')
         src[k++] = src[i];

      i++;
         
   }
   src[k]='\0';
   s2[j] ='\0';

   return s2;
}

int main( )
{
   char s1[]="abc123ABCdDEFef";
   char *s2;
   s2 = divString(s1);
   printf("%s\n",s1);
   printf("%s",s2);
   
   return 0;
}

这个的功能是将给定字符串分解成两个字符串。其中小写字母在一个,数字和大写的再另一个。

请注意src[k]='\0' s2[j]='\0',如果没有会发生什么呢?

可以看见输出了很多不该输出的,这是因为

数组(字符串)未定义结尾

在 C 语言中,字符串必须以空字符(‘\0’)结尾。如果 s2 没有以空字符结尾,那么当你尝试使用 printf 或其他字符串函数时,它们将继续读取内存直到遇到一个空字符。这可能导致打印出垃圾数据或者引发运行时错误,并且会污染s1的数据。

(链表)指针乱指

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* next;
} Node;

Node* createNode(int value) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    if (newNode == NULL) {
        printf("Memory allocation failed.\n");
        exit(1);
    }
    newNode->data = value;
    newNode->next = NULL;
    return newNode;
}

void appendNode(Node** head, int value) {
    Node* newNode = createNode(value);
    if (*head == NULL) {
        *head = newNode;
    } else {
        Node* current = *head;
        // 错误:没有检查 current->next 是否为 NULL
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = newNode;
    }
}

int main() {
    Node* head = NULL;
    
    // 正确添加节点
    appendNode(&head, 1);
    appendNode(&head, 2);
    appendNode(&head, 3);

    // 故意制造一个错误,让链表中的某个节点的 next 指针指向自己
    // 这将导致无限循环和潜在的内存溢出
    Node* current = head;
    while (current->next != NULL) {
        current = current->next;
    }
    current->next = current; // 错误操作:指针指向自己

    // 尝试打印链表,这将导致无限循环
    current = head;
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
        // 此处不会终止,因为 current->next 指向了 current
    }
    printf("\n");

    // 释放链表内存的代码被省略了,因为上面的循环永远不会结束

    return 0;
}

以下是正确版本(让大家提前了解一下链表)

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* next;
} Node;

Node* createNode(int value) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    if (newNode == NULL) {
        printf("Memory allocation failed.\n");
        exit(1);
    }
    newNode->data = value;
    newNode->next = NULL;
    return newNode;
}

void appendNode(Node** head, int value) {
    Node* newNode = createNode(value);
    if (*head == NULL) {
        *head = newNode;
    } else {
        Node* current = *head;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = newNode;
    }
}

void freeList(Node* head) {
    Node* current = head;
    Node* next;
    while (current != NULL) {
        next = current->next;
        free(current);
        current = next;
    }
}

int main() {
    Node* head = NULL;
    
    // 正确添加节点
    appendNode(&head, 1);
    appendNode(&head, 2);
    appendNode(&head, 3);

    // 遍历并打印链表
    Node* current = head;
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }
    printf("\n");

    // 释放链表内存
    freeList(head);

    return 0;
}

死循环(也是一种爆内存的方式)

这是因为没有循环截止方式或者无效截止(0肯定大于-1,i++后也一定,没有限定)

细节问题

主要就是试错,很多都是要通过极端值试出来。

因为没有对b=0定义,所以卡死,需要补充定义。


总结

以上都是萌新会出现的问题,当你的代码“死掉”后,一定要回头细想有没有出现上述问题,建议反复抄写熟稔于心。(不是)

以上都是本人理解还有本人犯过的错误,各位萌新不要着急,都是一步一步来的,欢迎各位大佬补充~

这是一个系列,主要针对我个人学习过程中出现过的错误,以后肯定会更新(不出bug那真是神人了)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值