【问题标题】:Dynamic allocation in struct结构中的动态分配
【发布时间】:2020-11-28 13:25:44
【问题描述】:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INITIAL_CAPACITY 2

struct student {
    char student[50];
    int age;
};
struct student *arr;
void print(int i){
    for (int j=i; j<=i;j++){
        printf("%s\n%d\n",arr->student,arr->age);
    }
}
void push(int *size, int *capacity){
    printf("push\n");
     int *tmp;
     if(*size > *capacity){
          arr = realloc(arr, 2 * sizeof(arr));;
          *capacity = sizeof(arr) * 2;
     }
}
void add_record(char *name, int age){
    printf("add_record \n");
    static int size = 0;
    static int i=0;
    static int capacity = INITIAL_CAPACITY;
    arr = malloc(INITIAL_CAPACITY * sizeof(arr));
    push(&size,&capacity);
    strcpy(arr[i].student, name);
    arr[i].age = age;
    printf("%d\n", size);
    printf("%d\n", capacity);
    print (i);
    i++;
}
int main()
{   
    add_record("uvhj",34);
    add_record("sakenof",34);
    add_record("sakekbdjb",34);
    add_record("sakenohjm",34);
    return 0;
}

在这段代码中,我尝试使用 calloc 和 malloc 将内存动态分配给 struct。 但我一直失败,无法获得所需的输出。 对不起,如果这段代码伤害了你的眼睛,我是编码新手

【问题讨论】:

  • 期望什么输出,而你会得到什么?
  • arr 是指向struct student 的指针,将sizeof(arr) 更改为sizeof(struct student)
  • arr = malloc(INITIAL_CAPACITY * sizeof(arr)); 每次调用 add_record 时都会创建一个新数组。也就是说,它永远不会保留除最后添加的数据之外的任何数据。更不用说它像筛子一样泄漏内存。
  • 我希望它会打印数组的条目和大小,但它正在打印一些随机值
  • 我应该怎么做才能解决这个问题@kaylum

标签: c struct dynamic


【解决方案1】:

我创建了一个struct students 来处理你所有的静态问题。

size 没有更新,capacity 应该是大于内存大小的元素数。

sizeof 是针对不在元素上的指针完成的,因此分配的内存大小不正确。

push 函数重命名为 check_capacity 以匹配函数的目标。 如果还有剩余空间 (size &lt; capacity),则什么也不做。 否则,应通过将数组 (capacity *= 2;) 和 realloc realloc(arr-&gt;data, arr-&gt;capacity * sizeof(student));; 的大小加倍来扩展数组

添加一个元素,就是在最后添加一个元素。 首先通过调用check_capacity检查是否还有剩余空间,然后用于放置在最后一个元素之后的位置是位置size,因为有从0size - 1的元素。 在size位置添加元素然后增加sizearr-&gt;size++;

输出是:

add_record 
size: 1
capacity: 2
0: uvhj, 34
add_record 
size: 2
capacity: 2
0: uvhj, 34
1: sakenof, 34
add_record 
size: 3
capacity: 4
0: uvhj, 34
1: sakenof, 34
2: sakekbdjb, 34
add_record 
size: 4
capacity: 4
0: uvhj, 34
1: sakenof, 34
2: sakekbdjb, 34
3: sakenohjm, 34
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INITIAL_CAPACITY 2

typedef struct student {
    char student[50];
    int age;
} student;

typedef struct students {
    int size;
    int capacity;
    student *data;
} students;

void print(students *arr){
    for (int i = 0; i < arr->size; i++){
        student s = arr->data[i];
        printf("%d: %s, %d\n", i, s.student, s.age);
    }
}
void check_capacity(students *arr) {
     if(arr->size < arr->capacity) {
         return;
     }
     arr->capacity *= 2;
     arr->data = realloc(arr->data, arr->capacity * sizeof(student));;
}
void add_record(students *arr, char *name, int age){
    printf("add_record \n");
    check_capacity(arr);
    strcpy(arr->data[arr->size].student, name);
    arr->data[arr->size].age = age;
    arr->size++;
    printf("size: %d\n", arr->size);
    printf("capacity: %d\n", arr->capacity);
    print(arr);
}
int main()
{
    students arr;
    arr.size = 0;
    arr.capacity = INITIAL_CAPACITY;
    arr.data = malloc(INITIAL_CAPACITY * sizeof(student));
    add_record(&arr, "uvhj",34);
    add_record(&arr, "sakenof",34);
    add_record(&arr, "sakekbdjb",34);
    add_record(&arr, "sakenohjm",34);
    free(arr.data);
    return 0;
}
~                                   

【讨论】:

  • 你能解释一下你做了什么吗?或分享一些参考,以便我可以从中学习。
  • @DoLandTrump 我已经添加了一些细节,如果您需要更多细节,请询问您不明白的部分。
猜你喜欢
  • 1970-01-01
  • 2015-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多