CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Wierd Seg Fault problem.

  1. #1
    Join Date
    Feb 2009
    Posts
    5

    Wierd Seg Fault problem.

    I thought I knew my way around C++ and rarely get stuck. But this time i'm totally stumped. So i'm turning to this forum for help. Hopefully someone here can tell me what is going on.

    compiling with g++ in netbeans. I have removed the code i'm using that doesnt affect the problem from the listing. It comes down to this.

    for (int i = 0; i < RBFCount; i++) {
    cout << "MM "<<RBFList[0]->cy<<endl;
    }
    cout << "MM "<<RBFList[0]->cy<<endl;//The line causes seg fault.

    RBFList is a class wide variable. I can print out RBFList[0]->cy inside the for loop it's fine, but for some reason in the scope of this function is causes a seg fault. I can print it out the line before I call this function its fine. The call to this function is just UpdateRBF(); nothing special, but in the body of this function if i try to access RBFList[0]->cy i get a segfault. Yet inside the for loop it's also fine.

    I can't find any logic whatsoever for this. Ive be using RBFList[0]->cy for sometime without problems. it get wierder.

    if i do

    for (int i = 0; i < RBFCount; i++) {
    cout << "MM "<<RBFList[0]->cy<<endl;
    }

    no segfault.

    I can print the value of RBFCount. it is 32 and doesnt change since the program init.
    but if i do: (or use any other non zero integer, say 1)

    for (int i = 0; i < 32; i++) {
    cout << "MM "<<RBFList[0]->cy<<endl;
    }

    it segfaults.

    The only thing i can think of is that the memory is messed up and some how when it loads RBFCount as the upper limit for the for loop it stops the segfault occurring in some bizzare way... i'm cluching at straws. Can anyone help me with this?

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Wierd Seg Fault problem.

    Print out the value (which is an address) of RBFList before doing an access that works; then do it again before doing an access that doesn't. Alternatively, use the debugger to verify these values at these times.

    If they aren't the same, then figure out why not and you'll have your answer.

    Segmentation faults are hard enough to track down; they're nearly impossible if you don't show more complete code than you've given here. We need to be able to follow the lifetime of the RBFList pointer at least from the last place it worked as expected.

  3. #3
    Join Date
    Feb 2009
    Location
    Ukraine
    Posts
    64

    Re: Wierd Seg Fault problem.

    It's strange, that you use the same string all over the iterations. Maybe you meant RBFList[i]->cy?
    Anyway, Lindley is right - show us more code, illustrate the lifetime of RBFList and how it is declared as.
    The code provided gives a little thing to work around the problem.

  4. #4
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Wierd Seg Fault problem.

    The behaviour you are describing suggests a stack or heap corruption - possibly/probably caused by a buffer overrun, and almost certainly caused by overwriting data that an object/pointer has not been allocated. Furthermore, it may or may not have anything to do with RBFList[0]->cy. I'll add to previous comments by saying "more code please".

  5. #5
    Join Date
    Feb 2009
    Posts
    5

    Re: Wierd Seg Fault problem.

    ah...

    well, here is the code. I should warm you though this is my first c++ project, and it may be a little unpleasant to go through. I will try to analyse this problem further and upload some more presentable and succinct code tomorrow, but in the meantime here it is raw. I've taken out the worst of the commented out bits of code... hope i'm not supposed to be commenting what's going on...


    If anyone does have a look, i'd like to listen to any tips/criticism of my c++ code.

    ps: it just occurred to me that when i looked with the debugger initially i noticed that the address of RBFList one change between it faulting scope and the other, but that the last few digits were same. I thought it might be a coincidence, but could this be because the address has been partially over written? I need to understand how segfaults occur/are detected better. I previous had been getting them when writing to an array out of bounds before i added lines such as the two ifs in addToBoundary(). i naively let myself think that i'd be warned whenever i accessed out of bounds, despite knowing somewhere that that doesnt happen in c++. What sort of memory corruption scenarios could be happening here?

    pps: did I mean RBFList[i]? Yes, i had that but the problem still happens with 0.


    Liam



    #include "phi.h"

    using namespace std;

    Phi::Phi(int x, int y) {
    RBFCount = 0;
    RBFList = new RBF*[100];

    //loadInput();
    //loadInputRBFs();
    xDim = x;
    yDim = y;
    outputMap = allocate2DArray<int>(xDim, yDim);
    intensityMap = allocate2DArray<float>(xDim, yDim);

    gamma = 0;
    muIn = 0;
    muOut = 0;
    totalD = 0;
    diag = false;
    divTermVal = 1;
    centrePhiVal = 0;
    startTime = time(NULL);
    data1open = false;
    data2open = false;
    data3open = false;
    boundaryLen = 0;

    updateRBFs();

    }

    Phi::Phi(const Phi& orig) {
    }

    Phi::~Phi() {
    }

    int Phi::addRBF(RBF* anRBF) {
    RBFList[RBFCount++] = anRBF;


    return 0;
    }

    int Phi::updatePhi() {

    evolveRBFs();
    }

    void Phi::stepIntegrateRBFs(int x, int y) {

    if (x < 0) x = 0;
    if (y < 0) y = 0;
    D = (pow((intensityMap[x][y] - muIn), 2) - pow((intensityMap[x][y] - muOut), 2));
    float d1 = (intensityMap[x][y] - muIn);
    float d2 = (intensityMap[x][y] - muOut);
    totalD += D;
    //cout<<"with "<<intensityMap[x][y]<<" and mus "<<muIn<<" "<<muOut<<" D is "<<D<<endl;

    for (int i = 0; i < RBFCount; i++) {

    float si2 = RBFList[i]->six * RBFList[i]->six;

    float changex = D * RBFList[i]->w * RBFList[i]->localRBFVal * (((float) x / xDim) - RBFList[i]->cx) / si2;
    float changey = D * RBFList[i]->w * RBFList[i]->localRBFVal * (((float) y / yDim) - RBFList[i]->cy) / si2;
    RBFList[i]->dcx += changex;
    RBFList[i]->dcy += changey;
    RBFList[i]->dw += D * RBFList[i]->localRBFVal;

    if (data1open == true && i == 8 && step == 5 && (changey > 0.001 || changey <-0.001)) {
    data1 << "RBF " << i << "x y \t" << x << " " << y << "cxcy: " << RBFList[i]->cx << " " << RBFList[i]->cy << "\t dcxComp: " << changex << "\t dcyComp: " << changey << " \tPhi: " << RBFList[i]->localRBFVal << " \txPosFactor: " << (((float) x / xDim) - RBFList[i]->cx) / si2 << " \tyPosFactor: " << (((float) y / yDim) - RBFList[i]->cy) / si2 << " D: " << D << " 1: " << d1 << " 2: " << d2 << endl;
    }
    if (data3open == true && i == 16 && step > 5 && (changex > 0.001 || changex <-0.001)) {
    data3 << "RBF " << i << "x y \t" << x << " " << y << "cxcy: " << RBFList[i]->cx << " " << RBFList[i]->cy << "\t dcxComp: " << changex << "\t dcyComp: " << changey << " \tPhi: " << RBFList[i]->localRBFVal << " \txPosFactor: " << (((float) x / xDim) - RBFList[i]->cx) / si2 << " \tyPosFactor: " << (((float) y / yDim) - RBFList[i]->cy) / si2 << " D: " << " 1: " << d1 << " 2: " << d2 << D << endl;
    }

    // if (change > 1 || change<-1) {
    // if (D<-0.00001 && RBFList[i]->localRBFVal > 0.00001) {
    //if (i == 12)cout << endl<<endl <<"at x y "<<x<<" "<<y<<"cx cy"<<RBFList[i]->cx<<" "<<RBFList[i]->cy<< " 12 RBFList[i]->dcy " << RBFList[i]->dcy << " w:" << RBFList[i]->w << " localRBFVal:" << RBFList[i]->localRBFVal << "D " << D << " changex = " << changex << " changey = " << changey<< " F:" << (y - RBFList[i]->cy) / si2;
    // if (i == 12&&abs(changey)>1)cout << endl<<endl <<"at x y "<<x<<" "<<y<<"cx cy"<<RBFList[i]->cx<<" "<<RBFList[i]->cy<< " 12 RBFList[i]->dcy " << RBFList[i]->dcy << " localRBFVal:" << RBFList[i]->localRBFVal << "D " << D << " changex = " << changex << " changey = " << changey<< " F:" << (y - RBFList[i]->cy) / si2;
    // if (i == 12)cout << endl << "12 RBFList[i]->dcx " << RBFList[i]->dcx << " w:" << RBFList[i]->w << " localRBFVal:" << RBFList[i]->localRBFVal << "D " << D << " changex = " << changex << " changey = " << changey << " F:" << (x - RBFList[i]->cy) / si2;
    //if (i == 13&&RBFList[i]->dcy<-1)cout << endl << "13 RBFList[i]->dcy " << RBFList[i]->dcy << " w:" << RBFList[i]->w << " localRBFVal:" << RBFList[i]->localRBFVal << "D " << D << " changex = " << changex << " changey = " << changey<< " F:" << (y - RBFList[i]->cy) / si2;
    // }
    // }
    }
    //}
    //cout<<"rbf3 "<<RBFList[3]->dw<<" "<<RBFList[3]->dcx<<" "<<RBFList[3]->dcy<<endl;
    }

    void Phi::evolveRBFs() {

    bool inside = false;
    inAreaPixels = 0;
    totalBoundedIntensity = 0;
    outAreaPixels = 0;
    totalUnboundedIntensity = 0;
    float phiVal = 0;
    boundaryLen = 0;

    //for each pixel position
    float maxPhi = 0;
    float minPhi = 0;

    for (int xIndex = 0; xIndex < xDim; xIndex++) {
    for (int yIndex = 0; yIndex < yDim; yIndex++) {

    phiVal = 0;
    outputMap[xIndex][yIndex] = 0;
    //sum new phiVal
    for (int i = 0; i < RBFCount; i++) {
    phiVal += RBFList[i]->w * RBFList[i]->getVal((float) xIndex / xDim, (float) yIndex / yDim);

    }

    // if (phiVal > maxPhi) {
    // maxPhi = phiVal;
    // }
    //
    // if (phiVal < minPhi) {
    // minPhi = phiVal;
    // }


    //cout<<phiVal;
    if (phiVal <= 0) {

    if (inside == false) {

    //crossing boundary - entering
    stepIntegrateRBFs(xIndex, yIndex);
    addToBoundary(xIndex, yIndex);
    inside = true;
    if (xIndex == 100) {

    // cout << "FOOOH" << yIndex << " val" << phiVal << endl;
    }
    }

    modifyInMean(xIndex, yIndex);

    }

    if (phiVal > 0) {

    if (inside == true) {
    //crossing boundary - leaving
    stepIntegrateRBFs(xIndex, yIndex - 1);
    addToBoundary(xIndex, yIndex - 1);

    if (xIndex == 100) {

    // cout << "YOOOH" << yIndex << " val" << phiVal << endl;
    }

    inside = false;
    }
    modifyOutMean(xIndex, yIndex);

    }

    }
    }

    for (int yIndex = 0; yIndex < yDim; yIndex++) {
    for (int xIndex = 0; xIndex < xDim; xIndex++) {

    phiVal = 0;

    //sum new phiVal
    for (int i = 0; i < RBFCount; i++) {
    phiVal += RBFList[i]->w * RBFList[i]->getVal((float) xIndex / xDim, (float) yIndex / yDim);

    }

    //cout<<phiVal;
    if (phiVal <= 0) {

    if (inside == false) {
    inside = true;

    if (outputMap[xIndex][yIndex] == 0) {
    //crossing boundary - entering
    stepIntegrateRBFs(xIndex, yIndex);
    addToBoundary(xIndex, yIndex);
    }
    }



    }

    if (phiVal > 0) {

    if (inside == true) {
    inside = false;

    if (outputMap[xIndex][yIndex] == 0) {
    //crossing boundary - entering
    stepIntegrateRBFs(xIndex - 1, yIndex);
    addToBoundary(xIndex - 1, yIndex);
    }
    }


    }

    }
    }

    //cout << "field max val " << maxPhi << "field min val " << minPhi << endl;

    //Update RBFs

    updateRBFs();
    cout << "MM " << RBFList[0]->cy << endl;
    muIn = (float) totalBoundedIntensity / (inAreaPixels * 255 + 0.00001);
    muOut = (float) totalUnboundedIntensity / (outAreaPixels * 255 + 0.00001);
    if (inAreaPixels == 0 || outAreaPixels == 0) {
    cout << "SPace error!!!!!";
    }
    cout << " In in" << muIn << " from " << inAreaPixels;
    cout << " Out in" << muOut << " f" << outAreaPixels << " totalD " << totalD;
    data2 << "Step:" << step << " Bounded Intensity: " << muIn << "\t Unbounded: " << muOut << "\t Total D: " << totalD << " Boundary Length: " << boundaryLen << endl;
    totalD = 0;
    }

    void Phi::updateRBFs() {


    float maxy = 0;
    float max2y = 0;
    int maxId = 0;
    int max2Id = 0;



    float dt;

    if (muIn < 0.3) {
    dt = 1000 * exp(-muIn * muIn / 0.013);
    } else {
    dt = exp(-2.3 * (muIn - 0.3));
    }

    dt = 0.00002;

    float maxMov = 0;
    int maxMoverId = 0;

    for (int i = 0; i < RBFCount; i++) {
    if (abs(RBFList[i]->dcx) > maxMov) {
    maxMov = abs(RBFList[i]->dcx);
    maxMoverId = i;

    }
    if (abs(RBFList[i]->dcy) > maxMov) {
    maxMov = abs(RBFList[i]->dcy);
    maxMoverId = i;

    }

    }


    for (int i = 31; i < RBFCount; i++) {
    if (abs(RBFList[maxMoverId]->dcx) > abs(RBFList[maxMoverId]->dcy)) {
    dt = 0.5 / RBFList[maxMoverId]->dcx;
    cout << "MMid MMx " << maxMoverId << " " << RBFList[maxMoverId]->dcx << endl;
    } else {
    dt = 0.5 / RBFList[maxMoverId]->dcy;
    cout << "MMid MM " << maxMoverId << " " << RBFList[maxMoverId]->dcy << endl;
    }
    }
    cout << "MMid MM " << maxMoverId << " " << RBFList[maxMoverId]->dcy << endl;

    // dt=0.0002;
    cout << " updating with dt " << dt;
    for (int i = 0; i < RBFCount; i++) {


    // if (RBFList[i]->cy > maxy && RBFList[i]->w <= 0) {
    // maxId = i;
    // maxy = RBFList[i]->cy;
    // }
    // if (RBFList[i]->cy > max2y && RBFList[i]->w > 0) {
    // max2Id = i;
    // max2y = RBFList[i]->cy;
    // }


    if (data1open == true && i == 8) {
    data1 << "RBF " << i << " Step: " << step << "\t dcxComp: " << RBFList[i]->dcx << "\t dcyComp: " << RBFList[i]->dcy << " Total D: " << totalD << " mu in out: " << muIn << " " << muOut << endl;
    }
    if (data3open == true && i == 16) {
    data3 << "RBF " << i << " Step: " << step << "\t dcxComp: " << RBFList[i]->dcx << "\t dcyComp: " << RBFList[i]->dcy << " Total D: " << totalD << " mu in out: " << muIn << " " << muOut << endl;
    }



    RBFList[i]->cx += RBFList[i]->dcx*dt;
    RBFList[i]->cy += RBFList[i]->dcy*dt;
    RBFList[i]->w += RBFList[i]->dw*dt;

    //if (diag == true) cout << endl << "rbf " << i << " " << RBFList[i]->w << " " << RBFList[i]->cx << " " << RBFList[i]->cy << " " << RBFList[i]->dw << " " << RBFList[i]->dcx << " " << RBFList[i]->dcy << "field centre" << centrePhiVal << endl;
    // if (RBFList[i]->dcx*dt > 1||RBFList[i]->dcx*dt<-1) {
    //if(i==12) cout << endl << "update this rbf" << " dcx " << RBFList[i]->dcx*dt << " dcy " << RBFList[i]->dcy*dt << " dsix " << RBFList[i]->dsix << " dw " << RBFList[i]->dw <<endl;
    //}

    RBFList[i]->dcx = 0;
    RBFList[i]->dcy = 0;
    RBFList[i]->dw = 0;
    }
    // cout << " found -ve max " << maxy << " id " << maxId << endl;
    //cout << " found +ve max " << max2y << " id " << max2Id << endl;

    }

    void Phi::modifyInMean(int xIndex, int yIndex) {
    inAreaPixels++;
    if (xIndex < 0 || yIndex < 0) {
    cout << "OOOOOOOOOOHNNNOOOOOOOO modify" << endl;
    }
    totalBoundedIntensity += intensityMap[xIndex][yIndex];
    }

    void Phi::modifyOutMean(int xIndex, int yIndex) {
    outAreaPixels++;
    if (xIndex < 0 || yIndex < 0) {
    cout << "OOOOOOOOOOHNNNOOOOOOOO modify" << endl;
    }
    totalUnboundedIntensity += intensityMap[xIndex][yIndex];
    }

    void Phi::createSetup(int shapeRadius, int negRadius, int posRadius) {

    BMP inputBMP;
    inputBMP.ReadFromFile("brain.bmp");
    // convert each pixel to grayscale using RGB->YUV
    xDim = inputBMP.TellWidth();
    yDim = inputBMP.TellHeight();
    //intensityMap = allocate2DArray<float>(xDim, yDim);
    cout << endl << "Loaded brain.bmp XDIM:" << xDim << " YDIM:" << yDim << endl;

    for (int j = 0; j < yDim; j++) {
    for (int i = 0; i < xDim; i++) {
    int intensity = (int) ((inputBMP(i, j)->Red + inputBMP(i, j)->Green + inputBMP(i, j)->Blue) / 3);
    intensityMap[i][j] = intensity / 255;
    }
    }



    int i = 0;

    for (float theta = 0; theta < 2 * PI; theta += (PI / 8)) {

    int r = negRadius;
    float x1 = (150 + r * cos(theta)) / xDim;
    float y1 = (150 + r * sin(theta)) / yDim;
    r = posRadius;
    float x2 = (150 + r * cos(theta)) / xDim;
    float y2 = (150 + r * sin(theta)) / yDim;
    float sigma = (float) 20 / xDim;

    addRBF(new RBF(x1, y1, 0, sigma, sigma, -1));
    cout << endl << "adding - rbf " << i++ << " at " << x1 << " " << y1 << "sigma " << sigma << endl;

    addRBF(new RBF(x2, y2, 0, sigma, sigma, 1));
    cout << endl << "adding + rbf " << i++ << " at " << x2 << " " << y2 << "sigma " << sigma << endl;


    }
    data2 << "Create RBFs #" << RBFCount << endl;

    }

    void Phi::addToBoundary(int x, int y) {
    boundaryLen++;
    if (x < 0) x = 0;
    if (y < 0) y = 0;
    outputMap[x][y] = 255;

    }

    void Phi::mapToFile(int** map, char *fname) {
    BMP outputBMP;
    outputBMP.SetSize(xDim, yDim);
    outputBMP.SetBitDepth(8);
    for (int j = 0; j < yDim; j++) {
    for (int i = 0; i < xDim; i++) {
    outputBMP(i, j)->Green = map[i][j];
    outputBMP(i, j)->Red = 0;
    outputBMP(i, j)->Blue = 0;
    }
    }


    outputBMP.WriteToFile(fname);
    }

    void Phi:utputData(int** map, char *fname) {
    BMP outputBMP;
    outputBMP.SetSize(xDim, yDim);
    outputBMP.SetBitDepth(8);
    for (int j = 0; j < yDim; j++) {
    for (int i = 0; i < xDim; i++) {

    //cout<<xDim<<" kjkj "<<yDim;

    outputBMP(i, j)->Green = map[i][j];
    outputBMP(i, j)->Red = 0;
    outputBMP(i, j)->Blue = (int) (intensityMap[i][j]*255);
    }
    }
    for (int j = 0; j < RBFCount; j++) {
    int cx = (int) ((RBFList[j]->cx) * xDim);
    int cy = (int) ((RBFList[j]->cy) * yDim);


    //cout << "outing rbf with w" << RBFList[j]->w<<"at "<<cx<<" "<<cy;
    if (cx > xDim) {

    cout << "overflow cx=" << cx;
    cx = xDim;
    }
    if (cy > xDim) {

    cout << "overflow cy=" << cy;
    cy = xDim;
    }
    if (cx < 0) {

    cout << "overflow cx=" << cx;
    cx = 0;
    }
    if (cy < 0) {

    cout << "overflow cy=" << cy;
    cy = 0;
    }

    if (RBFList[j]->w < 0) {
    outputBMP(cx, cy)->Green = 0;
    outputBMP(cx, cy)->Blue = 0;
    outputBMP(cx, cy)->Red = 255;
    } else {
    outputBMP(cx, cy)->Red = 30;
    outputBMP(cx, cy)->Green = 100;
    outputBMP(cx, cy)->Blue = 120;
    }

    }
    outputBMP.WriteToFile(fname);
    }

    void Phi:utputRBFs(char *fname) {
    BMP outputBMP;
    outputBMP.SetSize(xDim, yDim);
    outputBMP.SetBitDepth(8);
    for (int j = 0; j < RBFCount; j++) {
    int cx = (int) ((RBFList[j]->cx) * xDim);
    int cy = (int) ((RBFList[j]->cy) * yDim);


    cout << "outing rbf with w" << RBFList[j]->w << "at " << cx << " " << cy;
    if (cx > xDim) {

    cout << "overflow cx=" << cx;
    cx = xDim;
    }
    if (cy > xDim) {

    cout << "overflow cy=" << cy;
    cy = xDim;
    }
    if (cx < 0) {

    cout << "overflow cx=" << cx;
    cx = 0;
    }
    if (cy < 0) {

    cout << "overflow cy=" << cy;
    cy = 0;
    }
    if (RBFList[j]->w < 0) {
    outputBMP(cx, cy)->Green = 255;
    outputBMP(cx, cy)->Blue = 0;
    outputBMP(cx, cy)->Red = 0;
    } else {
    outputBMP(cx, cy)->Red = 255;
    outputBMP(cx, cy)->Green = 0;
    outputBMP(cx, cy)->Blue = 0;
    }

    }
    outputBMP.WriteToFile(fname);
    }

    int main() {
    char *command = new char[50];
    char *filename = new char[50];
    Phi *phi = new Phi(300, 300);
    sprintf(command, "mkdir run%d", phi->startTime);
    system(command);
    sprintf(command, "cp *.cpp ./run%d", phi->startTime);
    system(command);
    sprintf(command, "cp *.h ./run%d", phi->startTime);
    system(command);

    sprintf(filename, "./run%d/RBF8data.txt", phi->startTime);
    phi->data1.open(filename);
    phi->data1open = true;

    sprintf(filename, "./run%d/EnergyData.txt", phi->startTime);
    phi->data2.open(filename);
    phi->data2open = true;

    sprintf(filename, "./run%d/RBF16data.txt", phi->startTime);
    phi->data3.open(filename);
    phi->data3open = true;

    phi->diag = false;
    phi->createSetup(60, 60, 70);

    sprintf(filename, "./run%d/initial.bmp", phi->startTime);

    phi->outputData(phi->outputMap, filename);

    for (phi->step = 0; phi->step < 120; phi->step++) {

    cout << endl << "evolving step " << phi->step << endl;
    phi->evolveRBFs();

    if (phi->step % 1 == 0) {
    sprintf(filename, "./run%d/output-%d.bmp", phi->startTime, phi->step);

    phi->outputData(phi->outputMap, filename);
    }
    }
    phi->data1.close();
    phi->data3.close();
    return 0;

    }

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Wierd Seg Fault problem.

    Quote Originally Posted by quantum.leaf View Post
    ah...

    well, here is the code. I should warm you though this is my first c++ project, and it may be a little unpleasant to go through.
    First step to mitigating that is to post it in code tags so that indenting is preserved.

  7. #7
    Join Date
    Feb 2009
    Posts
    5

    Re: Wierd Seg Fault problem.

    i have to expose my ignorance now. I can't find a list of allowed tags. I've tried <code> </code> and read faq/tags without success. I'm sure it should be obvious but can you tell me what the tags are?

  8. #8
    Join Date
    Feb 2009
    Posts
    5

    Re: Wierd Seg Fault problem.

    Hi,

    Very sorry but i realise after stepping through more carefully with the debugger that the offending function was being called before RBFList had been intialised. At this time RBFCount was zero so for loops did not iterate. My approach to finding this bug was unfortunate and i managed to trick myself that there was a problem where there wasnt one. It was good experience in learning to use the debugger. I'm used to java where memory management is much easier.

    Anyway, i'm extremely sorry if i've wasted anyones time, especially if anyone took time to read through my code.

  9. #9
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Wierd Seg Fault problem.

    You don't need to be sorry - it's good you found the problem though. For future, the code tags are [code][/code].

  10. #10
    Join Date
    Feb 2009
    Posts
    5

    Re: Wierd Seg Fault problem.

    Thanks.

    Now i just need to find out what is wrong with my equation! (or their implementation)

    I was wondering, how does the run time know when a segfault occurs? when i tried to access an uninitialised pointer why did i get a seg fault and not just whatever random data happened to be there? is because the data that was there did not fit the pointer type? i thought data was just data...

  11. #11
    Join Date
    Jan 2009
    Posts
    596

    Re: Wierd Seg Fault problem.

    Quote Originally Posted by quantum.leaf View Post
    Thanks.

    Now i just need to find out what is wrong with my equation! (or their implementation)

    I was wondering, how does the run time know when a segfault occurs? when i tried to access an uninitialised pointer why did i get a seg fault and not just whatever random data happened to be there? is because the data that was there did not fit the pointer type? i thought data was just data...
    What happens would depend upon the value of the uninitialized pointer. If the pointer points to memory which your process is allowed to access, then the code will run as usual, except that it will use whatever random data happens to be at the pointers address.

    However, if the pointer points to an address which your process is not allowed to access then you will get a segmentation fault.

    As with most things in life, there is a wikipedia article about seg faults: http://en.wikipedia.org/wiki/Segmentation_fault

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)