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

Why I got such weird results for a simple linked list test?

Hi All, I've been testing some extremely simple code about a linked list.

What I keep doing is actually writing some parts of a linked list and testing them.

Below are my codes:
---------------------------------------------------------------------------

/* new.h */
#ifndef NEW_H
#define NEW_H

typedef int ListEntry;

typedef struct listnode{
ListEntry info;
struct listnode *next;
}ListNode;

typedef struct list{
ListNode *head;
ListNode *tail;
int count;
}List;

void CreatList(List *list);
int ListSize(List *list);

#endif

-------------------------------------------------------
/* new.c */

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

void CreatList(List *list){
list = (List*)malloc(sizeof(List));
if(list==NULL) Error("No memory available.");
list->head = list->tail = NULL;
list->count = 0;
}

int ListSize(List *list){
return (list->count);
}



--------------------------------------------
/* newtest.c */

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

main(){
List *list;
CreatList(list);
printf("%d\n", ListSize(list));
}

--------------------------------------------------------



I have 3 files: .h, .c and test.c
I try to test if I successfully created a new linked list
If I did, I should have got a result ListSize(list) == 0;
But I didn't...below are the compiling and running results:

With one system, I got:

[phe@f438-07 hw4]$ #include "new.h"
[phe@f438-07 hw4]$ gccx newtest.c new.o
[phe@f438-07 hw4]$ gccx newtest.c new.c
[phe@f438-07 hw4]$ ./a.out
-2130706312


With another system, I got:

[phe@inceptor mergesort]$ #include "new.h"
[phe@inceptor mergesort]$ gccx newtest.c new.o
[phe@inceptor mergesort]$ gccx newtest.c new.c
[phe@inceptor mergesort]$ ./a.out
33949697

Then, what's wrong...??? I am really frustrated, this is rather simple code and test, but I got so wired errors...Shouldn't the ListSize(list) be zero when I just created a list?

Or, anything wrong with memory?

Really appreciate your help!!!
Feb 24 '08 #1
4 1886
sicarie
4,677 Expert Mod 4TB
Expand|Select|Wrap|Line Numbers
  1. typedef struct list{
  2.     ListNode *head;
  3.     ListNode *tail;
  4.     int count;
  5. }List;
  6.  
You never initialize count. So when the compiling process starts, the memory location for count is created, but the value inside is left alone. This means it contains whatever values were in there before you ran your program, or is a "junk" variable. This is why you need to set variables on instantiation.
Feb 24 '08 #2
You never initialize count. So when the compiling process starts, the memory location for count is created, but the value inside is left alone. This means it contains whatever values were in there before you ran your program, or is a "junk" variable. This is why you need to set variables on instantiation.
Thanks for your reply. But I did initialize list->count in new.c...


void CreatList(List *list){
list = (List*)malloc(sizeof(List));
if(list==NULL) Error("No memory available.");
list->head = list->tail = NULL;
list->count = 0;
}
Feb 24 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
main(){
List *list;
CreatList(list);
printf("%d\n", ListSize(list));
}
Your problem is here: CreateList() takes a List* argument:
void CreatList(List *list){
list = (List*)malloc(sizeof(List));
if(list==NULL) Error("No memory available.");
list->head = list->tail = NULL;
list->count = 0;
}
So the call in main() takes the pointer list and makes a copy of it for the CreateList() arguiment. The function dutifully creates the list and updates the copy. The original pointer in main() never gets changed.

You need to change CreateList() to takes the address of a List*. That way CreateList() can change the address in the LIst* in main():

Expand|Select|Wrap|Line Numbers
  1. void CreatList(List **list){
  2. *list = (List*)malloc(sizeof(List));
  3. etc...
  4. }
  5.  
  6. int main()
  7. {
  8. List *list;
  9. CreatList(&list);
  10.  
  11. }
  12.  
Feb 24 '08 #4
sicarie
4,677 Expert Mod 4TB
Dang, thanks WFC, good catch. That's what I get for spot reading it without code tags.
Feb 24 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: na1paj | last post by:
here's a simple linked list program. the DeleteNode function is producing an infinit loop i think, but i can't figure out where.. #include <stdio.h> typedef struct { char *str; //str is a...
12
by: Eugen J. Sobchenko | last post by:
Hi! I'm writing function which swaps two arbitrary elements of double-linked list. References to the next element of list must be unique or NULL (even during swap procedure), the same condition...
3
by: Rick | last post by:
Hello, I ran Microsoft's free "Web Application Stress" tool to see how asp.net/c# performed against html. Are these results typical? Network: WAS ran on a server with a t3 Internet...
6
by: Julia | last post by:
I am trying to sort a linked list using insertion sort. I have seen a lot of ways to get around this problem but no time-efficient and space-efficient solution. This is what I have so far: ...
22
by: Daniel Rucareanu | last post by:
I have the following script: function Test(){} Test.F = function(){} Test.F.FF = function(){} Test.F.FF.FFF = function(){} Test.F.FF.FFF.FFFF = function(){} //var alias = function(){}; var...
18
by: atv | last post by:
at least to me it is. I can't figure out for the life what it is i'm doing wrong here. i have a function called assign_coordinate. usually, i pass a malloced pointer to it, then individual...
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...
6
by: Emiurgo | last post by:
Hi there to everyone! I've got a problem which may be interesting to some of you, and I'd be very grateful if there is someone who can give me some advice (or maybe redirect me to some other place)....
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
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...
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...
0
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...
0
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,...
0
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...

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.