c - 使用指针访问结构成员时产生垃圾数据

标签 c pointers struct

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

所以我创建了一个由这些结构组成的树,每个结构都包含不同的值。我使用以下函数初始化它们:

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

这个函数为我提供了传递给 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;
}

这是最终在 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;
}

这是我的主要函数,它使用另一个函数,但我认为只有上面的两个函数与这个问题相关:

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

最后我的问题是主函数中后面的 2 个 printfs 返回以下内容: First pointer Address is ok I think, but the second printf should be "Kapitel_1.txt" 为什么我在这里得到垃圾值?这是段错误吗?

最佳答案

在您的函数makeNewStoryElement中,您为root->title_of_chapter分配了空间,但您没有将给定title_of_chapter的内容放入它。这意味着 root->title_of_chapter 将包含从 malloc 返回的位置中的任何垃圾数据。
做类似的事情 strcpy(root->title_of_chapter, title_of_chapter);
printf 就可以了。
该代码还有一些其他问题和您不应该做的事情,但这应该可以解决您所询问的问题。

关于c - 使用指针访问结构成员时产生垃圾数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47610804/

相关文章:

c - 奇怪的 malloc 行为

pointers - 无法通过结构方法替换指针

c++ - 创建指向结构数组指针数组的指针,然后访问结构中的变量

更改结构内的数组大小

c - 结构中的数组和结构的 Malloc

c - 指向结构的指针声明会为其成员分配内存吗?

c - 为什么我不能在 C 中使用函数参数作为数组大小?

c - 在 C 中从类型 void * 分配给类型时出现错误不兼容类型

c++ - 该指针不同于调用该方法的指针

显示父子进程的C程序只显示子进程而不显示父进程