473,383 Members | 1,918 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,383 software developers and data experts.

can't find problem with memory

Hi all,
I'm using a linked list (double). The program is growing(windows xp task
manager) if the showAllListNodes function is activated. But I can't
figure out why. Any ideas???

Thanks
Steven

my code is as follows:

#include <stdio.h>
#include <stdlib.h>

typedef struct ListNode
{
int value;
struct ListNode *previous;
struct ListNode *next;
};

void newListNode(struct ListNode *temp_ls,int value);
void showAllListNodes(void);
void addListNode(int value);
void removeLastListNode(void);

struct ListNode *first_ls_ ;
struct ListNode *last_ls_ ;

int malloc_calls;
int free_calls;

void main(void)
{
int c = 1000;
int i = 13;
int x = 0;

first_ls_= malloc(sizeof(struct ListNode));
first_ls_->previous = NULL;
first_ls_->value = 0;
first_ls_->next = NULL;

last_ls_ = first_ls_;
first_ls_->next = last_ls_;

for (x=0;x<99000;x++)
{

for (i=0;i<c;i++)
{
addListNode(i+1);
}

showAllListNodes();

for (i=0;i<c;i++)
{
removeLastListNode();
}

}

printf("\n Malloc Calls = %d \n",malloc_calls);
printf("\n Free Calls = %d \n",free_calls);
}

void showAllListNodes()
{
struct ListNode *temp_ls = malloc(sizeof(struct ListNode));
malloc_calls++;
temp_ls = first_ls_;

while(temp_ls != NULL)
{
//printf("\n %d =%d= %d
Value=%d\n",temp_ls->previous,temp_ls,temp_ls->next,temp_ls->value);
temp_ls = temp_ls->next;
}

free(temp_ls);
free_calls++;

}

void addListNode(int value)
{
struct ListNode *temp_ls = malloc(sizeof(struct ListNode));
malloc_calls++;
temp_ls->previous = NULL;
temp_ls->value = value;
temp_ls->next = NULL;

temp_ls->previous = last_ls_;
last_ls_->next = temp_ls;
last_ls_ = temp_ls;
}

void removeLastListNode(void)
{
last_ls_ = last_ls_->previous;
free(last_ls_->next);
free_calls++;
last_ls_->next = NULL;

}

Nov 16 '05 #1
7 1223

Steffen Loringer wrote:
Hi all,
I'm using a linked list (double). The program is growing(windows xp task
manager) if the showAllListNodes function is activated. But I can't
figure out why. Any ideas???
<snip> void showAllListNodes()
{
struct ListNode *temp_ls = malloc(sizeof(struct ListNode));
malloc_calls++;
temp_ls = first_ls_;


temp_ls is allocated and leaked in those lines. After the assignment
to temp_ls you no longer have a pointer to the allocated memory.

-David

Nov 16 '05 #2
Thanks for respsonse, David,

the problem occurs in that function at

<snip>
while(temp_ls != NULL)
{
//printf("\n %d
=%d=Value=%d\n",temp_ls->previous,temp_ls,temp_ls->next,temp_ls->value);
temp_ls = temp_ls->next;
}
If I deactviate the loop, the problem does'nt occur. If you only
deactive printf expression the program is not growing.

Thanks
Steven

Nov 16 '05 #3
Ok, found it...must read

struct ListNode *temp_ls;

instead of

struct ListNode *temp_ls = malloc(sizeof(struct ListNode));

Thanks a lot
Steve

Nov 16 '05 #4

Steffen Loringer wrote:
Thanks for respsonse, David,

the problem occurs in that function at

<snip>
while(temp_ls != NULL)
{
//printf("\n %d
=%d=Value=%d\n",temp_ls->previous,temp_ls,temp_ls->next,temp_ls->value);
temp_ls = temp_ls->next;
}
If I deactviate the loop, the problem does'nt occur. If you only
deactive printf expression the program is not growing.

Thanks
Steven


The place I showed you was a memory leak. You are also not freeing the
thing that
was allocated in that function. In abstract, you are doing this:

char *foo = malloc(10);
foo = bar;

You no longer have a pointer to to the memory allocated, it can't ever
be recovered.

Your mistake is allocating anything at all for temp_ls. Why do you
need
a newly allocated node at all to iterate through your nodes?

-David

Nov 16 '05 #5
Your mistake is allocating anything at all for temp_ls. Why do you
need
a newly allocated node at all to iterate through your nodes?

-David


You are right, thanks. Any other recommendations for good coding practice?

Nov 16 '05 #6

Steffen Loringer wrote:
Hi all,

Lower down the thread you asked for other comments
on the code, here are some now that the leak is OK.

I'm using a linked list (double). The program is growing(windows xp task
manager) if the showAllListNodes function is activated. But I can't
figure out why. Any ideas???

Thanks
Steven

my code is as follows:

#include <stdio.h>
#include <stdlib.h>

typedef struct ListNode
{
int value;
struct ListNode *previous;
struct ListNode *next;
};
Um, what are you using typedef for here? Not needed.
Unless you actually do what to typedef it, in which case
you should provide a name for the new type!

void newListNode(struct ListNode *temp_ls,int value);
That isn't used, do you care?
void showAllListNodes(void);
void addListNode(int value);
void removeLastListNode(void);
I'd make all these static unless they are going to be exposed in a
header file.

