Prevention of memory leakage

I have a code like below. Here, I have a structure "chair" with a member A and from main, am calling a fucntion "func1" for a number of iterations. Inside func1, I have an integer typeA allocated with 10000 addresses and assigned values. Now, allocated 100 chairs of struct chair. I want each chair to hold 100 distinct addresses of typeA. For example, chair[0] = 0-99 of typeA, chair[1]= 1-199 of type B and so on. Then i use each such chair for parallel processing. Anyhow, am not showing the parallel code here. My problem is, after finishing i am trying to free the memory. But somewhere am getting a memory leakage which extremely slows down my program after few iterations. How to re-implement this code without this leakge problem. Thank u.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  struct chair
{
 int *A;
};

typedef struct chair chair;


int main()
{
  for(unsigned int i=0;i<100;i++)
  {
  func1();
}

return 0;
}

void func1()
{
     
int *typeA;

typeA = (int*)malloc(sizeof(int) * 10000);
for(unsigned int i=0;i<10000;i++)
{
   typeA[i]=i;
}

chair * ch;
// now i define 100 chairs of type chair
ch = (chair*)malloc(sizeof(chair) * 100);
int count =0;

// now i allocare 100 addreess from typeA for each chair (i.e.), ch[0] = 0-99 from typeA, ch[1] = 1-199 from typeA and so on

for(unsigned int i=0;i<100;i++)
{
  ch[i].A =  typeA + count;
  count = count + 100;
  
}


/// Now i parallely process each chair/// am not showing parallel processing

//after parallel processing is over, am doing this...

free(typeA);
free(ch);

//But i get a memory leak in the above for loop so my program is running slolwy after some iterations

}


Last edited on
This is where the problem was for me.

1
2
3
4
5
6
for(unsigned int i=0;i<100;i++)
{
  ch[i].A =  typeA + count;
  count = count + 100;
  
}


Here is my solution maybe this can help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdlib.h>     /* malloc, free, rand */

const int MAX = 100;

typedef struct
{
	int *ptr;
}chair;

void func1()
{
	//MAX
	chair ary[MAX];

	//Array of MAX pointers point to
	for (unsigned int i = 0; i < MAX; i++)
		ary[i].ptr = (int*)malloc(sizeof(int)* 100);

	//Deallocate
	for (unsigned int i = 0; i < MAX; i++)
		free(ary[i].ptr);

}

int main()
{
	func1();
	return 0;
}
Last edited on
Thanks for the reply. Do i need to do the second for loop (Allocation) before the are where i do ch[i].A = typeA + count???,, if that still memory overflow doesn happen??
Your original code is a little unorthodox but it looks okay. Perhaps your parallel processing function is corrupting memory. If you run the program as you've shown it does memory leak?

The way to handle memory leak issues is to figure out who "owns" the memory. That boils down to who has responsibility for deleting it. YOu can also simplify things by using constructors and destructors. For example, in this case maybe every chair has an array of 100 ints. If that's true then allocate the array in the chair constructor and delete it in the destructor.
Now that I am awake... I wanted to look at this again.
Should be able to parallelize this now, fairly easily.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdlib.h>     /* malloc, free, rand */
#include <stdio.h>

const int MAX = 100;

typedef struct{
	int *A;
}Chair;

void func1(){

	Chair ary[MAX];
	int *typeA = (int*)malloc(sizeof(int)* MAX*MAX);
	
	for (int i = 0; i<MAX*MAX; i++)
		typeA[i] = i;

	for (int i = 0, k = 0; i < MAX; i++, k += MAX)
		ary[i].A = &typeA[k];

	/*Verify*/
	for (int i = 0; i<MAX*MAX; i++)
		printf("%d\n", typeA[i]);

	for (int i = 0; i < MAX; i++)
		printf("%d\n", *ary[i].A);

	free(typeA);
}

int main(){

	func1();
	return 0;
}
Last edited on
Topic archived. No new replies allowed.