ç分段故障前/ return语句中 [英] C segmentation fault before/during return statement

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

问题描述

打印我return语句之前正确的,我将在返回值,并告诉我的code打印一个在该函数调用之后返回的值。不过,我得到一个分段错误我的第一个print语句后和我的第二个之前(也值得注意,这总是发生在第三次函数被调用;从来没有在第一或第二,第四从未或更高版本)。我试着打印出所有的我的工作,看看我的code的其余做一些它也许不应该的数据,但我的数据到这一点看起来不错。这里的功能:

  INT findHydrogen(结构氨基*氨基,诠释非营利组织,浮*差异,诠释totRead){    结构的Atom *原子;
    INT *债券;
    INT numBonds;
    INT I;
    INT retVal的;
    诠释numAtoms;    numAtoms =(*氨基).numAtoms;    原子=(结构的Atom *)malloc的(的sizeof(结构凌动)* numAtoms);
    原子=(*氨基).atoms;    numBonds =原子[非营利组织] .numBonds;    债券=(INT *)malloc的(的sizeof(int)的* numBonds);
    债券=原子[非营利组织] .bonds;    对于(I = 0; I&≤(*氨基).numAtoms;我+ +)
        的printf(ATOM \\ t \\ t%d个%S \\ T0001 \\ t%F \\ t%F \\ t%F \\ N,I + 1,原子[I] .TYPE,原子[I] .X,原子[我] .Y,原子[I] .Z);    对于(i = 0; I< numBonds;我++)
        如果(原子[键[Ⅰ] - totRead] .TYPE [0] =='H'){
            差异[0] =原子[债券[I] - totRead] .X - 原子[非营利组织] .X;
            差异[1] =原子[债券[I] - totRead] .Y - 原子[非营利组织] .Y;
            差异[2] =原子[债券[I] - totRead] .Z - 原子[非营利组织] .Z;            retVal的债券= [I] - totRead;            债券=(INT *)malloc的(的sizeof(INT));
            免费(债券);            原子=(结构的Atom *)malloc的(的sizeof(结构凌动));
            免费(原子);            的printf(2%d个\\ N,retVal的);            返回retVal的;
        }
}

正如我前面提到的,它工作正常,前两次我运行它,第三次它打印retVal的正确值,那么赛格故障的地方它会在那里我调用的函数,这是我为之前做:

  HPOS = findHydrogen((安培;氨基酸[I]),非营利组织,差异,totRead);
的printf(%d个\\ N,HPOS);


解决方案

这是不容易猜测错误所在,从这个code(还有这里为code的几乎每一行的bug潜在) - 可能是你有一个缓冲区溢出的地方,但是如果你使用的是* nix中,运行程序下的valgrind ,你应该能够找到错误相当快。

这行看起来很奇怪,但:

 碳原子=(结构的Atom *)malloc的(的sizeof(结构凌动)* numAtoms);
原子=(*氨基).atoms;

您正在泄漏内存,因为你放弃由malloc返回的指针。与债券,以及同样的事情同样的事情一遍又一遍你的循环中。

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\t\t%d  %s\t0001\t%f\t%f\t%f\n", 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\n", retVal);

            return retVal;
        }
}

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\n", hPos);

解决方案

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.

These lines look odd though:

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

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语句中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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