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

Problem with Linked List

I made a linked list that can add an item, go to the first item, and
get the current, next, and previous item in the list. When I compile
it, there are no errors, but it overloads. Here is the code:

//fnamelink.h------------------------------------------------------
class flink
{
public:
char data[_MAX_DIR+1];
flink* previous;
flink* next;
flink()
{
previous = NULL;
next = NULL;
}
};

class fnamelink
{
private:
flink* first;
public:
fnamelink()
{ first = NULL; }
void addItem(char* foldername);
char* getCurrent();
char* getPrevious();
char* getNext();
char* getFirst();
void deleteLink();
~fnamelink()
{
getFirst();
deleteLink();
}
};

//
fnamelink.cpp-------------------------------------------------------------------------------------------------
#include <string.h>
#include <malloc.h>
#include "StdAfx.h" //<--Includes #include "fnamelink.h"

void fnamelink::addItem(char* foldername)
{
flink* newlink = new flink;
memset(newlink->data, NULL, sizeof(newlink->data));
strcpy(newlink->data, foldername);
newlink->previous = first;
if(first)
first->next = newlink;
first = newlink;
}

char* fnamelink::getCurrent()
{
flink* current = first;
if(current != NULL)
return current->data;
else
return NULL;
}

char* fnamelink::getPrevious()
{
flink* current = first;
current = current->previous;
if(current->previous == NULL)
{
first = current;
return NULL;
}
if(current->previous != NULL)
return current->data;
else
return NULL;
}

char* fnamelink::getNext()
{
flink* current = first;
if((current = current->next) == NULL)
{
return NULL;
}
if(current != NULL)
return current->data;
else
return NULL;
}

char* fnamelink::getFirst()
{
flink* current = first;
if(current)
{
= while(current->previous != NULL)
{
current = current->previous;
}
}
first = current;
if(first)
return first->data;
else
return NULL;
}

void fnamelink::deleteLink()
{
int numToDel = 0;
flink* current = first;
if(current->next)
{
while(current->next != NULL)
{
current = current->next;
first = current;
numToDel++;
}
while((numToDel) >= 0)
{
numToDel--;
first = first->previous;
free(current);
current = first;
}
free(current);
}
else
{
first = first->previous;
free(current);
}
}
The '=>' I placed in the code was the exception the debugger pointed
out, but I can't seem to find a problem with it. Any help would be
greatly appreciated.

Sep 16 '07 #1
5 1412
flink* newlink = new flink;
free(current);
When you allocate using new, you de-allocate using delete, not free.

(I haven't checked for any other errors.)

-Howard

Sep 17 '07 #2
"oceanspell" <ra************@gmail.comwrote in message
news:11**********************@o80g2000hse.googlegr oups.com...
>I made a linked list that can add an item, go to the first item, and
get the current, next, and previous item in the list. When I compile
it, there are no errors, but it overloads. Here is the code:

//fnamelink.h------------------------------------------------------
class flink
{
public:
char data[_MAX_DIR+1];
flink* previous;
flink* next;
flink()
{
previous = NULL;
next = NULL;
}
};

class fnamelink
{
private:
flink* first;
public:
fnamelink()
{ first = NULL; }
void addItem(char* foldername);
char* getCurrent();
char* getPrevious();
char* getNext();
char* getFirst();
void deleteLink();
~fnamelink()
{
getFirst();
deleteLink();
}
};

//
fnamelink.cpp-------------------------------------------------------------------------------------------------
#include <string.h>
#include <malloc.h>
#include "StdAfx.h" //<--Includes #include "fnamelink.h"

void fnamelink::addItem(char* foldername)
{
flink* newlink = new flink;
memset(newlink->data, NULL, sizeof(newlink->data));
strcpy(newlink->data, foldername);
newlink->previous = first;
if(first)
first->next = newlink;
first = newlink;
}

char* fnamelink::getCurrent()
{
flink* current = first;
if(current != NULL)
return current->data;
else
return NULL;
}

char* fnamelink::getPrevious()
{
flink* current = first;
current = current->previous;
if(current->previous == NULL)
{
first = current;
return NULL;
}
if(current->previous != NULL)
return current->data;
else
return NULL;
}

char* fnamelink::getNext()
{
flink* current = first;
if((current = current->next) == NULL)
{
return NULL;
}
if(current != NULL)
return current->data;
else
return NULL;
}

char* fnamelink::getFirst()
{
flink* current = first;
if(current)
{
= while(current->previous != NULL)
{
current = current->previous;
}
}
first = current;
if(first)
return first->data;
else
return NULL;
}

void fnamelink::deleteLink()
{
int numToDel = 0;
flink* current = first;
if(current->next)
{
while(current->next != NULL)
{
current = current->next;
first = current;
numToDel++;
}
while((numToDel) >= 0)
{
numToDel--;
first = first->previous;
free(current);
current = first;
}
free(current);
}
else
{
first = first->previous;
free(current);
}
}
The '=>' I placed in the code was the exception the debugger pointed
out, but I can't seem to find a problem with it. Any help would be
greatly appreciated.
You are attempting to dereference a NULL pointer. Lets look at one of your
methods:

char* fnamelink::getPrevious()
{
flink* current = first;
current = current->previous;
if(current->previous == NULL)
{
first = current;
return "NULL";
}
if(current->previous != NULL)
return current->data;
else
return "NULL";
}

flink* current = first;
at this point first may be NULL if there are no items in the list.
current = current->previous;
if first was NULL, you just attempted to use a NULL pointer. This will
abend (abnormal end). Now, lets presume we have 1 item in the list, so
first was not null. But since there's only one item, previous will be NULL.
So current is now NULL. So we get to the next line:
if(current->previous == NULL)
Ooops, current is NULL Again, you are attempting to derefence a NULL
pointer. You didn't check if current was NULL first.

These types of errors are all over the code in all the functions.
Sep 17 '07 #3

"Jim Langston" <ta*******@rocketmail.comwrote in message
news:WS*************@newsfe02.lga...

>char* fnamelink::getFirst()
{
flink* current = first;
if(current)
{
= while(current->previous != NULL)
{
current = current->previous;
}
}
first = current;
if(first)
return first->data;
else
return NULL;
}
You are attempting to dereference a NULL pointer. Lets look at one of
your methods:
You're correct, of course, but interestingly, the location of the reported
error is in the one function which does NOT allow a NULL pointer to be
dereferenced! :-) (But once you've got undefined behavior, nothing else can
be trusted, so the location of the crash *could* be just about anywhere.)

