当我向 main 添加 for 循环时,出现意外位置的分段错误

问题描述 投票:0回答:1

我在一个名为 Practice_3.c 的 C 文件中编写了下面的代码来练习我的 C,但我对 C 很生疏。顶部有标准输入、一些函数声明/定义和一些结构。现在,如果您注意到代码的最底部,我有一个基本上不执行任何相关操作的 for 循环(或者至少我是这么认为的)。然而,通过这里的 for 循环,这是我的输出。

type herHello, World
 b is now: 5

The 0th element of the array is: 0.
The 1th element of the array is: 1.
The 2th element of the array is: 2.
The 3th element of the array is: 3.
The 4th element of the array is: 4.
The 5th element of the array is: 5.
The 6th element of the array is: 6.
The 7th element of the array is: 7.
The 8th element of the array is: 8.
The 9th element of the array is: 9.
3
Value is: 13

Value is: 100
zsh: segmentation fault  ./Practice_3e

现在,如果我删除 for 循环并只留下 print 语句,程序将正常运行,但由于这个 for 循环位于底部(我什至尝试注释掉内部的 print 语句),我得到了这个分段错误。更让我困惑的是,如果您查看分段错误在哪里,它会发生在打印语句“Value is:...”之后。现在,当我删除底部的 for 循环时,输出变为以下内容:

tHello, World
 b is now: 5

The 0th element of the array is: 0.
The 1th element of the array is: 1.
The 2th element of the array is: 2.
The 3th element of the array is: 3.
The 4th element of the array is: 4.
The 5th element of the array is: 5.
The 6th element of the array is: 6.
The 7th element of the array is: 7.
The 8th element of the array is: 8.
The 9th element of the array is: 9.
3
Value is: 13

d

0
The y coordinate of point1 is: -1
The coordinates for point4 are 7 and 12
For this one our coordinates are 10
The bottom left of our rectangle is 38
Arr of 0 is: -4.

The 0th element of arrAgain is: -8
The 1th element of arrAgain is: -3
The 2th element of arrAgain is: 0
The 3th element of arrAgain is: 4
The 4th element of arrAgain is: 7
The 5th element of arrAgain is: 11
The 6th element of arrAgain is: 12
The 7th element of ype here

请注意,1)在下面打印“值是:...”的部分中给出我的代码,代码的正确工作仅打印“值是:13”,并且之后不会打印“值是:100”,如下所示不满足在该循环中继续的条件。 2) 该代码区域不靠近导致此问题的代码行,即底部的 FOR 循环。它不是导致此问题的其他 for 循环之一,而只是那个 for 循环。也就是说,如果我将该循环复制/粘贴到 main 中的其他位置,错误仍然存在,但是如果我删除该循环并放入不同的语句(例如 print 语句),则不会出现分段错误。

我无法弄清楚为什么 main 中的孤立 for 循环会导致段错误并在 main 的较早且不相关的部分中导致设置错误。为什么添加带有“垃圾”/“hello world”类型内容的循环会导致“值是:...”/节点结构给我一个段错误。

这太奇怪了。我已经重新启动了 VS-Code,我已经检查了我的编译器(gcc),但是只要 main 中有一个 for 循环,就会出现这个问题。我的非理性思维几乎想知道我是否达到了 main 中允许的 for 循环数量的上限,从而导致了此错误。我非常困惑为什么会出现这种情况,这些代码区域之间的交互以及......好吧,关于这一切。

这是代码。只有这个文件,没有链接文件或头文件或任何东西。如果有人有任何想法请告诉我。

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

struct node {
    int val;
    struct node* left;
    struct node* right;
};

void swap(int* x, int* y){
    int temp = *y;
    *y = *x;
    *x = temp;
}

int modular(int base, int val){
    if(val < 0){
        return modular(base, -val);
    }
    else if(val < base){
        return val;
    }
    else {
        return modular(base, val-base);
    }
}

