Thread: 0xC0000005 Run time error when trying to allocate memory .

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    11

    0xC0000005 Run time error when trying to allocate memory .

    Hello everyone ,

    A quick question here about an error I seem to get around my code a lot , the program crashes at several locations with run time error 0xC0000005 .

    This happens to me at the following function , which creates a user`s profile and puts it in a database later :
    Code:
    /*
        The following function will create a profile in the linked list.
        Basically , create nodes on "top" of the head node in the linked list .
    */
    void createProfile(char *userMail,char *userPass,short age,char *firstName,char *lastName,char *country)
    {
        char userName[MAX_NAME_LENGTH];         //Will save the users - user name (the part without @gmail.com).
        Profiles *new_profile=(Profiles *)malloc(sizeof(Profiles *));
        if(new_profile!=NULL) //Check if not NULL , we can only continue if the malloc operation was successfull.
        {
            strcpy(new_profile->userMail,userMail);
            strcpy(new_profile->password,userPass);
            new_profile->age=age;
            strcpy(new_profile->firstName,firstName);
            strcpy(new_profile->lastName,lastName);
            strcpy(new_profile->country,country);
            p_heap[numberOfProfiles]=new_profile;
            p_heap[numberOfProfiles]->signedIn=0;
            getUser(userMail,userName);
            strcpy(new_profile->userName,userName);
        }//if
        else
            createProfile(userMail,userPass,age,firstName,lastName,country);   //If the malloc operation failed - try again.
        return;
    }//createProfile
    Where`s Profile is a struct :
    Code:
    typedef struct Profile {
        char userMail[MAX_MAIL_LENGTH];     //uses the MAX_MAIL_LENGTH macro (40 as for now) for the mail of ones profile , acts as user name . I believe that this is enough.
        char password[MAX_PASS_LENGTH];     //uses the MAX_PASS_LENGTH macro (8 as requested) , this will be a string that holds up to 8 chars and one for '\0' .
        char firstName[MAX_NAME_LENGTH];    //use the MAX_NAME_LENGTH (20 as for now) for the first name , I think it is enough , could be raised if I will differ my thoughts.
        char lastName[MAX_NAME_LENGTH];     //^ ditto .
        char country[MAX_COUNTRY_LENGTH];   //The country will be validated from a list that will be saved in a file.
        short age;                          //Saving as much memory as we can .
        char phone[MAX_PHONE_LENGTH];       //Uses the MAX_PHONE_LENGTH macro (11 = 10 + 1 for '\0' for now) , will be used to save the phone number .
        enum Smartphone{                    //An enum denoting if one has a smartphone or has not , this for suggesting an app , which we wont really have , yet again ..
            noPhone=0,yesPhone} Smartphone;
        enum Sinefield_or_friends{          //An enum denoting if one is prefering sinefield , or rather friends as a tv show .
            sinefield=0,friends} S_or_f;
        enum Dogs_or_cats{                  //An enum denoting if one is prefering dogs or cats as a pet .
            dogs=0,cats} D_or_c;
        enum Jackey_got_an_sms{             //An enum denoting if one has sent Jackie from the show "The big brother" an SMS text for to win the million NIS .
            noSms=0,yesSms} J_got_sms;
        short signedIn;                     //How many times the user have logged in.
        short filledAdditionalInfo;         //If one has filled additional information (boolean & phone number)
        char userName[MAX_NAME_LENGTH];     //This is the user name (the part without @gmail.com).
        short pals[MAX_PROFILES];           //Will save indexes of profiles which are pals with the current profile .
        short wantToBePals[MAX_PROFILES];   //Will save indexes of profiles which would like to be pals with the current profile .
        short blockedPals[MAX_PROFILES];    //Will save indexes of profiles which are blocked .
        //To be done - dates .
    } Profiles;
    If I copy this to a clean code (just the struct , this function and main) it works flawlessly , but in my code it crashes on the second time
    I call the creatProfile(...) function .

    When I checked it - it crashes at the allocation point :
    Profiles *new_profile=(Profiles *)malloc(sizeof(Profiles *));

    Tried using calloc instead , same problem .
    This really drives me mad , I don`t know what and how to fix it .

    Any help would be extremely appreciated ,

    Thanks ,
    Danny .

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Profiles *new_profile=(Profiles *)malloc(sizeof(Profiles *));
    You didn't allocate enough space. You only allocated enough for ONE pointer.

    Either
    Profiles *new_profile=malloc(sizeof(Profiles));

    or preferably,
    Profiles *new_profile=malloc(sizeof(*new_profile));

    You also don't need the cast (see the FAQ), if you have included stdlib.h and you're compiling this as C code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    11
    While the second suggestion did not work (same error) - the first one did the job .
    Thank you so very much ,

    Danny .

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Weird, because they both do the same thing, in a different way.

    I'm guessing this isn't the only place you've messed up the malloc size.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocate memory inside allocated memory block?
    By Heidi_Nayak in forum C Programming
    Replies: 14
    Last Post: 04-15-2009, 04:19 PM
  2. 0xc0000005 error
    By Hankyaku in forum C++ Programming
    Replies: 32
    Last Post: 03-14-2008, 04:20 PM
  3. Memory allocation error (Nicely formatted this time)
    By killiansman in forum C Programming
    Replies: 1
    Last Post: 06-08-2007, 11:14 AM
  4. Allocate memory question
    By h3ro in forum C++ Programming
    Replies: 6
    Last Post: 04-12-2007, 01:55 PM
  5. when to allocate memory
    By SAMSAM in forum Windows Programming
    Replies: 3
    Last Post: 01-22-2003, 11:40 PM