Click here to Skip to main content
15,913,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
It is a very wierd problem:

In my program there is a function called update_Index:

C++
int update_Index(int documentID,char *_word,INVERTED_INDEX *index)
{
  char *hashValue=_word;
  unsigned long hashKey=hash1(hashValue)%MAX_NUMBER_OF_SLOTS;//get hash key
  if (index->hash[hashKey]->word==NULL)//case that the hash for this word not exists
    {
      /*
        add documentNode
       */
      DocumentNode *docNode=NULL;
      docNode=(DocumentNode *)malloc(sizeof(DocumentNode));
      docNode->next=NULL;
      docNode->document_id=documentID;
      docNode->page_word_frequency=1;
    }
    ...
}


Here is part of it, I define a data structure called DocumentNode as following:
C++
typedef struct _DocumentNode{
  struct _DocumentNode *next;
  int document_id;
  int page_word_frequency;
} __DocumentNode;
typedef struct _DocumentNode DocumentNode;


hash1 is a function to general hashKey of a string, there is no problems about that~

problem is everytime when I try to malloc memory to DocumentNode *docNode,
the input argument _word will change!!!

I use gdb to detect the code, it really happens..
Plz help me~

Thanks!

ps:

C#
unsigned long hash1(char* str) {
  unsigned long hash = 5381;
  int c;
  while ((c = *str++) != 0)
    hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
  return hash;
}



more strangely, when I add the following one line code, the _word is not changing anymore.
<pre lang="c++">
char s[MAX_WORD_LENGTH];
strcpy(s,_word);
Posted
Updated 24-Feb-12 5:46am
v3
Comments
Chuck O'Toole 24-Feb-12 0:11am    
You really need to show the hash1() function.

When you say "input argument _word will change", you need to be more specific. Does the binary value of the pointer "_word" change so that is has a different hexidecimal value or does the string of characters pointed to by "_word" change to a different or clobbered string? Makes a difference.
zhaoyilong1 24-Feb-12 10:01am    
only the string change, the memory is the same ~
I post my hash1 function

It seems you're not supplying all of the relevant code.
As nonsensical as your assertion sounds, that malloc modifies unrelated memory - I whipped up a quick test. There is no such behaviour exhibited.

I notice that you set hashValue to be equal to _word. Can't help but wonder if you're modifying *hashValue (which obviously or not, will modify *_word)

Here's the code I compiled and the output:

Code:

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

typedef struct _DocumentNode
{
    struct _DocumentNode *next;
    int document_id;
    int page_word_frequency;
} __DocumentNode;

typedef struct _DocumentNode DocumentNode;

int update_Index(int documentID,char *_word) //,INVERTED_INDEX *index)
{
//  char *hashValue=_word;
//  unsigned long hashKey=hash1(hashValue)%MAX_NUMBER_OF_SLOTS;//get hash key
//  if (index->hash[hashKey]->word==NULL)//case that the hash for this word not exists
    {
      /*
        add documentNode
       */
      DocumentNode *docNode=NULL;
      printf("_word = 0x%X\n", (int)_word);
      printf("*_word = %s\n\n", _word);

      docNode=(DocumentNode *)malloc(sizeof(DocumentNode));

      printf("_word = 0x%X\n", _word);
      printf("_word[0] = %c\n\n", _word[0]);


      docNode->next=NULL;
      docNode->document_id=documentID;
      docNode->page_word_frequency=1;
    }
}


int main()
{
    update_Index(0 , "inputWord");
    char *tmp1, *tmp2;
    tmp1 = (char*)malloc(10);
      printf("tmp1 = 0x%X\n", tmp1);
    tmp2 = (char*)malloc(10);
      printf("tmp2 = 0x%X\n", tmp2);
}


Result:
_word = 0x40305F
*_word = inputWord

_word = 0x40305F
_word[0] = i

tmp1 = 0x9F3D48
tmp2 = 0x9F3D60

Process returned 0 (0x0)   execution time : 0.054 s
Press any key to continue.
 
Share this answer
 
Comments
zhaoyilong1 24-Feb-12 9:59am    
Thanks a lot, but I still have this problem, here is the test of the code:


235 docNode=(DocumentNode *)malloc(sizeof(DocumentNode));
(gdb) p _word
$3 = 0x7fffffffdf20 "www"
(gdb) n
236 docNode->next=NULL;
(gdb) p _word
$4 = 0x7fffffffdf20 "["
zhaoyilong1 24-Feb-12 10:01am    
the input argument for _word is a char **, is that will be a problem ~?
kamendula 22-Nov-12 3:08am    
i come up with the same problem and that really confused me...could u tell me how u fix the proble?? plz...i really dont konw how to fix it...thank u very very much.....
<pre lang="c++">
char s[MAX_WORD_LENGTH];
strcpy(s,_word);

Here are the problem:
----char *hashValue=_word;
At this line,the two pointer hashValue and _word point to the same memory.If you change one ,the other changes too.
As with why if you add this
----char s[MAX_WORD_LENGTH];
----strcpy(s,_word);
that would be fine? Here's the strcpy:
<pre lang="c++">
char *strcpy(char *strDestination, const char *strSource)
  {
  assert(strDestination && strSource);
  char *strD=strDestination;
  while ((*strDestination++=*strSource++)!='\0')
  NULL;
  return strD;
  }</pre></pre>
As you can see,it creates a new string ,but not just give the point to it.
So the use "char *hashValue=_word" would be very dangerous
 
Share this answer
 
v2

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