WIll this program cause any memory leak?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sathishc58
    New Member
    • Sep 2007
    • 34

    WIll this program cause any memory leak?

    Hi All

    Please review the program and let me know whether my program will cause memory leak or not.

    1 #include <stdio.h>
    2
    3 struct emp
    4 {
    5 int empno;
    6 char empname[10];
    7 } obj;
    8
    9
    10 void copy(struct emp *);
    11 void freeStruct(stru ct emp);
    12
    13 main()
    14 {
    15 memset(obj, 0x00, sizeof(obj));
    16 copy(&obj);
    17 printf("Main Emp No = %d Emp Name = %s\n", obj.empno, obj.empname);
    18 }
    19
    20 void copy(struct emp *e)
    21 {
    22 struct emp eno;
    23
    24 memset(eno, 0x00, sizeof(eno));
    25 eno.empno = 5;
    26 strcpy(eno.empn ame, "sathish");
    27
    28 memcpy(e, &eno, sizeof(eno));
    29
    30 freeStruct(eno) ;
    31
    32 printf("Copy Emp No = %d Emp Name = %s\n", eno.empno, eno.empname);
    33 }
    34
    35 void freeStruct(stru ct emp eno1)
    36 {
    37 memset(obj, 0x00, sizeof(eno1));
    38 }

    Output

    Segmentation Fault(coredump)

    I am getting the expected output due to some problem.

    Thanks & Regards
    Sathish Kumar
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    Originally posted by sathishc58
    1 #include <stdio.h>
    2
    3 struct emp
    4 {
    5 int empno;
    6 char empname[10];
    7 } obj;
    8
    9
    10 void copy(struct emp *);
    11 void freeStruct(stru ct emp);
    12
    13 main()
    14 {
    15 memset(obj, 0x00, sizeof(obj));
    16 copy(&obj);
    17 printf("Main Emp No = %d Emp Name = %s\n", obj.empno, obj.empname);
    18 }
    Your program is crashing because main is trying to memset() on obj - obj is a TYPE. You need to create an instance of type obj somewhere and memset() on the instance of it.

    for example

    Code:
    int main()
    {
        obj myInst;
        memset( &myInst, 0, sizeof( myInst ));
        ....
    }
    Furthermore, you aren't allocating any memory (yet) so there is no reason to worry about memory leaks. If you declare your variable (myInst) as I did above you still won't have to worry about leaks because you're using stack variables (i.e. you didn't malloc() anything).

    Comment

    • mschenkelberg
      New Member
      • Jun 2007
      • 44

      #3
      Originally posted by mac11
      Your program is crashing because main is trying to memset() on obj - obj is a TYPE. You need to create an instance of type obj somewhere and memset() on the instance of it.

      for example

      Code:
      int main()
      {
          obj myInst;
          memset( &myInst, 0, sizeof( myInst ));
          ....
      }
      Furthermore, you aren't allocating any memory (yet) so there is no reason to worry about memory leaks. If you declare your variable (myInst) as I did above you still won't have to worry about leaks because you're using stack variables (i.e. you didn't malloc() anything).


      Actually, you can declare an instance of a struct immediately after declaration. So in his case obj is an object. There are quite a few problems though.

      1. freeStruct is setting your "obj" to null, so therefore why would you pass in another struct for the size?

      2. when doing memory copies always use the size of the struct. eg sizeof(struct emp);

      3. line 24 needs the address of "eno". As does line 15 need the address of "obj"

      When I fixed those errors I ran the code and got:
      Copy Emp No = 5 Emp Name = sathish
      Main Emp No = 0 Emp Name =

      "eno" gets set, then you copy into "e" which is the address of "obj", then you call freeStruct and set "obj" to NULL, then you output "eno" then output "obj"
      So this is what should happen once you fix your address errors and sizeof errors.


      Oh ya, for the original question, you are not dealing with dynamic memory, so there is no chance for memory leak... if you called malloc on a struct emp * then forgot to call "free" then you would have a memory leak.

      Max

      Comment

      Working...