如何分配内存为指针 [英] How to allocate memory to pointers

查看:141
本文介绍了如何分配内存为指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>

struct date {
    int year;
    int month;
    int day;
};

struct person{
    char name[64];
    struct date birthday;
};

struct aop {
   int max;
   struct person **data;
};

 struct aop *create_aop(int max) {
    struct aop *new = malloc(sizeof(struct aop));
    new->data = malloc(max*sizeof(struct person));
    for (int i=0; i<max; i++) {
    new->data[i] = NULL;
    }
    new->max = max;
    return new;}

void destroy_aop(struct aop *a) {
    free(a->data);
    free(a);
}


int add_person(struct aop *a, char *name, struct date birthday) {
    if (a->data[a->max-1] != NULL) {
        return -1;
    }
    struct person *new_person = malloc(sizeof(struct person));
    strcpy(new_person->name, name);
    new_person->birthday = birthday;
    for (int i=0; i<a->max; i++) {
        if (a->data[i] == NULL) {
            a->data[i] = new_person;
            break;
        }
    }
    free(new_person);
    return 0;
}    

我对code我写了一些问题。首先,我需要C $ CS增加额外的$成create_aop初始化的人里面的名字和生日?而且我发现,在add_person中自由(new_person)后,我不能达到A->数据[0] - >名称。我怎样才能改变的A->数据[I],而不使用另一个指针?

I have some questions about the code I wrote. First, do I need to add extra codes into create_aop to initialize name and birthday inside of person? And I found that after the free(new_person) in add_person, I cannot reach a->data[0]->name. How can I change the of a->data[i] without using another pointer?

struct aop *birthdays(const struct aop *a, int month) {
    int m = a->max;
    struct aop *n = create_aop(m);
    int j = 0;
    for (int i=0; i<m; i++) {
        if (a->data[i] != NULL) {
            if (a->data[i]->birthday.month == month) {
                n->data[j] = a->data[i];
                j++;
            }
        } else {
            break;
        }
    }
    if (j == 0) {
        return NULL;
    }
    return n;
}

我每次运行上面的功能,也有一些错误的记忆。我一直在想这件事了几个小时,但不知道什么是错在这一个。

Every time I run the function above, there are some errors memory. I have been thinking about it for hours but have no idea what is wrong in this one.

推荐答案

有这个code两个大错误。首先,在结构AOP数据数组的分配是有缺陷的。

There are two big mistakes in this code. First, the allocation of your data array in struct aop is flawed.

new->data = malloc(max*sizeof(struct person));

您不希望它指向尺寸最大长度时代的人结构的内存,你呢?既然是指针的指针,它的大小只有是最大长度时间指针,即

You don't want it to point to memory of size max times length of a person struct, do you? Since it is a pointer to pointers, it's size only has to be max times length of a pointer, i.e.

new->data = malloc(max*sizeof(struct person*));

您也可以让数据直接指向结构体的人。然后,第一行是正确的,你不必每次创建一个新的人的时间分配内存。相反,你只需使用内存的数据点:

You could also let data directly point to struct person. Then the first line would be correct and you don't have to allocate memory each time you create a new person. Instead, you would just use the memory that data points to:

(aop->data[i]).name = ...

等。

其次,你在创建后释放你的人结构的权利。

Secondly, you are freeing your person struct right after creation.

free(new_person);

现在AOP - >数据[i]为一个悬挂指针,因为地址指向可能会被覆盖的任何时间(因为它不是由malloc锁定了)。相反,你必须释放它在你的破坏作用。它看起来是这样的:

Now aop->data[i] is a dangling pointer, because the address it points to could be overwritten any time (because it's not locked by malloc anymore). Instead, you have to free it in your destroy function. It might look something like this:

void destroy_aop(struct aop *a) {    
  int i;
  for(i = 0; i < a->max; i++)
    {
      if(a->data[i] != NULL) {
      free(a->data[i]);
      }
    }
    free(a->data);
    free(a);
}

这篇关于如何分配内存为指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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