C 节点 - 如何不覆盖分配的内存空间而是创建新的(作业问题)

发布于 2024-10-07 11:16:10 字数 720 浏览 6 评论 0原文

Company 结构体定义如下:

typedef struct company {
 char* company_name;
 int employee_counter;
} company;

我使用此函数创建一个新的公司节点:

company *make_company_node(char* company_name, int employee_counter) {
 company *newNode = (company*) malloc(sizeof(company));
 if(!newNode) return NULL; 

 newNode->company_name = company_name;
 newNode->employee_counter = employee_counter;

 return newNode;
}

然后我从输入中获取一些公司名称,为每个公司创建公司节点:

companyUnion->company_arr[i] = make_company_node(company_name, 0);

(company_arr 最终包含指向所有公司的指针)。

问题是,我似乎总是覆盖最初分配的内存空间,因此最后我得到一个数组,其单元格全部指向最后一个公司(提交了姓氏)。

我怎样才能纠正它,以便它不会覆盖,而是总是分配新空间?

Company struct is defined as follows:

typedef struct company {
 char* company_name;
 int employee_counter;
} company;

I use this function to create a new company node:

company *make_company_node(char* company_name, int employee_counter) {
 company *newNode = (company*) malloc(sizeof(company));
 if(!newNode) return NULL; 

 newNode->company_name = company_name;
 newNode->employee_counter = employee_counter;

 return newNode;
}

Then I get some company names from the input, create company node for each one:

companyUnion->company_arr[i] = make_company_node(company_name, 0);

(the company_arr eventually contains pointers to all the companies).

The problem is that it seems I'm always overwriting the originally allocated memory space and therefore at the end I wind up with an array whose cells all point to the last company (with the last name submitted).

How can I correct it so that it doesn't overwrite but instead always allocated new space?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

§普罗旺斯的薰衣草 2024-10-14 11:16:10

如果您在调用 make_company_node 函数之前知道 company_name 的长度,我会更改该函数以接受该长度(否则使用 strlen)。

company *make_company_node(char *company_name, size_t cn_len, int employee_counter) {
    company *newNode = malloc(sizeof *newNode);
    if (newNode) {
        newNode->company_name = malloc(cn_len + 1);
        if (newNode->company_name) {
            strcpy(newNode->company_name, company_name);
            newNode->employee_counter = employee_counter;
        } else {
            free(newNode);
            newNode = NULL;
        }
    }
    return newNode;
}

我更喜欢使用 mallocstrcpy,而不是 strdup,因为 strdup 不是由标准定义的(尽管它是由 POSIX 定义的),并且使用 strdup 没有任何优势。

If you know the length of company_name before calling your make_company_node function, I'd change the function to accept that length (use strlen otherwise).

company *make_company_node(char *company_name, size_t cn_len, int employee_counter) {
    company *newNode = malloc(sizeof *newNode);
    if (newNode) {
        newNode->company_name = malloc(cn_len + 1);
        if (newNode->company_name) {
            strcpy(newNode->company_name, company_name);
            newNode->employee_counter = employee_counter;
        } else {
            free(newNode);
            newNode = NULL;
        }
    }
    return newNode;
}

I prefer to do it with malloc and strcpy, instead of strdup, because strdup is not defined by the Standard (it is defined by POSIX though), and there's no advantage of using strdup.

折戟 2024-10-14 11:16:10

正如 pmg 提到的,如果您事先知道字符串的长度,或者已知可以设置的最大字符数,则更愿意将 company_name 定义为静态数组:


char 公司名称[MAX_LENGTH];

这将减少您需要执行的内存管理量,以及您刚刚遇到的潜在错误。

If, as pmg mentioned, you know in advance the length of the string or if there is a known maximum number of characters that you can set, prefer defining company_name as a static array instead:


char company_name[MAX_LENGTH];

This will reduce the amount of memory management you need to do, along with potential errors as you just experienced.

醉殇 2024-10-14 11:16:10

嗯,问题似乎出在 company_name 变量上。这是指向公司名称实际数据的指针。您如何分配这些数据?你确定这是正确的指针吗?也许您需要这样的东西:

newNode->company_name = strdup(company_name);

strdup动态地复制您传递给它的字符串。不要忘记释放它。

这是常见错误,因为 company_name 包含某些数据的地址。如果使用相同的地址并且其中的数据发生变化,您最终会得到不同的结果,因此您需要复制它。

Hrm, it seems like the issue is with the company_name variable. This is a pointer to the actual data of the company name. How are you allocating this data? Are you sure it's the right pointer? Maybe you need something like this:

newNode->company_name = strdup(company_name);

strdup duplicates the string you pass to it, dynamically. Don't forget to free it.

This is common error because company_name contains the address of some data. If the same address is used and the data inside it changes, you end up with different results, therefore you need to make a copy of it.

听风吹 2024-10-14 11:16:10

您需要复制company_name字符串:

newNode->company_name = strdup(company_name);

之后不要忘记free()它。

You need to duplicate the company_name string:

newNode->company_name = strdup(company_name);

Don't forget to free() it afterwards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文