struct ListNode *first_ls_ ;
struct ListNode *last_ls_ ;
Above should be static. I gather trailing _ is your way to indicate
something
is static? Many people use initial capital, but whatever.

int malloc_calls;
int free_calls;
Above should be static. And end with _ if that is your way to say they
are global.

void main(void)
main should return int. That isn't a portable signature for main.
{
int c = 1000;
int i = 13;
Strange magic numbers should be explained by a comment.
And you set i to 0 anyway later.
int x = 0;

first_ls_= malloc(sizeof(struct ListNode));
malloc can fail, you should check
Generally better malloc paradigm is
first_ls_ = malloc(sizeof *first_ls_)
Why? If you change the type of first_ls_ this still works...

Same malloc comments elsewhere

first_ls_->previous = NULL;
first_ls_->value = 0;
first_ls_->next = NULL;

last_ls_ = first_ls_;
first_ls_->next = last_ls_;
I think you want = NULL here. If you iterate through the list at this
point
you'd loop.

for (x=0;x<99000;x++)
{

for (i=0;i<c;i++)
{
addListNode(i+1);
}

showAllListNodes();

for (i=0;i<c;i++)
{
removeLastListNode();
}

}

printf("\n Malloc Calls = %d \n",malloc_calls);
printf("\n Free Calls = %d \n",free_calls);
}

void showAllListNodes()
{
struct ListNode *temp_ls = malloc(sizeof(struct ListNode));
As seen elsewhere, you don't want to malloc this
malloc_calls++;
temp_ls = first_ls_;

while(temp_ls != NULL)
{
//printf("\n %d =%d= %d
Value=%d\n",temp_ls->previous,temp_ls,temp_ls->next,temp_ls->value);
Should print pointers to %p, and cast to (void*).
temp_ls = temp_ls->next;
}

free(temp_ls);
Don't want to free this either
free_calls++;

}

void addListNode(int value)
{
struct ListNode *temp_ls = malloc(sizeof(struct ListNode));
malloc_calls++;
temp_ls->previous = NULL;
temp_ls->value = value;
temp_ls->next = NULL;

temp_ls->previous = last_ls_;
last_ls_->next = temp_ls;
last_ls_ = temp_ls;
}

void removeLastListNode(void)
{
last_ls_ = last_ls_->previous;
free(last_ls_->next);
free_calls++;
last_ls_->next = NULL;
If you are removing the first list node, seems like last_ls_ will be
NULL
here and you have troubles dereferencing it.

}


Nov 16 '05 #7
Steffen Loringer wrote
(in article <3u************@news.dfncis.de>):
Your mistake is allocating anything at all for temp_ls. Why do you
need
a newly allocated node at all to iterate through your nodes?

-David


You are right, thanks. Any other recommendations for good coding practice?


Yes, stop using 'void main(void)' unless you are working on an
embedded project (unlikely, since you are using malloc() and
friends).

It doesn't matter what your C book or your instructor say, 'void
main...' is not legit in standard C apart from some embedded
scenarios.

If you do not intend to use command line arguments at all, then
int main(void)
is the proper form.

--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Nov 19 '05 #8

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

Similar topics

4
by: Greg Baker | last post by:
I don't know what standard protocol is in this newsgroup. Am I allowed to post code and ask for help? I hope so.. :) Here's my problem: I am trying problem 127 of the valladolid online...
7
by: Claire | last post by:
Im sat here watching task manager and the memory consumption of my application rising second by second. What tools are there out there for me to use to find where it's all going please? (I wish...
25
by: Zeng | last post by:
I finally narrowed down my code to this situation, quite a few (not all) of my CMyClass objects got hold up after each run of this function via the simple webpage that shows NumberEd editbox. My...
9
by: charliewest | last post by:
Hello - I have images saved in my SQL SERVER 2000 database. Using ASP.NET (C#) is there any way to temporarily save an image to a session object, and after running some other operations, later...
2
by: Dips | last post by:
Hello All, Does any of you know of any tool in the market to identify which Process/Software is leaking memory. The PDA is Samsung 730 , Pocket PC 2003 OS. The Device does not leak any memory if...
4
by: comp.lang.php | last post by:
I downloaded the tarball and while was able to compile within PHP with no problems, I am having memory timeout issues involving image manipulation with extremely large images (800K - 2mb). ...
21
by: Steven T. Hatton | last post by:
I'm trying to improve my formal understanding of C++. One significant part of that effort involves clarifying my understanding of the vocabulary used to describe the language. This is from the...
48
by: Ward Bekker | last post by:
Hi, I'm wondering if the GC.Collect method really collects all objects possible objects? Or is this still a "smart" process sometimes keeping objects alive even if they can be garbage collected?...
65
by: Chris Carlen | last post by:
Hi: From what I've read of OOP, I don't get it. I have also found some articles profoundly critical of OOP. I tend to relate to these articles. However, those articles were no more objective...
2
by: Tom Shelton | last post by:
On 2008-04-15, DR <softwareengineer98037@yahoo.comwrote: Where are you seeing that? In the task manager? If so, then you are looking in the wrong place. Let me tell you a little something...
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.