Thread: malloc problem

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    17

    malloc problem

    Hi,

    I have a serious problem with my allocation of memory, and quite frankly I have no clue why the following code won't work.

    Code:
    ...
    	  if ((dMob = malloc(sizeof(*dMob))) == NULL)
    	  {
    		  log(LOG_E, "load_profile: Cannot allocate memory.");
    		  abort();
    	  }
    	  if ((dMob->pcdata = malloc(sizeof(*dMob->pcdata))) == NULL)
    	  {
    		  log(LOG_E, "load_profile: Cannot allocate memory.");
    		  abort();
    	  }
    	  dMob->pcdata->password="Test.";  /*   char *password;    */
    
    ...
    It crashes, and throws a segmentation fault after getting to that last statement there. So whats wrong with this thing? Its not having problems allocating the memory. If I remove the last line, the program continues on fine. Buts its like pcdata wasn't allocated, even though I just allocated it.

    Any help is greatly appreciated. Thank you very much.

    -Jase
    Last edited by Jase; 05-25-2003 at 09:53 AM.
    [ Jase]

  2. #2
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    What kind of data type is pc_data. Is that a pointer to the same structure? If so, you should use sizeof() the whole structure and not the sizeof() a pointer in the structure. Is this going to be a linked list?
    The keyboard is the standard device used to cause computer errors!

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    17
    Yes, its a linked list. To give you an idea of my structures...

    Code:
    struct DMOB
    {
         char     *name;
         ...
         PC_DATA  *pcdata;
    };
    
    struct PC_DATA
    {
         char   *password;
    };
    Like I said, it doesn't seem to have a problem allocating the memory, but when I try to access it, it acts like it wasn't allocated in the first place. -ponder- I'm going to try an if check after the allocation to see if returns NULL or not.
    [ Jase]

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    17
    Alright, heres an update.

    I tried using *dMob->pcdata and PC_DATA when allocating memory for dMob->pcdata.

    I also did an ifcheck after the allocation...


    Code:
    if(dMob->pcdata==NULL){
         log(LOG_S, "Memory was NOT allocated.");
    }
    And... it logged it. Which means it wasn't allocated... which is odd, because when it allocates it there is error handleing incase it couldn't be allocated.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    17
    I found the problem. It had to do with the function clear_mobile which made dMob equal an empty data structure of DMOB, and thus pcdata was allocated, but when dMob took on the values of DMOB *clear_mobile; it made pcdata=NULL.

    Thanks for your help.
    [ Jase]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Replies: 12
    Last Post: 06-24-2005, 04:27 PM
  3. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM
  4. freeing problem with concurrent processes
    By alavardi in forum C Programming
    Replies: 2
    Last Post: 03-07-2005, 01:09 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM