return 语句之前/期间的 C 分段错误 [英] C segmentation fault before/during return statement

查看:16
本文介绍了return 语句之前/期间的 C 分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 return 语句之前打印我正在返回的值,并告诉我的代码在函数调用之后打印返回的值.但是,在我的第一个 print 语句之后和第二个之前,我得到了一个分段错误(值得注意的是,这总是在第三次调用函数时发生;从来没有第一次或第二次,从来没有第四次或更晚).我尝试打印出我正在处理的所有数据,以查看我的其余代码是否在做一些它可能不应该做的事情,但到目前为止我的数据看起来还不错.函数如下:

I print the value that I'm returning right before my return statement, and tell my code to print the value that was returned right after the function call. However, I get a segmentation fault after my first print statement and before my second (also interesting to note, this always happens on the third time the function is called; never the first or the second, never fourth or later). I tried printing out all of the data that I'm working on to see if the rest of my code was doing something it maybe shouldn't, but my data up to that point looks fine. Here's the function:

int findHydrogen(struct Amino* amino, int nPos, float* diff, int totRead) {

    struct Atom* atoms;
    int* bonds;
    int numBonds;
    int i;
    int retVal;
    int numAtoms;

    numAtoms = (*amino).numAtoms;

    atoms = (struct Atom *) malloc(sizeof(struct Atom) * numAtoms);
    atoms = (*amino).atoms;

    numBonds = atoms[nPos].numBonds;

    bonds = (int *) malloc(sizeof(int) * numBonds);
    bonds = atoms[nPos].bonds;

    for(i = 0; i < (*amino).numAtoms; i++)
        printf("ATOM		%d  %s	0001	%f	%f	%f
", i + 1, atoms[i].type, atoms[i].x, atoms[i].y, atoms[i].z);

    for(i = 0; i < numBonds; i++) 
        if(atoms[bonds[i] - totRead].type[0] == 'H') {
            diff[0] = atoms[bonds[i] - totRead].x - atoms[nPos].x;
            diff[1] = atoms[bonds[i] - totRead].y - atoms[nPos].y;
            diff[2] = atoms[bonds[i] - totRead].z - atoms[nPos].z;

            retVal = bonds[i] - totRead;

            bonds = (int *) malloc(sizeof(int));
            free(bonds);

            atoms = (struct Atom *) malloc(sizeof(struct Atom));
            free(atoms);

            printf("2 %d
", retVal);

            return retVal;
        }
}

正如我之前提到的,前两次运行它运行良好,第三次它打印 retVal 的正确值,然后在到达我调用函数的位置之前在某处出现 seg 错误,我这样做:

As I mentioned before, it works fine the first two times I run it, the third time it prints the correct value of retVal, then seg faults somewhere before it gets to where I called the function, which I do as:

hPos = findHydrogen((&aminoAcid[i]), nPos, diff, totRead);
printf("%d
", hPos);

推荐答案

很难从这段代码中猜出错误在哪里(这里几乎每一行代码都可能存在错误) - 可能你有一个缓冲区在某处溢出,但是如果你在 *nix 上,在 valgrind 下运行你的程序,你应该能够找到错误相当快.

It's not easy to guess where the error is from this code(there's a potential for bug in just about every line of code here) - likely you have a buffer overrun somewhere, however if you're on a *nix , run your program under valgrind, you should be able to find the error rather quickly.

这些行看起来很奇怪:

atoms = (struct Atom *) malloc(sizeof(struct Atom) * numAtoms);
atoms = (*amino).atoms;

您正在泄漏内存,因为您丢弃了 malloc 返回的指针.与 bonds 相同,在你的 for 循环中再次发生相同的事情.

You're leaking memory, as you discard the pointer returned by malloc. Same thing with bonds, and same thing over again inside your for loop.

这篇关于return 语句之前/期间的 C 分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