1

Hi guys i'm trying to debug this application in C++ using netbeans (running it gives me run failed after closing it) it gives me a signal error at this point

*c = (char)any number;

where any number is an integer from 1-7

it tells me signal caught SIGSEGV ? with signal error ?

what is it

the code writes some stuff to a binary file it goes like this

void clean_up(Dot &myDot, Uint32 &bg) {
    SDL_FreeSurface(DotS);

    ofstream f(SAVE_FILE_PATH, ios::binary | ios::out);

    f.clear();

//    char *buffer;

//    buffer[0] = *(char*)(&myDot.get_location().x + 0);
//    buffer[1] = *(char*)(&myDot.get_location().x + 1);
//    buffer[2] = *(char*)(&myDot.get_location().x + 2);
//    buffer[3] = *(char*)(&myDot.get_location().x + 3);
//    buffer[4] = *(char*)(&myDot.get_location().y + 0);
//    buffer[5] = *(char*)(&myDot.get_location().y + 1);
//    buffer[6] = *(char*)(&myDot.get_location().y + 2);
//    buffer[7] = *(char*)(&myDot.get_location().y + 3);

    f.write((char*)&myDot.get_location().x, sizeof(myDot.get_location().x));

    f.write((char*)&myDot.get_location().y, sizeof(myDot.get_location().y));

    char *c;

    if (bg == C0)
        *c = (char)1;
    else if (bg == C1)
        *c = (char)2;
    else if (bg == C2)
        *c = (char)3;
    else if (bg == C3)
        *c = (char)4;
    else if (bg == C4)
        *c = (char)5;
    else if (bg == C5)
        *c = (char)6;
    else if (bg == C6)
        *c = (char)7;

    f.write(c, 1);

    f.close();

    SDL_Quit();
}

and please can someone tell me why the commented part gives me a signal too

1 Answer 1

1

You haven't allocated memory to c. It's a pointer to some random location (or NULL, depending on how memory is being initialized). You then try to write to it.

Does it really need to be a pointer? Could you just use char c;, and then c = (char)1; etc. You'd then want to change your fwrite call to f.write(&c, 1); (or use fputc).

EDIT: The commented out part of your code looks like it has the same underlying problem. You haven't allocated any memory for buffer. If the example is the most of it, you're only ever having 8 items, then declare it as char buffer[8] instead of as a pointer.

1
  • Thanks a lot man! guess what, the whole program worked and it worked well. Thanks again Sep 3, 2011 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.