小编Yia*_*nis的帖子

如何在类图上将属性的数据类型表示为对象数组?

哪种方式能够显示属性的数据类型是对象数组?我在网上找到了两个不同的版本.

在此输入图像描述

我有一个SportsCentre课程,我也有一个Employee课程

arrays uml class-diagram

39
推荐指数
1
解决办法
7万
查看次数

如何正确地为 C 中的结构数组分配内存?

这是我第一次使用结构体,因为我对 C 还是比较陌生。当我尝试为 3 个结构体输入值时,程序崩溃了,但它对 1 和 2 工作正常。我想我实际上可能没有分配足够的记忆。这是为结构数组分配内存的正确方法吗?

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

#define MIN_SIZE 0
#define MAX_SIZE 100
#define MAX_MONTH_STR 9

//Define a struct data type
typedef struct
{
    char* month;
    int day;
    int year;
}date;

//Method to allocate memory for a list of date structures
date* allocateStruct(int size)
{
    //Declaration of variables
    date *array;
    int i;

    //Allocate memory for rows (to store 'size' many pointers to 'date' struct data type)
    array = malloc(size*sizeof(date*));

    //For-loop to allocate memory for …
Run Code Online (Sandbox Code Playgroud)

c arrays malloc struct

3
推荐指数
1
解决办法
1万
查看次数

C - 我的记忆释放功能有什么问题?

我有一个结构,其中包含2个整数和一个指向另一个结构的指针.我首先为struct分配内存然后为指针分配内存.当我释放内存时,我先释放指针,然后释放结构.

当我运行我的程序并调用释放内存的函数时,它会在调用时崩溃.当我不调用释放内存的函数时它工作正常,但后来我没有释放内存.

我尝试删除释放分配给指针的内存的行,程序不会崩溃,但我不认为这是正确的,因为每个"malloc/calloc"都需要"免费"吗?任何人都认为释放功能有问题吗?

//Define a struct data type
struct q_element
{
    //Declaration of struct members
    int element;
    int priority;
    struct q_element *next_element;
};

//Method to allocate memory
struct q_element* allocateStruct()
{
    //Declaration of a variable
    struct q_element *e;

    //Allocate memory for one queue element
    e = malloc(sizeof(struct q_element));

    //Allocate memory for one pointer to a queue element
    e->next_element = calloc(1,sizeof(struct q_element*));

    //Initialize integer members of queue element
    e->element = 0;
    e->priority = 0;

    return e;
}

//Method to free memory …
Run Code Online (Sandbox Code Playgroud)

c free struct pointers memory-management

1
推荐指数
1
解决办法
523
查看次数

在声明它时,是否可以在数组名称中包含循环计数器?(在C中)

我有这个循环,它根据用户输入的值创建一定数量的数组.我想在数组名称的末尾包含数组的计数器,使其为:array1 [],array2 [],array3 []等等,每次迭代一次.这可能吗?我们刚开始在大学学习C,所以我对它还不太了解.我尝试了以下方法:

#include <stdio.h>

int main(void)
{
    //Variables
    int i, columns, column_size;

    //Read input
    printf("Input number of columns:\n");
    scanf("%d", &columns)

    //Loop to create arrays
    for (i=1; i<=columns; i=i+step)
    {
        //Read column size
        scanf("%d", &column_size);

        //Create an array of given size for this column
        int column+"i"+[column_size];
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

c arrays counter naming for-loop

0
推荐指数
1
解决办法
113
查看次数