且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

malloc问题,为什么当我调用malloc时一个无关的指针发生变化!

更新时间:2022-05-07 00:16:01

似乎您没有提供所有相关代码.
就像您的断言听起来那样荒谬,该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