473,385 Members | 1,593 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.

Weird link list problem

Hi,

I have a linked list with 705 nodes, and the functions getContact and
listContact to deal with it. listContact works properly, and prints all
some debug information about each node. On the other hand, getContact
breaks when it reaches the 580th node although it's almost identical to
listContact .
Does anyone have any idea about what is wrong?

Thanks,

Andre

#define MAX_STR 127

typedef struct
{
char ContactName[MAX_STR + 1];

}CONTACT_ENTRY;

struct ContactNode
{
struct ContactNode * Next;
struct ContactNode * Previous;
CONTACT_ENTRY Contact;
};

void listContact( struct ContactNode * ContactListHead, unsigned int
counter )
{
FILE * pFile = fopen("./test.txt", "a");
fprintf( pFile, "%04d %07X %07X %07X\n",
counter,
ContactListHead->Previous,
ContactListHead,
ContactListHead->Next);
fflush(pFile);
fclose(pFile);

if( ContactListHead->Next != 0 )
{
listContact( ContactListHead->Next, ++counter );
}
}

CONTACT_ENTRY getContact( struct ContactNode * ContactListHead,
unsigned int counter )
{
FILE * pFile = fopen("./test.txt", "a");
fprintf( pFile, "%04d %07X %07X %07X\n",
counter,
ContactListHead->Previous,
ContactListHead,
ContactListHead->Next);
fflush(pFile);
fclose(pFile);

if( ContactListHead->Next != 0 )
{
getContact( ContactListHead->Next, ++counter );
}

return ContactListHead->Contact;
}

Nov 3 '06 #1
2 1465
Hi,

Forgot to post my addContact function just in case.

Thanks,

Andre

struct ContactNode * addContact( struct ContactNode *
ContactListPrevious,
struct ContactNode * ContactListHead,
CONTACT_ENTRY * Contact )
{
if( !ContactListHead )
{
struct ContactNode * tempContactNode =
( struct ContactNode * ) malloc( sizeof( struct ContactNode )
);

memcpy( &tempContactNode->Contact, Contact, sizeof( CONTACT_ENTRY
) );

tempContactNode->Previous = ContactListPrevious;
tempContactNode->Next = 0;

return tempContactNode;
}
else
{
ContactListHead->Next = addContact( ContactListHead,
ContactListHead->Next, Contact );

return ContactListHead;
}
}

si******@yahoo.com wrote:
Hi,

I have a linked list with 705 nodes, and the functions getContact and
listContact to deal with it. listContact works properly, and prints all
some debug information about each node. On the other hand, getContact
breaks when it reaches the 580th node although it's almost identical to
listContact .
Does anyone have any idea about what is wrong?

Thanks,

Andre

#define MAX_STR 127

typedef struct
{
char ContactName[MAX_STR + 1];

}CONTACT_ENTRY;

struct ContactNode
{
struct ContactNode * Next;
struct ContactNode * Previous;
CONTACT_ENTRY Contact;
};

void listContact( struct ContactNode * ContactListHead, unsigned int
counter )
{
FILE * pFile = fopen("./test.txt", "a");
fprintf( pFile, "%04d %07X %07X %07X\n",
counter,
ContactListHead->Previous,
ContactListHead,
ContactListHead->Next);
fflush(pFile);
fclose(pFile);

if( ContactListHead->Next != 0 )
{
listContact( ContactListHead->Next, ++counter );
}
}

CONTACT_ENTRY getContact( struct ContactNode * ContactListHead,
unsigned int counter )
{
FILE * pFile = fopen("./test.txt", "a");
fprintf( pFile, "%04d %07X %07X %07X\n",
counter,
ContactListHead->Previous,
ContactListHead,
ContactListHead->Next);
fflush(pFile);
fclose(pFile);

if( ContactListHead->Next != 0 )
{
getContact( ContactListHead->Next, ++counter );
}

return ContactListHead->Contact;
}
Nov 3 '06 #2


si******@yahoo.com wrote On 11/03/06 15:52,:
Hi,

