LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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-10-2012, 01:01 AM   #1
Anuradha12345
LQ Newbie
 
Registered: May 2012
Posts: 10

Rep: Reputation: Disabled
Unhappy code giving segmentation fault


Here is the code in c which giving me segmentation fault


node = (TrieNode *) malloc(sizeof(TrieNode));
memset(node, 0,sizeof(TrieNode));
can any body help me out. After execution it gives segmentation fault and it makes my system hang.I m using Debian linux. Thanking in advance.

here is the gdb o/p:


Trie root after creation 8055028
Reading file: /home/anuradha/ufilter/blacklists/spyware/domains
Reading file: /home/anuradha/ufilter/blacklists/humor/domains
Reading file: /home/anuradha/ufilter/blacklists/arjel/domains
Reading file: /home/anuradha/ufilter/blacklists/mixed_adult/domains

Program received signal SIGSEGV, Segmentation fault.
0xb7f00dde in memset () from /lib/libc.so.6
 
Old 08-10-2012, 01:05 AM   #2
kauuttt
Member
 
Registered: Dec 2008
Location: Atlanta, GA, USA
Distribution: Ubuntu
Posts: 135

Rep: Reputation: 26
Please post the whole code, otherwise it is difficult to help you out.
 
Old 08-10-2012, 01:20 AM   #3
Anuradha12345
LQ Newbie
 
Registered: May 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thanks for reply. Here is the code

Thanks for reply. Here the code.
void trie_insert(Trie *trie, char *key, void *value)
{
TrieNode **rover;
TrieNode *node;
char *p;
int c;
static int node_memory;
/* Cannot insert NULL values */

if (value == NULL) {
return;
}

/* Search down the trie until we reach the end of string,
* creating nodes as necessary */

rover = &trie->root_node;
p = key;

while (*p!='\0') {

node = *rover;

if (node == NULL) {


/* Node does not exist, so create it */
node = (TrieNode *) malloc(sizeof(TrieNode));
memset(node, 0,sizeof(TrieNode));
if(node){
++node_memory;
//printf("node malloc memory count :- %d",node_memory);
}
// printf("\n created new node %x", node);



/* Link in to the trie */

*rover = node;
}

/* One more use of this node */

++node->use_count;

/* Current character */

c = *p;

/* Reached the end of string? If so, we're finished. */

if (c == '\0') {

/* Set the data at the node we have reached */

node->data = value;
break;
}

/* Advance to the next node in the chain */

rover = &node->next[c];
++p;
}
node->data = value;
}



here is the gdb o/p i tried to print the node but its not allocating memory giving null

(gdb) f 1
#1 0x08048984 in trie_insert (trie=0x8055028, key=0xbfef9338 "angelnympho.com\n", value=0xbfef9338) at trietest.c:123
123 memset(node, 0,sizeof(TrieNode));
(gdb) list
118 if (node == NULL) {
119
120
121 /* Node does not exist, so create it */
122 node = (TrieNode *) malloc(sizeof(TrieNode));
123 memset(node, 0,sizeof(TrieNode));
124 if(node){
125 ++node_memory;
126 //printf("node malloc memory count :- %d",node_memory);
127 }
(gdb) print node_memory
$1 = 581262
(gdb) print node
$2 = (TrieNode *) 0x0
(gdb)

Last edited by Anuradha12345; 08-10-2012 at 01:23 AM.
 
Old 08-10-2012, 02:37 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
As your never check the return value of 'malloc', I suggest you use an 'emalloc' function instead that aborts when unsuccessful. Example:

Code:
void *emalloc (size_t size)
{
    void *p;
    char tmp [256];
    size_t tmpl;
 
    p = malloc (size);
    if (p) return p;
    fprintf (stderr, "*** Out of memory malloc(%lu)\n", (unsigned long)size);
    exit (1);
}
PS: please use tags [code] and [/code] around your code.
 
  


Reply



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
Why is my program giving me segmentation fault? Ahmad Mujtaba Programming 5 12-25-2010 07:33 AM
APT on FEDORA 1 giving SEGMENTATION FAULT prester john Linux - Software 3 03-05-2004 04:10 AM
APT on FEDORA 1 giving SEGMENTATION FAULT prester john Linux - Newbie 0 03-03-2004 10:14 PM
APT on FEDORA 1 giving SEGMENTATION FAULT prester john Fedora 0 03-03-2004 10:11 PM

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

All times are GMT -5. The time now is 12:01 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