473,732 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1483
Hi,

Forgot to post my addContact function just in case.

Thanks,

Andre

struct ContactNode * addContact( struct ContactNode *
ContactListPrev ious,
struct ContactNode * ContactListHead ,
CONTACT_ENTRY * Contact )
{
if( !ContactListHea d )
{
struct ContactNode * tempContactNode =
( struct ContactNode * ) malloc( sizeof( struct ContactNode )
);

memcpy( &tempContactNod e->Contact, Contact, sizeof( CONTACT_ENTRY
) );

tempContactNode->Previous = ContactListPrev ious;
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
2079
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 to get a working system just about finished. I am building an interface for our customer service folks to use to manage registered customers and am seeing some weird behavior.
7
3401
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 with no underline and then an underline for hover...but oddly, only ONE link will actually show the underline on hover. All the rest, even with the same HTML, won't pop up an underline. I just don't get it! If I could get any advice I'd really...
7
631
by: Shawn Windle | last post by:
----begin node.h-------- #ifndef NODE_H #define NODE_H #include <iostream> //NULL using namespace std; class node {
4
2084
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, dtDateTermSent and the tables are joined on the LO's name. I need to create two queries for this.. one for when I tell NH the LO is hired, and one for when I tell them they're fired. It's the hiring one I can't get straight. Basically, I need listed on...
5
1580
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 pdf in a folder instead of the usual html page. When I try to test the link an authentication dialog box pops up requesting username and password. Since the webuser has to log in to the main site anyway, I just want the file to pop up...
1
7736
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 - <a href="ApplicationHost.aspx">Open Application</a> 2. The ApplicationHost.aspx has the following code in its Load event handler - 'ASSUMPTION -
7
1883
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 continue. The "click here to continue" line is a link, when people click on it, you guess it: a pop up menu will apear... The old script I used was:
4
1779
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 varies in size if you scroll or move it around or refresh and stuff. HTML: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html>
14
2022
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 first link class on any page. Any thoughts? As a side thought, in my style sheet I changed what I guess was the "Default" link style (as seen below). Was I wrong in doing that?
0
8773
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9445
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9234
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9180
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6733
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6030
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4805
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2177
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.