I have a linked list with 705 nodes, and the functions getContact and
listContact to deal with it. listContact works properly, and prints all
some debug information about each node. On the other hand, getContact
breaks when it reaches the 580th node although it's almost identical to
listContact .
Does anyone have any idea about what is wrong?
[code snipped; see up-thread]
You didn't describe the manner in which it "breaks,"
but I'll make a guess: Are you getting some kind of stack
overflow?

A function in C needs to keep track of the location it
will return to, and the mechanism to do this is often
implemented as a stack. Also, a function that returns a
value (especially an aggregate value) may use additional
stack space in the caller to hold the returned value.

Now, you're using recursion to traverse your linked
list, and this means you need as many "stack frames" as
there are list nodes (plus or minus a few, depending on
how you detect end-of-list, and plus whatever is used by
the functions "above" the traversal and the functions
called during the traversal). Your listContact() function
returns nothing, and thus probably uses very little stack
space per call. However, getContact() returns a struct of
128 bytes or more, so each call needs to set aside some
space where that struct can be returned. Therefore, you
can expect getContact() to use at least 90KB more stack
space than listContact(), and if you're close to the edge
already this may well push you over it.

(All these calculations should be treated with some
caution. Different C implementations on different machines
do things differently out of necessity, and they'll have
different mechanisms for returning structs, using different
amounts of stack space. There might not even be a "stack"
at all! Still, the mechanisms I'm describing are fairly
typical, and the size penalty on your machine isn't likely
to differ from my calculations by more than a few multiples
of Finagle's Variable Constant.)

The problem (or *a* problem) with your approach is that
you're using recursion for a task that is more naturally
iterative. Think about it for a moment: in an iterative
solution, you'll just sit inside one function level instead
of descending and descending and descending. You won't need
to pass that `counter' as an argument in every call; it can
just be a local variable in the function. And you won't
need to keep opening, writing, closing, reopening, rewriting,
and reclosing that output file time and time and time again;
you'll just open it once near the beginning of the function,
loop through the list writing out each node, and close the
file once when you've reached the end. Lots less overhead.

By the way, "%d" is not the right way to print an unsigned
value: use "%u". And "%X" is not the right way to print a
pointer: use "%p" and apply a `(void*)' cast to the value.

--
Er*********@sun.com

Nov 3 '06 #3

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

Similar topics

3
by: redneck_kiwi | last post by:
Hi all: I have a really weird problem. I am developing a customer catalog system for my company and as such have delved into sessions for authentication and access levels. So far, I have managed...
7
by: LRW | last post by:
Below I'll paste my CSS and the HTML in question. But what's happening is, I'm trying to establish a link behavior for a class that's separate from the normal link class. I've established a: 's...
7
by: Shawn Windle | last post by:
----begin node.h-------- #ifndef NODE_H #define NODE_H #include <iostream> //NULL using namespace std; class node {
4
by: Andromeda | last post by:
I have two tables... one contains all the loan officer information (name/address/phone/email/etc) the other is a NH license table which lists has 3 columns - txtLOName, dtDateHireSent,...
5
by: Nevets Steprock | last post by:
I have been building a website diligently for the past three months and everything has been working well so far. Yesterday, I added a link on my javascript menu. This link is supposed to go to a...
1
by: Dees | last post by:
Hi, I am facing a weird problem with HTTPS and Request.Url.AbsoluteUri in my ASP.NET application. Here is the scenario - 1. I have a menu page (Default.aspx), which has the following anchor -...
7
by: JJ | last post by:
Dear all, I have this weird problem on the website I maintain.... A few visitors seemed to be unable to XS the website. It has a front image, with a link underneeth it saying: click here to...
4
by: Mark | last post by:
Hi.. I changed some of my CSS, and now I get this strange problem in IE. See link: http://xailus.com/files/sample.gif. Left is firefox, what it should look like, right is IE. The footer even...
14
by: Daniel Kaplan | last post by:
So my style sheet has many different types of LINK styles. And they all work fine on IE, even different styles on the same page. But in Firefox, all link throughout my site all follow JUST the...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.