0

For the past 4 days I've been stuck on this error:

Unhandled exception at 0x77c415de in ls_client_app.exe: 
0xC0000005: Access violation reading location 0xcccccccc.

Here 's the code :

typedef struct client {
    string  ID;
    // some other strings
} client;

typedef struct client_card {
    client CLIENT;
    client_card * next;
} client_card;

typedef client_card * ls_client;

this is add_client function:

int add_client(ls_client &LS_CLIENT) {

    client NEW_CLIENT;
    ls_client NEW_CLIENT_CARD = (ls_client) malloc (sizeof(client_card));

    cout << "CLIENT ID: " <<endl;
    cin  >>  NEW_CLIENT.ID;

    NEW_CLIENT_CARD->next    =  NULL;
    NEW_CLIENT_CARD->CLIENT  =  &NEW_CLIENT;

    while (LS_CLIENT != NULL) {
        LS_CLIENT = LS_CLIENT->next;
    }
    LS_CLIENT = NEW_CLIENT_CARD;

    return 0;
}

this is the main:

int main() {
    ls_client LS_CLIENT = (ls_client) malloc (sizeof(client_card));
    LS_CLIENT = NULL;
    add_client(LS_CLIENT);

    // Error in this line !
    cout << LS_CLIENT->CLIENT->ID <<endl;

    return 0;
}

Taking me to streambuf file second line !

else if (_Traits::eq_int_type(_Traits::eof(),
    overflow(_Traits::to_int_type(*_Ptr))))
    break;  // single character put failed, quit
else
//...

Could anyone help me please i will be grateful !!
Thanks for trying !!

sinelaw
  • 16,205
  • 3
  • 49
  • 80
Aziz Mat
  • 69
  • 2
  • 5
  • 2
    Please use `new` instead of `malloc`. Your client contains a `string`, and the constructor is probably not called for it. – Jesse Good Apr 03 '13 at 20:37
  • 1
    I cannot stress this enough. **Learn To Debug** Personally I'd go to your prof and **demand** coursework teaching on the subject. **With equal importance**, both *developing* code and ***debugging*** code should be honed. What you wracked your brain on for four days a debugger and three single-steps would have solved in two minutes. Knowing is half the battle (literally). – WhozCraig Apr 03 '13 at 20:39
  • `NEW_CLIENT_CARD->CLIENT = &NEW_CLIENT;` should probably be `NEW_CLIENT_CARD->CLIENT = NEW_CLIENT;` –  Apr 03 '13 at 20:40
  • @JesseGood +1, and you can squelch "probably" from comment (too late, I know). – WhozCraig Apr 03 '13 at 20:50
  • 0xCCCCCCCC is uninitialized memory [When and why will an OS initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete?](http://stackoverflow.com/q/370195/995714) – phuclv Apr 28 '15 at 15:45
  • Gee give the bloke a break. – demented hedgehog Sep 05 '16 at 04:33

1 Answers1

7

ls_client LS_CLIENT = (ls_client) malloc (sizeof(client_card)); LS_CLIENT = NULL;

You are creating client, then setting it to NULL. Your error is access violation error. Basically, you are trying to work on a NULL pointer. Swap those lines, and it should be okay.

Edit: Someone edited your code so that it no longer contains this error. If this is how you had it, disregard this answer (sorry for confusing answer with a comment).

Xyzk
  • 1,332
  • 2
  • 21
  • 36
  • You posted an answer, not a comment. – ApproachingDarknessFish Apr 03 '13 at 20:34
  • 2
    Its still there in `main()` (as of this comment, anyway). – WhozCraig Apr 03 '13 at 20:35
  • Thanks for trying but when we move NULL we get Access violation reading location 0xcdcdcdd1 on the: while (LS_CLIENT != NULL) – Aziz Mat Apr 03 '13 at 20:42
  • 3
    @AzizMat Sir, with all due respect, your code is *littered* with bugs. The one identified here is just one of them. Do you see how you're NOT setting the `next` member pointer of your freshly allocated LS_CLIENT to NULL? It is indeterminate, and in this case, clearly not NULL as it should be. Then there are the bugs in add_client, pushing the address of a local variable into a linked list to be accessed out of scope; also undefined behavior. You're using `malloc()` to allocate C++ objects, no constructors are fired and your std::string members are indeterminate, yet more undefined behavior. – WhozCraig Apr 03 '13 at 20:48
  • @AzizMat Does `char[10]` require a constructor? Do you think `std::string` (a C++ ***object class***) has one? Do you think the latter is being called if you `malloc()` memory rather than use `operator new()`? – WhozCraig Apr 04 '13 at 17:16
  • But how can i fix it ? can't put new instead of malloc ! – Aziz Mat Apr 04 '13 at 21:48
  • @AzizMat Hint: **Don't Use `malloc() with C++ Objects`** (how's that for subtle). – WhozCraig Apr 04 '13 at 22:08