LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-04-2004, 11:10 PM   #1
appas
Member
 
Registered: Jul 2004
Posts: 72

Rep: Reputation: 15
segmentation fault in list_for_each


I am writing a test module which uses linkedlist using list.h.
I use list_add to add entries in linkedlist, but when i try to iterate the list i get segmentation fault.
I am providing the code below. Could someone help me to fix the problem.

Code
-------

int init_module()
{
int returnValue ;
returnValue = register_chrdev(22, "xxx", &xxx_fops)))
TestList("hello_world00");
TestList("helloworld11");
IterateList();
return 0;
}

int cleanup_module()
{
unregister_chrdev(22,"xxx");
}

void TestList(char *data_in)
{
struct directoryList dirStruct;
my_memcpy(dirStruct.dirName,data_in,strlen(data_in) +1);
list_add(&dirStruct.list,&initList);
printk(" DATA ADDED %s \n", dirStruct.dirName); // i am receiving this print in the log
}

void IterateList()
{
struct list_head *tmp;
list_for_each(tmp, &initList)
{
struct directoryList *iterateList = list_entry(tmp, struct directoryList, list);
printk( " ITERATELIST DIR VALUE %s \n" , iterateList->dirName);
}

}

/*************** LOG OUTPUT IS PROVIDED BELOW *********************/
DATA ADDED hello_world00
DATA ADDED helloworld11
ITERATELIST DIR VALUE helloworld11
last message repeated 661 times
Unable to handle kernel NULL pointer dereference at virtual address 00000001
printing eip:
e0ade288
.... it goes on ....
 
Old 08-05-2004, 01:55 AM   #2
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
Segmentation fault occurs when we try to access the memory area that is not allocated, and this error is very common when playing with pointers and strings, so you better check for the proper memory alocation and check that none of your pointer will point to an invalid address.
If you can't figure out this way you better run it in a debugger and check the status of you pointers and buffers on each itration, Are they propely allocated???
 
Old 08-05-2004, 03:50 AM   #3
appas
Member
 
Registered: Jul 2004
Posts: 72

Original Poster
Rep: Reputation: 15
Thank you for your reply cppkid,
I got that working after allocating memory for the local variable used in function 'TestList' using kmalloc.
previos code has been changed to the following (only the changed line is included )

void TestList(char *data_in)
{
struct directoryList * dirStruct = (struct directoryList *) kmalloc(sizeof(struct directoryList),GFP_KERNEL);
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
what does Segmentation Fault mean ? baronlynx Linux - Newbie 10 10-25-2009 04:32 PM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
Segmentation fault tejas15_10 Programming 9 06-20-2005 09:12 AM
Segmentation fault tejas15_10 Linux - Newbie 1 06-14-2005 04:44 AM
Segmentation fault santhosh_o Programming 3 10-26-2004 05:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration