简体   繁体   English

使用指针访问结构成员时的垃圾数据

[英]Garbage data on accessing struct member using pointer

struct _StoryElement_ {
char *title_of_chapter_;
struct _StoryElement_ *path_a_;
struct _StoryElement_ *path_b_;
char *content_of_chapter_;
};
typedef struct _StoryElement_ StoryElement;

So I have a Tree of these Structs created each containing different values. 因此,我创建了一个包含这些结构的树,每个结构包含不同的值。 I initialised them all using the following functions: 我使用以下功能初始化了它们:

StoryElement *insertIntoStoryElement(StoryElement* root, char* title_of_chapter, char* content_of_chapter)
{
  if(root == NULL)
  {
    root = makeNewStoryElement(root, title_of_chapter, content_of_chapter);
  }
  else if (root->path_a_ == NULL)
  {
    root->path_a_ = makeNewStoryElement(root, title_of_chapter, content_of_chapter);
  }
  else if (root->path_b_ == NULL)
  {
    root->path_b_ = makeNewStoryElement(root, title_of_chapter, content_of_chapter);
  }

  return root;
}

StoryElement *makeNewStoryElement(StoryElement* root, char* title_of_chapter,
                                  char* content_of_chapter)
{
  root = (StoryElement*) malloc(sizeof(StoryElement));

  root->title_of_chapter_ =
  (char*)malloc(sizeof(char*)*(strlen(title_of_chapter) + 1));
  root->content_of_chapter_ =
  (char*)malloc(sizeof(char*)*(strlen(title_of_chapter) + 1));

  //strcpy(NewStoryElement->title_of_chapter_, title_of_chapter);
  //strcpy(NewStoryElement->content_of_chapter_, content_of_chapter);
  title_of_chapter = root->title_of_chapter_;
  content_of_chapter = root->content_of_chapter_;

  root->path_a_ = NULL;
  root->path_b_ = NULL;

  return root;
}

This function is what provides me with the string values that I pass onto insertIntoStoryElement(): 这个函数为我提供了传递给insertIntoStoryElement()的字符串值:

StoryElement *createStoryTree (StoryElement *root, char *storage)
{

  char* pos = storage;
  pos = strchr(pos, '\n');
  *pos = '\0';
  int size = strlen(storage);
  char* title = malloc(size + 1);
  strcpy(title, storage);

  char* ptr_path_a = pos + 1;
  pos = strchr(ptr_path_a, '\n');
  *pos = '\0';
  size = strlen(ptr_path_a);
  char* path_a = malloc(size + 1);
  strcpy(path_a, ptr_path_a);

  char* ptr_path_b = pos + 1;
  pos = strchr(ptr_path_b, '\n');
  *pos = '\0';
  size = strlen(ptr_path_b);
  char* path_b = malloc(size + 1);
  strcpy(path_b, ptr_path_b);

  char* ptr_text = pos + 1;
  pos = strchr(pos + 1, '\0');
  *pos = '\0';
  size = strlen(ptr_text);
  char* text = malloc(size + 1);
  strcpy(text, ptr_text);

  root = insertIntoStoryElement(root, title, text);

/*  if(strcmp(path_a, "-")!=0 && strcmp(path_b, "-")!=0)
  {*/
    root->path_a_ = readStoryFromFile(root->path_a_, path_a);
    root->path_a_ = readStoryFromFile(root->path_b_, path_b);
  //}

  return root;
}

And this is the function that is ultimately called in main: 这是最终在main中调用的函数:

StoryElement *readStoryFromFile (StoryElement *root, char *filename)
{
  if(strcmp(filename, "-") == 0)
  {
    //printf("End  reached\n");
    return 0;
  }
  FILE *file = fopen(filename, "r");

    if(!file)
  {
    printf("[ERR] Could not read file %s.\n", filename);
    return 0;
  }

  long fsize = getFileSize(file);

  char* storage = malloc(fsize + 1);
  if(!storage)
  {
    printf("[ERR] Out of memory.\n");
    return 0;
  }

  fread(storage, fsize, 1, file);
  storage[fsize] = '\0';
  fclose(file);

  root = createStoryTree(root, storage);

  free(storage);

  return root;
}

And this is my main which uses another function but only the 2 above are relevant for this question I think: 这是我的主要工具,它使用了另一个功能,但是我认为只有上面的2个与此问题相关:

int main ( int argc, char *argv[] )
{
  if ( argc != 2 ) /* argc should be 2 for correct execution */
  {
    printf( "Usage: ./ass2 [file-name]\n");
    return (1);
  }

  StoryElement *root = NULL;

  root = readStoryFromFile(root, argv[1]);
  if(!root)
    return 3;

  printf("%p\n", root);
  printf("%s\n", root->title_of_chapter_);

  //printStoryTree(root);
  freeStoryTree(root);

  root = NULL;

  return 0;
}

And finally my problem is that the 2 printfs after in the main function return the following: First pointer Address is ok I think, but the second printf should be "Kapitel_1.txt" Why am I getting a garbage value here? 最后,我的问题是主函数中的2个printfs返回以下内容: 我认为第一个指针地址可以,但是第二个printf应该是“ Kapitel_1.txt”,为什么我在这里得到垃圾值? Is this a segmentation fault? 这是细分错误吗?

In your function makeNewStoryElement you malloc the space for root->title_of_chapter but you don't put the content of the given title_of_chapter in it. 在函数makeNewStoryElement您为root->title_of_chapter了空间,但没有在其中放置给定title_of_chapter的内容。 This means that root->title_of_chapter will contain whatever garbage data was in the location you got back from the malloc. 这意味着root->title_of_chapter将包含您从malloc取回的位置中的所有垃圾数据。
Do something like strcpy(root->title_of_chapter, title_of_chapter); 做类似strcpy(root->title_of_chapter, title_of_chapter);
the printf will then be fine. 然后,printf就可以了。
The code has some other problems and things you shouldn't do but this should fix the problem that you are asking about. 该代码还有其他一些问题和您不应该做的事情,但这应该可以解决您所询问的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM