473,473 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

malloc inside loop

2 New Member
Hi
I have code which looks like this
ABC_STRUCT abc[50];
unsigned int j=0;

for (i=0;i< x;i++) {
char* sub = (char *) malloc (somesize);
strcpy(sub,some source);
abc[j].data= (unsigned char*) sub;
abc[j].len= strlen(sub);
...........
j++
}

for(i=0;i<=j-1;i++)
{
free((POINTER)abc[i].data
}

I just wanted to make sure that the above code is freeing up the memory properly and there are no memory leaks

Thanks in advance
Jan 9 '12 #1
3 3262
weaknessforcats
9,208 Recognized Expert Moderator Expert
The code should be
Expand|Select|Wrap|Line Numbers
  1. abc[i].data = (char *) malloc (somesize);
  2.  strcpy(abc[i].data,some source);
  3.  
The loop should be:
Expand|Select|Wrap|Line Numbers
  1. for (i=0;i< 50;i++) {
  2.  etc...
since there are 50 elements in your array.

You should not keep the strlen as a struct member. The length of
abc[i].data can gotten as needed. Plus if you keep the strlen then yu have to make sure you maintain it whenever abc[i].data changes.

The free would be:

Expand|Select|Wrap|Line Numbers
  1. free(abc[i].data);
  2.  
The loop should be:

Expand|Select|Wrap|Line Numbers
  1. (i=0;i< 50;i++)
  2.  {
  3. etc...
because you have 50 array elements.
Jan 10 '12 #2
star bright
2 New Member
Hi
Thanks for your reply. I understand the code doesn't look perfect because this is not complete code . I pulled out lines which were related to what i am trying to understand which is if the code is similar to what i posted will free function free up memory correctly .

Thanks
Jan 10 '12 #3
donbock
2,426 Recognized Expert Top Contributor
You also need to deal properly with the case when malloc fails. Skip the code that reads or writes via sub when malloc returns NULL; and skip the code that calls free when .data is NULL.
Jan 10 '12 #4

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

Similar topics

2
by: Aggelos | last post by:
Hi Guys!!! Thanks in advance for any kind of help about this problem. The problem: Acces Database One Table 'Orders' and second 'OrderItems' I want to export in txt file using FSO and...
6
by: Vinu | last post by:
Hi, I am maintaining a C++ project which is a server which continuously receives requeste from clients. I have noticed that we overload the new operator and in it then call malloc to allocate...
5
by: rjames.clarke | last post by:
I have the following. $result=mysql_query($sql); $nrows=mysql_num_rows($result); for ($i=0;$i<$nrows;$i++) { $row_array=mysql_fetch_row($result); echo "<form name='testform'...
20
by: spasmous | last post by:
main() { float * f; initialize_f(f); // ...use f for processing free(f); }
6
by: dpswissboy | last post by:
I have a form with 30 input boxes named box1, box2, box3, etc. Instead of: var1 = Request.Form("box1") var2 = Request.Form("box2") etc. ..I am trying to collect the values in a loop statement...
9
by: saravanan82 | last post by:
Hi All, I tried to execute follwoing program, which got killed by the OS. #include<stdlib.h> main() { while(1) { char *c=(char*)malloc(10000); } }
3
by: fishnfrogs | last post by:
Hi, I can't figure out why this isn't working. I'm trying to loop through an array and do a mysql update. However, it doesn't work. for($i = 0; $i < $len; ++$i) { $param = $array . '%';...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.