【问题标题】:How to populate a character pointer inside another pointer without using dynamic memory allocation如何在不使用动态内存分配的情况下将字符指针填充到另一个指针中
【发布时间】:2019-06-23 18:29:54
【问题描述】:

我试图在结构内的另一个指针内分配一个字符指针。我的环境没有 malloc/calloc,因此动态内存分配不是一种选择。我如何才能在 read_string_from_byte_array() 函数中填充字符指针?

    typedef struct custom_string
    {
        char textt[5];
        int length;
    }custom_string;

    typedef struct custom_string_container
    {
        custom_string* string;
    }custom_string_container;

    void read_string_from_byte_array(custom_string_container* string_container)
    {
        char* byte_array = "12345";
        int i;

        puts("assigning");
        for(i = 0; i < 5; i++)
        {
            string_container->string->textt[i] = byte_array[i]; //failing with exit code 139
        }

        puts("done assigning");

    }



    void main()
    {
        //dynamic memory allocation is strictly prohibited
        custom_string_container string_container;

        read_string_from_byte_array(&string_container);
        printf("read string is %s \n", string_container.string->textt);

    }

【问题讨论】:

  • 你得到什么警告?
  • 您显示的代码存在多个问题。首先,您使用没有初始化的指针(string_container-&gt;string 没有初始化为指向任何地方)。其次,您的textt 成员是一个指向char指针 数组,即它可以被视为一个(可能的)字符串数组。第三,您忘记了 C 中的 char 字符串实际上称为 null-terminated 字节字符串。 null-terminator 是所有标准字符串函数用来查找字符串结尾的函数(包括带有 "%s" 格式说明符的 printf 函数)。
  • 您的 char* textt[5] 字段是由五个字符指针组成的表,因此 ttext[i]char*byte_array[i]char 并且您正在尝试将 char 分配给 char*
  • char* textt[5] 是一个错字。
  • minimal reproducible example 的一点是我们基本上应该能够复制粘贴它并获得与您获得的完全相同的结果。这包括获得与您可能遇到的完全相同的构建警告和错误,如果您没有收到任何构建警告或错误,请确保您显示的代码也没有任何警告或错误,即使从编译器启用更多警告也是如此。

标签: c memory-management dynamic-memory-allocation


【解决方案1】:

如果您不想在堆中进行动态分配,请替换

typedef struct custom_string_container
{
    custom_string * string;
}custom_string_container;

通过

typedef struct custom_string_container
{
    custom_string string;
}custom_string_container;

当然在需要的时候用'.'替换'->'

还有警告

    for(i = 0; i < 5; i++)
    {
        string_container->string->textt[i] = byte_array[i]; //failing with exit code 139
    }

空终止字符丢失,这是做时的问题

printf("read string is %s \n", string_container.string->textt);

如果你想存储 5 个字符(不计算空字符) 在 for 之后添加 string_container-&gt;string-&gt;textt[i] = 0; 或将 i &lt; 5 替换为 i &lt;= 5 以同时复制空字符

当然在struct custom_string中用char textt[6];替换char textt[5];

注意在大小写 6 然后在其他地方写 5 或 6 是危险的,以防你改变大小,使用 sizeof 更安全

终于有了所有的变化:

typedef struct custom_string
{
    char textt[6];
    int length;
}custom_string;

typedef struct custom_string_container
{
    custom_string string;
}custom_string_container;

void read_string_from_byte_array(custom_string_container* string_container)
{
    char* byte_array = "12345";
    int i;

    puts("assigning");
    for(i = 0; i < sizeof(string_container->string.textt); i++)
    {
        string_container->string.textt[i] = byte_array[i]; 
    }
    string_container->string.textt[i] = 0;

    puts("done assigning");

}



void main()
{
    //dynamic memory allocation is strictly prohibited
    custom_string_container string_container;

    read_string_from_byte_array(&string_container);
    printf("read string is %s \n", string_container.string.textt);

}

执行:

assigning
done assigning
read string is 12345 

valgrind 下:

pi@raspberrypi:~ $ valgrind ./a.out 
==8507== Memcheck, a memory error detector
==8507== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==8507== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==8507== Command: ./a.out
==8507== 
assigning
done assigning
read string is 12345 
==8507== 
==8507== HEAP SUMMARY:
==8507==     in use at exit: 0 bytes in 0 blocks
==8507==   total heap usage: 1 allocs, 1 frees, 1,024 bytes allocated
==8507== 
==8507== All heap blocks were freed -- no leaks are possible
==8507== 
==8507== For counts of detected and suppressed errors, rerun with: -v
==8507== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)

还要注意,read_string_from_byte_array 中的复制可以简单地使用 strncpy 完成(如果 byte_array 太长而无法在 中记住,则不能使用 strcpy textt) 而不是 for 循环

【讨论】:

  • 程序在 i 为零时失败,它与空终止符无关。即使在 null 终止最后一个元素后,程序仍然崩溃。
  • @salimsaid 那是因为string_container-&gt;string 没有初始化。
  • @Osiris 我如何在不使用 malloc 的情况下初始化 string_container->string ?
猜你喜欢
  • 2013-09-22
  • 2013-10-22
  • 2015-09-12
  • 2021-02-14
  • 2020-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多