C 节点 - 如何不覆盖分配的内存空间而是创建新的(作业问题)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您在调用
make_company_node
函数之前知道 company_name 的长度,我会更改该函数以接受该长度(否则使用 strlen)。我更喜欢使用
malloc
和strcpy
,而不是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).I prefer to do it with
malloc
andstrcpy
, instead ofstrdup
, becausestrdup
is not defined by the Standard (it is defined by POSIX though), and there's no advantage of usingstrdup
.正如 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.
嗯,问题似乎出在
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: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.您需要复制company_name字符串:
之后不要忘记
free()
它。You need to duplicate the company_name string:
Don't forget to
free()
it afterwards.