473,385 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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(struct 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.empname, "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(struct 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
Apr 18 '08 #1
2 3345
mac11
256 100+
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(struct 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

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     obj myInst;
  4.     memset( &myInst, 0, sizeof( myInst ));
  5.     ....
  6. }
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).
Apr 18 '08 #2
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

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     obj myInst;
  4.     memset( &myInst, 0, sizeof( myInst ));
  5.     ....
  6. }
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
Apr 18 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Vijay | last post by:
Hi , I have created a program using CMapStrintToPtr where I would be mapping structure ptr to map string. This is just a sample program which I thought to avoid linear search in link list since...
5
by: Alfonzo Morra | last post by:
MyClass::foo ( const std::string str) { char* s = str.c_str() ; //<- presumably a call to calloc/malloc // behind the scenes ... ... //Do I need a free(s) here to free memory allocated ? } ...
4
by: Jeff Rodriguez | last post by:
If you realloc, and the new memory has a different address, what happens at the old address? Is that memory basically free()'d, or do you need to keep track of address changes and manually free() ?...
27
by: Neil | last post by:
Hello all! I wrote program with a array of pointers, and I suspect they are pointing at each other in the Do ...While loop. Something is messed up with the increment variable word. A program...
22
by: Simon | last post by:
Hi all, I have a huge memory leak problem in what is really very simple data insert code. In my app I'm trying to use a typed dataset to insert into a database. It adds quite a few rows (say...
6
by: Yi | last post by:
There is originally a class called "message". For some reason, I need to add "set<A_update_list;" as a new private data member of "message" ("A" can be IPv4 or IPv6, which are defined elsewhere). ...
16
by: KG | last post by:
Hi, I do have a question. int main() { char *p = (char *)malloc(9); strcpy(p,"TajMahal"); p++;
20
by: gNash | last post by:
Hi all, void main() { char *fp; fp=malloc(26); strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ"); fp='\0'; free(fp); }
6
by: nmehring | last post by:
I have an MFC app with 2000 users. I have one user that experiences a crash in our software anywhere from 1 to 5 times a week when opening a particular module. No other users have reported this...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.