【问题标题】:can't print values when call structure values from main function从主函数调用结构值时无法打印值
【发布时间】:2015-05-17 13:37:27
【问题描述】:

我也清除了一个结构并分配了一些内存。使用我更新数据的功能。我在访问数据时遇到了错误分段错误。

这是我的代码

在头文件中:

typedef struct
{
int member;
char *name;
}place;

void update(place **,int);
void display(place **,int);

在函数中

static memallocate(place **ptr,int viname,int index)
{

 ptr[index]=(place *)malloc(sizeof(place));
 ptr[index]->name=(char *)malloc(viname*sizeof(char *));

}

void update(place **ptr,int index)
{
 ---read string value "na" find the strlen as "pp"---
 memallocate(ptr,pp,index);
 ptr[index]->name=na;

}

void display(place **ptr,int index)
{
 int i;
 for(i=0;i<index;i++)
  {
    printf("%s\n",ptr[i]->name);
    printf("%s\n",ptr[i]->country);
  }
}

在主文件中:

void main()
{
 int index=0;
 place *pla[5]={NULL};
 while(index<2)
 {
    update(&pla[index],index);
    index++;
 }
 display(pla,index);
}

我的问题是我在访问函数display 时出现分段错误,并且无法打印数据 ptr[0]->name,ptr[0]->country,ptr[1]->name,ptr[1] ->国家..为什么会这样?任何内存故障。每次更新后我都使用 printf 进行打印。

【问题讨论】:

  • 如果您发布了可以编译的内容,将会有很大帮助。最好是您从中获得段错误的实际代码。

标签: c pointers structure function-pointers dynamic-memory-allocation


【解决方案1】:

我在这里看到两个市长问题。

第一

这里

static void memallocate(place **ptr,int viname,int index)
{
  ptr[index]=(place *)malloc(sizeof(place));
  ptr[index]->name=(char *)malloc(viname*sizeof(char *));  
}

你分配了太多的内存。应该是

static void memallocate(place ** ptr, int viname, int index)
{
  ptr[index] = malloc(sizeof(place));
  ptr[index]->name = malloc(viname * sizeof(char));  
}

甚至更好:

static int memallocate(place ** ptr, size_t viname, size_t index)
{
  int result = 0;

  if (NULL == ptr)
  {
    result = -1;
    errno = EINVAL;
  }
  else
  {
    ptr[index] = malloc(sizeof *ptr[index]);
    if (NULL == ptr[index])
    {
      result = -1;
    }
    else
    {
      ptr[index]->name = malloc(viname * sizeof *(ptr[index]->name));  
      if (NULL == ptr[index]->name)
      {
        result = -1;
        free(ptr[index]);
      }
    }
  }

  return result;
}

第二次

然后在这里(假设 nachar* 正确初始化以引用 C-“字符串”)

void update(place **ptr,int index)
{
  ---read string value "na" find the strlen as "pp"---
  memallocate(ptr,pp,index);
  ptr[index]->name=na;
}

您覆盖了您刚刚分配给name 的内容。要复制 C-“字符串”,请使用 strcpy()

int update(place ** ptr, size_t index)
{
  ---read string value "na" find the strlen as "pp"---
  int result = memallocate(ptr, pp, index)
  if (-1 == result)
  {
    perror("memallocate() failed");
  }
  else
  { 
    strcpy(ptr[index]->name, na);
  }

  return result;
}

然后这样称呼它:

int main(void)
{
  size_t index = 0;
  place * pla[5] = {NULL};

  /* Loop over all array's elements. */
  while (index < sizeof pla/sizeof *pla)
  {
    update(pla, index);
    ++index;
  }

  ...
}

注意事项:

【讨论】:

    【解决方案2】:

    当您调用update() 时,您将传递一个place ** 当前索引 作为参数。

    然而,你还是传递了index 并且稍后在你的memallocate() 分配内存,就好像它是一个指向place *[] 的指针。

    所以应该有助于从update()memallocate() 中删除参数index 并将内存分配更改为:

    *ptr = (place *)malloc(sizeof(place));
    *ptr->name = (char *)malloc(viname*sizeof(char *));
    

    【讨论】:

    • 至少,我看不出在main() 中调用update(&amp;pla[index],index); 并稍后将内存分配给它的某个偏移量是正确的。如果要通过传递索引来完成,那么第一个参数应该指向数组:update(pla, index);
    • 你是对的,要么直接传递元素,要么传递数组的根和元素的索引。我完全错过了 OP 的 main() 的代码(后来添加了吗?)。我假设代码是根据我更新的答案调用的。请原谅,我收回我的评论。
    猜你喜欢
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    • 2020-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    相关资源
    最近更新 更多