Click here to Skip to main content
15,917,731 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
 dict->start=(DNODE *)malloc(sizeof(DNODE)); // error occurs here

 /* DNODE and dict defined as following:

typedef struct _DICTIONARY {
  DNODE *hash[MAX_HASH_SLOT]; // the hash table of slots, each slot points to a DNODE
  DNODE *start;               // start of double link list of DNODES terminated by NULL pointer
  DNODE *end;                 // points to the last DNODE on this list
} __DICTIONARY;

typedef struct _DICTIONARY DICTIONARY;

extern DICTIONARY *dict;


typedef struct _DNODE {
  struct _DNODE  *next;
  struct _DNODE  *prev;
  void    *data;        //  actual data points to URLNODE
  char key[KEY_LENGTH]; //  actual (URL) key 
} __DNODE;

typedef struct _DNODE DNODE;


Initialization:

  dict->start=(DNODE *)malloc(sizeof(DNODE));  //fault~? why?~
  dict->end=(DNODE *)malloc(sizeof(DNODE));
  dict->start->prev=dict->start->next=NULL;
  dict->hash[0]=dict->start;
  dict->start->data=n;
  BZERO(dict->start->key,KEY_LENGTH);


gdb:

Program received signal SIGSEGV, Segmentation fault.
0x00000000004016e8 in initLists () at crawler.c:202
202 dict->start=(DNODE *)malloc(sizeof(DNODE));




So, why there is seg fault in malloc line~?
Posted
Updated 6-Feb-12 22:42pm
v3
Comments
Sergey Alexandrovich Kryukov 7-Feb-12 4:29am    
Are you sure this is in this line?
You should also show the definition of dict.
--SA
zhaoyilong1 7-Feb-12 4:33am    
I show it, it is a instance of dictionary, OK, let me add the segfault from gdb
Jochen Arndt 7-Feb-12 4:37am    
If the seg fault happens at the indicated line, dict contains probably an invalid address.

As already noted by SA, you should show us how dict is initialized.
zhaoyilong1 7-Feb-12 4:39am    
oh, sure, wait a minute
Jochen Arndt 7-Feb-12 4:48am    
Thanks for the updates. But you should show us where 'extern DICTIONARY *dict' is initialized. That is, the code from the other module where the global variable dict is declared and how it is intialized there (it is a pointer, so there may be something like dict = (DICTIONARY *)malloc(sizeof(DICTIONARY))).

[EDIT]This has been added as the solution found during the comment section conversation.[/EDIT]

If the seg fault happens at the indicated line, dict contains probably an invalid address. This may happen when extern DICTIONARY *dict; is not initialized in the other module.
 
Share this answer
 
v2
Comments
Andreas Gieriet 7-Feb-12 5:09am    
I also suspect that the dict pointer is not initialized.
In the initialization code, there is no
dict = (DICTIONARY*)malloc(sizeof(DICTIONARY));

Cheers

Andi
Check 7.19b of the c faq:
http://c-faq.com/malloc/noscale.html[^]

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900