Thread: Problem with Structure inside a structure

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    7

    Problem with Structure inside a structure

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct cachecontents
    {
       int bstart;
       int bcount;
       struct cachecontents *next;
    };
    
    typedef struct cache_unit  // Cached unit for a given disk
    {
       int unitno;
       struct cachecontents *contents;
       struct cache_unit *next;
    }  cache;
    cache *logorgcache[8]={NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
    
    void cache_insert(int devno, int unitno, int bstart, int bcount)
    {
       cache *temp = (cache *)malloc(sizeof(*temp));
       cache *curr = (cache *)malloc(sizeof(*curr));
       curr->unitno = unitno;
       curr->contents->bstart = bstart;
       curr->contents->bcount = bcount;
       curr->contents->next=NULL;
       curr->next=NULL;
    
    
       if ((logorgcache[devno] == NULL) || (unitno < logorgcache[devno]->unitno)) // Inserting at Unit Level
       {
          curr->next=logorgcache[devno];
          logorgcache[devno]=curr;
       }
       else
       {
          temp = logorgcache[devno];
          while ((temp->next) && (unitno >= temp->unitno))
          {
             temp = temp->next;
          }
          curr->next = temp->next;
          temp->next = curr;
       }
    }
    
    int main()
    {
    cache_insert(0,1, 32, 4);
    return 0;
    }
    Compiles perfectly. But when I try to run/debug the program it is giving a segmentation fault(cannot access memory at address 0X04...) while running the line marked in blue. Any suggestions would be greatly appreciated.
    Last edited by purush12; 05-16-2011 at 07:30 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Where have you allocated any memory for the structure contents?

    Jim

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    7
    Quote Originally Posted by jimblumberg View Post
    Where have you allocated any memory for the structure contents?

    Jim
    Jim, thanks for your response. When I malloc'ed the outer structure won't it allocate a memory for the internal structure?? If not where can i do that?

    Thanks once again

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > cache *temp = (cache *)malloc(sizeof(*temp));
    Why are you using malloc in a C++ program?

    Why not make cache_unit and cachecontents into classes, with constructors and so forth?
    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.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    7
    Quote Originally Posted by Salem View Post
    > cache *temp = (cache *)malloc(sizeof(*temp));
    Why are you using malloc in a C++ program?

    Why not make cache_unit and cachecontents into classes, with constructors and so forth?
    salem, thanks for your response. I need to integrate this code to a C based simulator. So, keeping the code C "look like"

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    So write it in C then.

    Read this, if you're still under the illusion that C and C++ are mostly the same.
    Incompatibilities Between ISO C and ISO C++
    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.

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    7
    Quote Originally Posted by Salem View Post
    So write it in C then.

    Read this, if you're still under the illusion that C and C++ are mostly the same.
    Incompatibilities Between ISO C and ISO C++
    salem, thanks for the link I made the changes and it still pukes the same error

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Jim, thanks for your response. When I malloc'ed the outer structure won't it allocate a memory for the internal structure??
    No it won't allocate memory for the internal structure, if you use a pointer you must allocate memory for it.
    If not where can i do that?
    Somewhere after you initialize the external structure and before you try to use the internal structure?

    Also please don't edit your post to change the code. That makes it very hard to follow the changes. Just post the new code.

    Jim

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    7
    Quote Originally Posted by jimblumberg View Post
    No it won't allocate memory for the internal structure, if you use a pointer you must allocate memory for it.

    Somewhere after you initialize the external structure and before you try to use the internal structure?

    Also please don't edit your post to change the code. That makes it very hard to follow the changes. Just post the new code.

    Jim
    Jim, Thanks for the reply.

    I can init the internal structure say like,
    cachecontents contents = (cachecontents *)malloc(sizeof(*contents));

    but how can i link it to the external structure.

    Sorry for my ignorance.

  10. #10
    Registered User
    Join Date
    May 2011
    Posts
    7
    Quote Originally Posted by jimblumberg View Post
    No it won't allocate memory for the internal structure, if you use a pointer you must allocate memory for it.

    Somewhere after you initialize the external structure and before you try to use the internal structure?

    Also please don't edit your post to change the code. That makes it very hard to follow the changes. Just post the new code.

    Jim
    Jim, Thanks for the reply.

    I can init the internal structure say like,
    cachecontents contents = (cachecontents *)malloc(sizeof(*contents));

    but how can i link it to the external structure.

    Sorry for my ignorance.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by purush12 View Post
    Jim, Thanks for the reply.

    I can init the internal structure say like,
    cachecontents contents = (cachecontents *)malloc(sizeof(*contents));

    but how can i link it to the external structure.

    Sorry for my ignorance.
    You don't want to "link" it to your node in the sense of a linked list. You want to change the value of curr->contents to be a valid pointer. So, change the value of curr->contents.

  12. #12
    Registered User
    Join Date
    May 2011
    Posts
    7
    My bad, thanks guys it worked

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-25-2010, 10:57 AM
  2. Defining structure inside another structure
    By mkdl750 in forum C Programming
    Replies: 1
    Last Post: 07-20-2008, 07:50 AM
  3. Problem with arrays inside a structure
    By babu in forum C Programming
    Replies: 4
    Last Post: 07-12-2007, 09:35 AM
  4. Structure inside Structure
    By tdep in forum C Programming
    Replies: 11
    Last Post: 07-05-2007, 08:23 AM
  5. Replies: 4
    Last Post: 11-22-2006, 12:20 PM