Memory allocation for Structure Pointer within Structure Pointer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • djcm75
    New Member
    • Dec 2011
    • 3

    Memory allocation for Structure Pointer within Structure Pointer

    typedef struct siblings {
    char sibname[20];
    char gender;
    }sib;


    typedef struct employee {
    char empid[10];
    char empname[20];
    char status;
    float sal;
    sib ** s;
    }emp;

    typedef struct stackemp {
    int size;
    int ptrPos;
    emp ** e;
    }stack;

    function myfunction(stac k * s) {
    (s->e[s->size]) = (emp *) malloc(sizeof(e mp)); //this line works without any errors

    (s->e[s->size]->s[0]) = (sib *)malloc(sizeof (sib)); //this line displays segmenation fault during run time
    .....
    ....
    }

    This code compiles without errors but displays "Segmentati on Fault" during runtime. How do I assign memory for the double pointer 's'
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Where are you allocating s->e ?

    That is, I don't see where you are allocating an array of emp*. Not doing that makes e[s->size] invalid.

    Comment

    • johny10151981
      Top Contributor
      • Jan 2010
      • 1059

      #3
      to add note with weaknessforcats

      Code:
      s->e[[B]s->size[/B]]->s[0]
      The length of e is s->size; and remember in c/c++ array always start with zero and end in length-1, i.e. in here s->size-1; s->size th point is definitely invalid

      Comment

      • djcm75
        New Member
        • Dec 2011
        • 3

        #4
        I did not post my entire code

        allocation for s->e ? complies & runs perfectly

        s->e[s->size] = (emp *)malloc(sizeof (emp));
        //where s->size has already been incremented

        I am having problem only with
        (s->e[s->size]->s[0]) = (sib *)malloc(sizeof (sib));
        no compilation during run time i get sementation fault

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          malloc(sizeof(s ib)) should allocate 4 bytes on a 32-bit OS since sib is a pointer.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Do you verify that none of your malloc calls returned NULL?
            You shouldn't dereference any pointer until you have reason to believe it is not NULL.

            Comment

            • djcm75
              New Member
              • Dec 2011
              • 3

              #7
              Solved

              typedef struct siblings {
              char sibname[20];
              char gender;
              }sib;


              typedef struct employee {
              char empid[10];
              char empname[20];
              char status;
              float sal;
              sib ** s;
              }emp;

              typedef struct stackemp {
              int size;
              int ptrPos;
              emp ** e;
              }stack;

              function myfunction(stac k * s) {
              (s->e[s->size]) = (emp *) malloc(sizeof(e mp)); //this line works without any errors

              (s->e[s->size]->s) = malloc(sizeof(s->e[s->size]->s[0]));
              (s->e[s->size]->s[0]) = malloc(sizeof(s ib));
              (s->e[s->size]->s[1]) = malloc(sizeof(s ib));

              .....
              ....
              }

              Comment

              Working...