我没有收到以下字符串输出:
struct stringItem { int len; char str[1]; }void allocationStringBuffer (char* stringContent, struct stringItem *string) { // dynamically sized object int n; n = strlen(stringContent); //struct stringItem *string = malloc(sizeof(struct stringItem) + n); string = malloc(sizeof(struct stringItem) + n); if (string == NULL) { // check if malloc is successful printf("Memory allocation for string fails.\n"); // exit(-1); } strcpy(string->str, stringContent); printf("Struct string: %s\n", string->str); string->len = n; }
in main:
struct stringItem *string2; allocationStringBuffer ("helloWorld", string2); printf("Struct string: %s\n", (*string2).str); free(string2); allocationStringBuffer ("another Statement...", string2); printf("Struct string: %s\n", string2->str); free(string2);
The result is:
Struct string: helloWorld Struct string: ÉÉÉÉÉï Uï∞â∞¶SVWh♦☺ Struct string: another Statement... Struct string: ÉÉÉÉÉï Uï∞â∞¶SVWh♦☺Thank you for your help.
[Updates with Thanks to ALL] Here is the full working code. It has been resolved. Thank you to ALL. struct stringItem { int len; char str[1]; }; void allocationStringBuffer (char* stringContent, struct stringItem** pstring) { // dynamically sized object int n; n = strlen(stringContent); struct stringItem *string; string = malloc(sizeof(struct stringItem) + (n+1)); if (string == NULL) { // check if malloc is successful printf("Memory allocation for string fails.\n"); // exit(-1); } strcpy(string->str, stringContent); printf("Struct string: %s\n", string->str); string->len = n; *pstring = string; // Copy allocated pointer to out-parameter. }in main
struct stringItem *string2; allocationStringBuffer ("helloWorld", &string2); printf("Struct string: %s\n", (*string2).str); free(string2); allocationStringBuffer ("another Statement...", &string2); printf("Struct string: %s\n", string2->str); free(string2);
答案 0 :(得分:3)
在我看来,你需要做出这些改变:
allocationStringBuffer ("helloWorld", &string2); // Pass ADDRESS of string2, not just string2
void allocationStringBuffer (char* stringContent, struct stringItem **pstring)
{
// dynamically sized object
int n;
n = strlen(stringContent);
struct stringItem* string; // Local variable, will be later copied to function parameter.
string = malloc(sizeof(struct stringItem) + n);
if (string == NULL) { // check if malloc is successful
printf("Memory allocation for string fails.\n");
// exit(-1);
}
strcpy(string->str, stringContent);
printf("Struct string: %s\n", string->str);
string->len = n;
*pstring = string; // Copy allocated pointer to out-parameter.
}
答案 1 :(得分:0)
您在 malloc 调用中缺少终止空字符。
string = malloc(sizeof(struct stringItem) + (n + 1));
strlen 为您提供不包含终止空字符的大小。
此致
答案 2 :(得分:0)
您将一个指向stringItem的指针传递给该函数,然后使用malloc
的返回值覆盖该参数,然后将其丢弃,从而泄漏其内存。您有两种方法可以解决这个问题:
void allocationStringBuffer(char* stringContent, struct stringItem** string) {
int length = strlen(stringContent);
*string = malloc(sizeof(struct stringItem) + length + 1);
/* The +1 is padding. You do want that. */
strcpy(*string->str, stringContent);
}
或:
struct stringItem* allocationStringBuffer(char* stringContent) {
int length = strlen(stringContent);
struct stringItem* string = malloc(sizeof(struct stringItem) + length + 1);
strcpy(string->str, stringContent);
return string;
}
基本上,你必须以某种方式返回你malloc
的指针。你不能像你一样向它发送一个指向堆栈stringItem
的指针(即一个局部变量)。
答案 3 :(得分:0)
这个似乎不对:
string = malloc(sizeof(struct stringItem) + n);
你想在这做什么? string
是struct stringItem
的元素。所以我认为你只需要分配string = malloc(sizeof(struct stringItem));
p / s:如果你发布struct stringItem的声明
,我应该会很好