Linked List in C problem in Vista

September 7, 2009 at 08:03:46
Specs: Windows Vista, 2 GB DDR2
I am having problem running this code of a vry simple linked list in Windows Vista Ultimate(32 bit).

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

struct node0{
char *n;
struct node0 *next;

typedef struct node0 node;
void printList(node *);

int main(int argc, char** argv) {
node *current,*head,*temp;
int i,j;
char *c;
printf("Enter a word\n");

current=(node *)malloc(sizeof(node));
temp=(node *)malloc(sizeof(node));
printf("Enter a word\n");
return (EXIT_SUCCESS);
void printList(node *ptr){
node *temp;

while(temp->next != NULL)
----------------code ends-------------------------
It is compiling perfectly. When executing it windows says the application has stopped working.
I am using NetBeans ide with MinGW gcc compilers and make. I have tried to compile it with Borland free command lines tools compiler(bcc32)(v5.5) but still same problem- application not executing.I guess its some compatibility issue with memory access but since I am new to C I don't know what to do.
Any help will be highly appreciated.

See More: Linked List in C problem in Vista

Report •

September 7, 2009 at 08:24:12
char *c;

Make c an array of 20 char (either on the stack or the heap), and try again.

EDIT: Also, you might want to use the C++ features of your C++ complier; C++ does have slightly better memory management than vanilla C.

Report •

September 7, 2009 at 12:21:17
Hi Razor2.3,
Thanks for your reply. I tried your approach. However the problem remains there only.I am pasting the output.
Enter a word
Enter a word
After the second word it just hangs and I have to press
Ctrl+Break to end it.
The problem,I guess, is with the fgets and scanf library functions-- they aren't working correctly on my computer !!
The same program is working on my friends computer - He uses XP but it still doesn't run on my computer when I try it on XP(I have dual operating systems).

However I have solved my problem by writing my own readLine() routine which makes the program work correctly.
I feel the problem is with memory management in my system and it's compatibility with the fgets and scanf library functons. I have used explicit memory management in my readLine() function by the way of malloc and realloc.
If you can think of any probable problem with these library input functions ( fgets and scanf )on my system do enlighten me please. I want to make my concepts strong. For the time being my problem is solved.

Thanks once again for your help. I really appreciate your gesture.

Report •

September 7, 2009 at 12:52:42
If you can think of any probable problem with these library input functions ( fgets and scanf )on my system do enlighten me please
You're probably still passing an invalid pointer.

I assume you're coming from some higher level language, where you don't have to worry about memory management. C assumes you'll handle the memory yourself. This is why the program will compile, but the resulting executable will crash.

Report •

Related Solutions

September 7, 2009 at 14:46:56
What Razor says.

1. Why are you using fgets and a local variable for the first
line, but scanf straight into the node for all subsequent lines?
Decide which one is better and be consistent.

2. Remember that fgets and scanf don't do any memory
management. As Razor says, you are wholly responsible for

3. Just because the program appears to work doesn't mean
it's correct. When you read data into memory that you have
not allocated, anything can happen, including apparently
correct behaviour. This is in fact the worst thing that can
happen, because it hides your errors.

Report •

September 8, 2009 at 05:01:49
@Razor:-- Yes you guessed it right,I am coming from Java and was doing significant level programming there-and that didn't require memory management.I guess you are right about invalid pointer because when I wrote my own function which did manage memory the program worked perfectly.

@klint:-- (point no 2): I guessed that fgets and scanf do not manage memory by reading their code as defined in the standard library but I wasn't very sure as both these functions were working properly in simple programs (and even in this program on my friend's computer).
(point no 3):You are very correct and I guess this must be by chance that they were working sometimes and failing at others.

Thanks a lot to both of you guys !!!

Report •

September 8, 2009 at 06:53:51
And one last thing guys - you say that fgets and scanf do not manage memory. They assume tht the pointer I am passing to them has been assigned a valid memory address.They are input functions - how do I know the length of ,say, an input string before the user has entered it ? In this case it becomes impossible for me to allocate memory for the unknown input and hence pass a valid pointer to these functions such that it has been allocated sufficient memory. If it is so then then this clause renders these functions useless for string inputs. And using an array of wastefully large capacity to accomodate the largest probable input is the worst programming practice.
So if there is any efficient other way round do tell me please.

Report •

September 8, 2009 at 14:17:18
scanf("%s\n", ptr) will assume that ptr points to a sufficiently
large buffer, and there is no checking. It's therefore
dangerous. fgets(ptr, 20, stdin) assumes that ptr points to a
buffer at least 20 bytes in size. If your input string exceeds
this, the remaining data will remain in stdin to be read the
next time you call fgets, even though it is on the same line.

fgets will copy the newline ('\n') into the buffer. If it doesn't,
you know that the input line exceeded the buffer size. What
you probably want to do, then, is read stdin one character at
a time until you get to the end of the line (until you get '\n' or

Report •

September 8, 2009 at 22:55:16
@klint: Thanks for that technically precise definition of those 2 functions. I had downloaded a book on C but it listed all important library routines in brief without explaining their intricacies. Can you tell me where to find these precise definitions and specifications of C language and also some advanced texts on C since I have read the core part. I am seeking the memory management and complex pointer manipulations text as well as windows interfacing in C.

Report •

September 9, 2009 at 01:34:26
Your gcc compiler should have come with all the documentation.
You need to read the Library Reference manual. If you haven't
got it, you can get it at

If you want to write portable code that works on any compiler,
you should buy a copy of the ISO C language standard from (but it is a bit difficult to read.)

Report •

September 9, 2009 at 01:45:56
Also you asked about Windows interfacing in C. This is a bit
outdated nowadays, as most Windows programming is done in
C++ or, more recently, using the .NET framework and C#. You
may find a second-hand copy of Charles Petzold's book on the
Win32 API which is one of the most respected books on the

Report •

Ask Question