473,513 Members | 4,116 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

freeing allocated memory

for(;;)
{
c = a * b;
c %= 100000;
printf("%9d", c);
d = int2str(c);
left = midstr(d, 0, 3);
right = midstr(d, strlen(d)-3, 3);
free(d);
a = atoi(left);
b = atoi(right);
if(a * b == 0)
break;
printf("%6d %6d\n", a, b);
}
free(left);
free(right);

In the code fragment above d is a buffer allocated by a call to
malloc in int2str and freed each pass through the loop. left and
right are also buffers allocated by calls to malloc in midstr,
but they are freed only once when execution breaks out of the
loop. What happens to the memory that is allocated to left and
right in each call to midstr? Is allocated memory getting cleaned
up properly in this code?

Nov 14 '05 #1
7 1665
Curley Q. <cu*****@bogus.net> spoke thus:
In the code fragment above d is a buffer allocated by a call to
malloc in int2str and freed each pass through the loop. left and
right are also buffers allocated by calls to malloc in midstr,
but they are freed only once when execution breaks out of the
loop. What happens to the memory that is allocated to left and
right in each call to midstr? Is allocated memory getting cleaned
up properly in this code?


This sounds very homework-like, so I'll just tell you that the
answers are "bad things" and "hell no", respectively.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #2
Christopher Benson-Manica wrote:
Curley Q. <cu*****@bogus.net> spoke thus:

In the code fragment above d is a buffer allocated by a call to
malloc in int2str and freed each pass through the loop. left and
right are also buffers allocated by calls to malloc in midstr,
but they are freed only once when execution breaks out of the
loop. What happens to the memory that is allocated to left and
right in each call to midstr? Is allocated memory getting cleaned
up properly in this code?

This sounds very homework-like, so I'll just tell you that the
answers are "bad things" and "hell no", respectively.


Not homework. They only teach VisualBasic and Java in my school.
Your answer to question #2 was OK, but maybe somebody could do a
better job with #1.

