malloc问题,为什么当我调用malloc时一个无关的指针发生变化! [英] malloc question, why one irrelevant pointer changes when I call malloc!

查看:95
本文介绍了malloc问题,为什么当我调用malloc时一个无关的指针发生变化!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常棘手的问题:

在我的程序中,有一个名为update_Index的函数:

It is a very wierd problem:

In my program there is a function called update_Index:

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;
    }
    ...
}



这是其中的一部分,我定义了一个名为DocumentNode的数据结构,如下所示:



Here is part of it, I define a data structure called DocumentNode as following:

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



hash1是字符串的一般hashKey的函数,对此没有问题〜

问题是每次我尝试将内存分配给DocumentNode * docNode时,都会出现问题,
输入参数_word将更改!!!

我使用gdb来检测代码,它确实发生了. 请帮我〜

谢谢!

ps:



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:

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;
}




更奇怪的是,当我添加以下一行代码时,_word不再更改.




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);

推荐答案

似乎您没有提供所有相关代码.
就像您的断言听起来那样荒谬,该malloc修改了不相关的内存-我迅速进行了一次测试.没有表现出这种行为.

我注意到您将hashValue设置为等于_word.不禁想知道您是否正在修改* hashValue(显然会修改* _word)

这是我编译的代码和输出:

代码:

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:

#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.


< pre lang ="c ++">
char s [MAX_WORD_LENGTH];
strcpy(s,_word);

这里是问题:
---- char * hashValue = _word;
在此行,两个指针hashValue和_word指向同一内存.如果更改一个,则另一个也更改.
以及为什么要添加此
---- char s [MAX_WORD_LENGTH];
---- strcpy(s,_word);
那会好吗?这是strcpy:
< pre lang ="c ++">
char * strcpy(char * strDestination,const char * strSource)
{
assert(strDestination&& strSource);
char * strD = strDestination;
while(((* strDestination ++ = * strSource ++)!=''\ 0'')
NULL;
返回strD;
}</pre></pre>
如您所见,它创建了一个新字符串,但不仅是将其指向该字符串.
因此,使用"char * hashValue = _word"将非常危险
<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


这篇关于malloc问题,为什么当我调用malloc时一个无关的指针发生变化!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