-Howard

Sep 17 '07 #4
"Howard" <me@here.comwrote in message
news:zb******************************@comcast.com. ..
>
"Jim Langston" <ta*******@rocketmail.comwrote in message
news:WS*************@newsfe02.lga...

>>char* fnamelink::getFirst()
{
flink* current = first;
if(current)
{
= while(current->previous != NULL)
{
current = current->previous;
}
}
first = current;
if(first)
return first->data;
else
return NULL;
}
>You are attempting to dereference a NULL pointer. Lets look at one of
your methods:

You're correct, of course, but interestingly, the location of the reported
error is in the one function which does NOT allow a NULL pointer to be
dereferenced! :-) (But once you've got undefined behavior, nothing else
can be trusted, so the location of the crash *could* be just about
anywhere.)
Yeah, the interesting this is when I compiled and tested (with a few
modifications such as changing free to delete, returning "NULL" instead of
NULL for testing, etc...) getFirst() was actually working. I think it
depends on how the class is used however, and without code showing how he
was using it, could only go on what I observed.

The intesting thing about his linked list, however, is that you can get the
first, second and last items in the list, but no other using fnamelink's
wrapper. Also, the first in the fnamelink is actually the last item in the
list, since new items are iserted before first. So to get the first item
added, he has to iterate through from the last item added XD
Sep 17 '07 #5
On 2007-09-17 05:28, Jim Langston wrote:
"Howard" <me@here.comwrote in message
news:zb******************************@comcast.com. ..
>>
"Jim Langston" <ta*******@rocketmail.comwrote in message
news:WS*************@newsfe02.lga...

>>>char* fnamelink::getFirst()
{
flink* current = first;
if(current)
{
= while(current->previous != NULL)
{
current = current->previous;
}
}
first = current;
if(first)
return first->data;
else
return NULL;
}
>>You are attempting to dereference a NULL pointer. Lets look at one of
your methods:

You're correct, of course, but interestingly, the location of the reported
error is in the one function which does NOT allow a NULL pointer to be
dereferenced! :-) (But once you've got undefined behavior, nothing else
can be trusted, so the location of the crash *could* be just about
anywhere.)

Yeah, the interesting this is when I compiled and tested (with a few
modifications such as changing free to delete, returning "NULL" instead of
NULL for testing, etc...) getFirst() was actually working. I think it
depends on how the class is used however, and without code showing how he
was using it, could only go on what I observed.

The intesting thing about his linked list, however, is that you can get the
first, second and last items in the list, but no other using fnamelink's
wrapper. Also, the first in the fnamelink is actually the last item in the
list, since new items are iserted before first. So to get the first item
added, he has to iterate through from the last item added XD
Actually, as far as I can understand the code, it is possible to get any
element in the list, since first does not necessarily point to the first
element (look at getNext()/getPrevious()). This means that insertions
are not necessarily made at the end either, it could be in the middle.

--
Erik Wikström
Sep 17 '07 #6

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

Similar topics

5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
6
by: Steve Lambert | last post by:
Hi, I've knocked up a number of small routines to create and manipulate a linked list of any structure. If anyone could take a look at this code and give me their opinion and details of any...
10
by: Ben | last post by:
Hi, I am a newbie with C and am trying to get a simple linked list working for my program. The structure of each linked list stores the char *data and *next referencing to the next link. The...
57
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
11
by: bofh1234 | last post by:
Hello, I am having a problem with linked lists. My program is based on a client server model. The client sends some packets of data to the server. The server reads those packets and is...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
12
by: kalyan | last post by:
Hi, I am using Linux + SysV Shared memory (sorry, but my question is all about offset + pointers and not about linux/IPC) and hence use offset's instead on pointers to store the linked list in...
6
by: Gaijinco | last post by:
I'm trying to do a template class Node. My node.hpp is: #ifndef _NODE_HPP_ #define _NODE_HPP_ namespace com { namespace mnya { namespace carlos { template <typename T>
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.