struct point {
    int x;
    int y;
} point1, point2; 

struct point makePoint(int x, int y){
    struct point temp;
    temp.x = x;
    temp.y=y;
    return temp; 
}

struct rect {
    struct point pt1;
    struct point pt2;
} myRect; 

void insertionSort(int* head, int arrLength){
    for(int i = 0; i < arrLength; i++){
        for(int j = i+1; j < arrLength; j++){
            //printf("Here we have %d\n", *(head+j));
            if(*(head + j) < *(head + i)){
                int temp = *(head + j);
                *(head + j) = *(head + i);
                *(head + i) = temp;
            }
        }
        //printf("We've reached here...");   
    }
    return;
}

int main(){
    printf("Hello, World");
    int a = 5;
    int b = 3;
    swap(&a, &b);
    printf("\n b is now: %d\n", b);

    int arr[10];
    int l = sizeof(arr)/sizeof(arr[0]);
    for(int i = 0; i < l; i++) {
        arr[i] = i;
    }

    for(int i = 0; i < l; i++){
        printf("\nThe %dth element of the array is: %d.", i, *(arr+i));
    }
    //printf("The 2nd element in the array is: %d", (*arr+1));

    int i1 = 3;
    int* px = &i1;
    printf("\n%d", *px);

    struct node root; 
    root.val = 13;
    struct node left1;
    struct node left2;
    left1.val = 100;
    left2.val = 2;
    root.left = &left1;
    root.right = &left2;

    struct node curr = root;
    while(curr.left != NULL){
        printf("\nValue is: %d\n", curr.val);
        curr = *curr.left;
    }

    char s[] = "Hi, my name is Aaron";
    printf("\n%c\n", s[16]+3);

    int one = modular(8, 104);
    printf("\n%d\n", one);

    point1.x = 1;
    point1.y = -1;
    printf("The y coordinate of point1 is: %d\n", point1.y);

    struct point point3;
    struct point point4 = {7, 12};
    printf("The coordinates for point4 are %d and %d\n", point4.x, point4.y);

    struct point aP = makePoint(10, 10);
    printf("For this one our coordinates are %d\n", aP.x);

    myRect.pt1 = makePoint(38, 38);
    printf("The bottom left of our rectangle is %d\n", myRect.pt1);

    int arr8[10];
    arr8[0] = -4;
    printf("Arr of %d is: %d.\n\n", 0, arr8[0]);


    int arrAgain[] = {7, 4, -3, 0, -8, 11, 31, 12};
    insertionSort(arrAgain, sizeof(arrAgain)/sizeof(arrAgain[0]));
    for(int i = 0; i < sizeof(arrAgain)/sizeof(arrAgain[0]); i++){
        printf("The %dth element of arrAgain is: %d\n", i, arrAgain[i]);
    }

    int* pointer1;
    printf("Size here is: %d\n", sizeof(pointer1));

    for(int i = 0; i < 5; i++){
        printf("x");
    }

}

我尝试用其他“Hello-World”(即“不相关”)类型表达式替换 for 循环 - 这导致了我没有预料到的变化。我在 main 内移动了循环 - 这使段错误保持不变。我尝试重新启动 VSCode,尝试注释掉 for 循环(这有效果)。我希望无论最后有或没有这 3 行,代码的工作效果都是一样的,如果它确实执行不同,为什么差异会出现在该点上方多行代码的一部分中。

c loops debugging struct segmentation-fault
1个回答
0
投票

因为你新的left1和left2结构体的左子节点和左子节点没有初始化,所以两个结构体的指向有问题,存在野指针情况,所以出现了段错误

struct node left1;
struct node left2;
left1.val = 100;
//add
left1.left=NULL;
left1.right=NULL;
left2.val = 2;
//add
left2.left=NULL;
left2.right=NULL;
root.left = &left1;
root.right = &left2;
© www.soinside.com 2019 - 2024. All rights reserved.