-1

我没有从以下获得字符串输出:

结构字符串项 {
    国际化;
    字符 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;     
}

主要:

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);

结果是:

结构字符串:helloWorld
结构字符串:ÉÉÉÉÉï Uï∞â∞¶SVWh♦☺
结构字符串:另一个语句...
结构字符串:ÉÉÉÉÉï Uï∞â∞¶SVWh♦☺

感谢您的帮助。

[感谢所有更新]
这是完整的工作代码。已解决。谢谢你们。

结构字符串项 {
    国际化;
    字符 str[1];
};


void allocationStringBuffer (char* stringContent, struct stringItem** pstring) {

    // 动态大小的对象
    诠释n;
    n = strlen(字符串内容);

    结构字符串项目*字符串;
    字符串 = malloc(sizeof(struct stringItem) + (n+1));

    if (string == NULL) { // 检查 malloc 是否成功
       printf("字符串内存分配失败。\n");
       // 退出(-1);
    }   

   strcpy(string->str, stringContent);
   printf("结构字符串:%s\n", string->str);
   字符串->len = n;     

   *pstring = 字符串;// 将分配的指针复制到输出参数。
}


主要是

结构字符串项目 *string2; allocationStringBuffer ("helloWorld", &string2); printf("结构字符串:%s\n", (*string2).str); 免费(字符串2); allocationStringBuffer("另一个语句...", &string2); printf("结构字符串:%s\n", string2->str); 免费(字符串2);
4

4 回答 4

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.
}
于 2013-10-30T16:03:24.273 回答
0

您在malloc调用中缺少终止空字符。

string = malloc(sizeof(struct stringItem) + (n + 1));

strlen为您提供不包括终止空字符的大小。

问候,

于 2013-10-30T16:00:52.237 回答
0

您将指向 stringItem 的指针传递给函数,然后用 return from 覆盖参数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像您正在做的那样向它发送指向堆栈上的 a 的指针(即局部变量)。

于 2013-10-30T16:06:20.383 回答
0

这个好像不对:

string = malloc(sizeof(struct stringItem) + n);

你想在这里做什么?string是 的一个元素struct stringItem。所以我认为你只需要分配string = malloc(sizeof(struct stringItem));

p/s:如果您也发布 struct stringItem 的声明,我应该会很好

于 2013-10-30T16:22:56.490 回答