Nov 14 '05 #3
Curley Q. <cu*****@bogus.net> spoke thus:
for(;;)
{
c = a * b;
c %= 100000;
printf("%9d", c);
d = int2str(c);
What if malloc() fails (returns NULL)? Are you handling that
(unlikely) occurrence?
left = midstr(d, 0, 3);
Whatever memory left pointed to before is lost forever; you'll never
be able to free it. Same for the line below. Call free beforehand,
though, and all is well.

free( right ); right = midstr(d, strlen(d)-3, 3);
What if left and right are NULL? You should check.
free(d);
a = atoi(left);
b = atoi(right);
if(a * b == 0)
break;
printf("%6d %6d\n", a, b);
}
free(left);
free(right);
All the memory you lost track of (and didn't free) is presumably
returned to the operating system once your program exits, but you
should manage it judiciously yourself.
What happens to the memory that is allocated to left and
right in each call to midstr?
It stays allocated, and with no pointer to it, you can't free it.
Is allocated memory getting cleaned
up properly in this code?


No; see above.

I'm sorry to hear they don't teach C at your school - the tragedy
continues, apparently.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #4
Christopher Benson-Manica wrote:
All the memory you lost track of (and didn't free) is presumably
returned to the operating system once your program exits, but you
should manage it judiciously yourself.

What happens to the memory that is allocated to left and
right in each call to midstr?

It stays allocated, and with no pointer to it, you can't free it.

Thanks, that's what I wanted to know.
I'm sorry to hear they don't teach C at your school - the tragedy
continues, apparently.

A member of the faculty told me C was "obsolete." I guess I'm
just old-fashioned.

Cheers,
CQ


Nov 14 '05 #5
"Curley Q." <cu*****@bogus.net> wrote in message
news:40**************@bogus.net...
Christopher Benson-Manica wrote:
I'm sorry to hear they don't teach C at your school - the tragedy
continues, apparently.

A member of the faculty told me C was "obsolete." I guess I'm
just old-fashioned.


Sounds like your faculty is obsolete as well, but IMHO that's rather common.

Every tool has a use; C is useful for many things Java and VB can't do --
for instance, writing VB and Java interpreters and the OSes they run on.

S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin

Nov 14 '05 #6
On Wed, 28 Apr 2004 21:32:32 +0000, Curley Q. wrote:
Christopher Benson-Manica wrote:
All the memory you lost track of (and didn't free) is presumably
returned to the operating system once your program exits, but you
should manage it judiciously yourself.

What happens to the memory that is allocated to left and
right in each call to midstr?

It stays allocated, and with no pointer to it, you can't free it.

Thanks, that's what I wanted to know.


BTW, this is what people call a 'memory leak'.

(Admittedly, people throw in some other qualifiers as well, but this isn't
an X-rated newsgroup. ;))
I'm sorry to hear they don't teach C at your school - the tragedy
continues, apparently.
A member of the faculty told me C was "obsolete." I guess I'm
just old-fashioned.


That member of the faculty is broken and needs to be fixed. (Er, repaired.)

C is still the language of choice for many, many things. OS design, for
example, is 90% C and 10% machine-specific assembly. (Well, percentages
vary, but that's becoming a common ratio.) This means that the OS's
structure is portable between machines, so you can run the same OS on your
Cray supercomputer, your System/360 legacy system, and your embedded
controllers. See NetBSD for an example of this philosophy.

Embedded systems are usually programmed in something besides pure assembly
these days, too, and you can do a lot worse than C. (You can use BASIC,
for example. ;)) C's standards impose as little on the compiler writers as
possible, and the C virtual machine is largely compatible in philosophy
with most hardware machines. (Yes, C has a virtual machine, it simply
usually isn't explicitly implemented other than via libraries. C provides
a flat, indefinite-length memory model, good support for floating-point
numbers, and the ability to address chars at arbitrary memory locations
within an array. Some systems have to go through contortions to meet these
demands, but many of them fit this model quite naturally.)

Cheers,
CQ


--
yvoregnevna gjragl-guerr gjb-gubhfnaq guerr ng lnubb qbg pbz
To email me, rot13 and convert spelled-out numbers to numeric form.
"Makes hackers smile" makes hackers smile.

Nov 14 '05 #7
August Derleth wrote:
C provides a flat, indefinite-length memory model


Locally flat, please.

--
++acr@,ka"
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
22962
by: disco | last post by:
I am working on this example from a book "C Primer Plus" by Prata 4th edition - p. 672. There is no erata on this problem at the publisher's website. 1) Is it a violation of copyright laws to post example code from a book to a newsgroup? 2) The program crashes as it tries to free memory and I would like to know the best way to correct...
3
2569
by: Hassan Iqbal | last post by:
Hi, (1)if i do the memory allocation like: int **p; p= malloc(n*sizeof p); for(i=0;i<n;i++) /* i and n are integers */ p= malloc(n*sizeof p); then in that case is this command sufficient to free all the allocated memory:
11
2263
by: Rodrigo Dominguez | last post by:
there are sometimes that I use third party libraries, I use some functions that returns char * or structs, etc. sometimes the memory that is returned by those libraries, when I try to free this memory whith the function free, it brokes my application, and sometimes it's ok, why? how do I realize when I have to free the memory that is allocated...
6
2747
by: Fernando Cacciola | last post by:
Help me out here please: While watching Brad Abraham's MSDN TV talk about the Dispose pattern, refering to: public virtual void Dispose ( bool disposing ) { if ( disposing ) { <-- WHAT GOES HERE -->
2
1672
by: tristan.chaplin | last post by:
I have an unmanaged dll that returns a pointer (that it allocates, I didn't allocate it with Marshal.AllocHGlobal), how do I free the memory when I'm finished with it? It doesn't seem to like me calling Marshal.FreeHGlobal on it. I have heard rumours that the dll needs to supply its own destructor function - is that true?
6
2023
by: Praetorian | last post by:
This is actually 2 questions: The first one: I have a function (FuncA) calling another (FuncB) with a set of parameters which also includes an int * initialized to NULL before the call. Now FuncB looks at the other parameters and decides whether or not to allocated memory to the int * passed to it (using calloc()). One of the other...
66
3635
by: karthikbalaguru | last post by:
Hi, Will 'free' return the memory Immediately to the OS ? Thx in advans, Karthik Balaguru
11
1981
by: vivek | last post by:
Hello, I have a pointer to a main structure which again consists of structures, enums, char, int, float and again complex structures. When i free all the contents of the main structure, it takes me a lot of time (since i have to loop determining the data type and freeing it). Is there any idea to free all the contents of the structure in...
25
4682
by: Andreas Eibach | last post by:
Hi again, one of the other big woes I'm having... typedef struct perBlockStru /* (structure) Long words per block */ { unsigned long *lword; } lwperBlockStru_t;
0
7270
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7563
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7125
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7543
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5102
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4757
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3252
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
1612
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 we have to send another system
1
813
muto222
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.