c - 如何在不使用动态内存分配的情况下将字符指针填充到另一个指针中
问题描述
我正在尝试在结构内的另一个指针内分配一个字符指针。我的环境没有 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);
}
解决方案
如果您不想在堆中进行动态分配,请替换
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 个字符(不计算空字符)string_container->string->textt[i] = 0;
,请在for或替换i < 5
后添加i <= 5
以也复制空字符
当然替换char textt[5];
为char textt[6];
instruct custom_string
注意在大小写 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(如果太长而无法在textt中记住,则不是strcpy)而不是for循环来简单地完成复制byte_array
推荐阅读
- image - Xamarin Binding 函数作为具有数据绑定的 Listview 中的 ImageSource
- python - 使用 python scrapy 库进行网页抓取(python 3)
- java - 如何管理 XML 模式及其生成的类的版本和部署?
- corda - Spring Web 服务器无法加载 Cordapp。找不到 State 类。错误 - AMQ214000:调用 onMessage 失败
- android - Android资源链接在android中失败
- python - 如何使用 Imagemagick 或 PIL 绘制高质量的图像描边(边框)?
- matlab - 设置多个堆叠条的堆叠颜色
- c# - AppDomain.CreateInstance() 参数会发生什么?
- html - 如何为网格布局中的多个生成项目编写响应式 CSS?
- flutter - 如何防止多次单击手势?