struct x {
char a[10];
char b[20];
int i;
char *c;
char *d[10];
};
Run Code Online (Sandbox Code Playgroud)
我正在填充此结构,然后使用值.在下一次迭代中,我想在重新开始重用之前0
或null
之前重置所有字段.
我怎样才能做到这一点?我可以使用memset
或者必须通过所有成员然后单独进行吗?
例如,如果somestruct
有三个整数成员,我一直认为在C(或C++)函数中执行此操作是可以的:
somestruct s = {123,};
Run Code Online (Sandbox Code Playgroud)
第一个成员将初始化为123,最后两个成员将初始化为0.我经常对自动数组执行相同的操作,写入int arr[100] = {0,};
以便将数组中的所有整数初始化为零.
最近我在GNU C参考手册中读到:
如果不初始化结构变量,则效果取决于它是否具有静态存储(请参阅存储类说明符).如果是,则使用0初始化具有整数类型的成员,并将指针成员初始化为NULL; 否则,结构成员的价值是不确定的.
有人可以告诉我C和C++标准对部分自动结构和自动数组初始化的看法吗?我在Visual Studio中执行上述代码没有问题,但我希望与gcc/g ++兼容,也可能与其他编译器兼容.谢谢
这是一个相关的 C 答案,在 C++ 中不起作用(作为结构的零初始化器):Initializing a struct to 0。提出的解决方案之一是:
myStruct _m1 = {0};
Run Code Online (Sandbox Code Playgroud)
这在 C 中工作正常,但在 C++ 中不起作用。:( :
错误:无法使用“int”类型的右值初始化“myScope::MyStruct”类型的成员子对象。
如何在 C++ 中对结构体进行零初始化?
我的问题不是这个其他问题的重复(Initialization with empty curlybraces),因为这个其他问题不是询问在 C++ 中初始化结构体的各种方法以及为什么 C 方法不起作用,而是它们是询问为什么 C++ 关键字会explicit
破坏他们的初始化技术之一。两个不同的问题。不重复。
在这段代码中,将所有100项C.B
初始化为零?
struct A { int B[100]; int D; };
A C = {0, 0};
Run Code Online (Sandbox Code Playgroud)
它似乎工作,但记忆可能提前是空的.
I'm debugging some code that essentially is identical to this:
struct Foo { int a; int b; };
struct Bar { Bar() {} Foo foo{0}; };
Run Code Online (Sandbox Code Playgroud)
When I make an instance of Bar
, it seems like both a
and b
are initialized to zero. Is this guaranteed? Where can I find that in the spec?
管理联合的未初始化字节的规则是什么?(假设有些是初始化的)
下面是一个32字节的并集,我通过第一个成员初始化前16个字节.似乎剩余的字节是零初始化的.这对我的用例很有用,但我想知道这背后的规则是什么 - 我在期待垃圾.
#include <cstdint>
#include <iostream>
using namespace std;
union Blah {
struct {
int64_t a;
int64_t b;
};
int64_t c[4];
}
int main()
{
Blah b = {{ 1, 2 }}; // initialize first member, so only the first 16 bytes.
// prints 1, 2, 0, 0 -- not 1, 2, <garbage>, <garbage>
cout << b.c[0] << ", " << b.c[1] << ", " << b.c[2] << ", " << b.c[3] << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我已经编译了GCC …
我遇到了一些与此类似的代码:
struct Struct
{
int a;
int b;
};
int main()
{
struct Struct variable = { }; // ???
variable.a = 4;
variable.b = 6;
}
Run Code Online (Sandbox Code Playgroud)
这很奇怪.初始化a
和b
可能在初始化器中发生(在花括号之间).但他们不是.保持这个= { }
部分有什么意义?以下应该没问题吧?
struct Struct variable;
Run Code Online (Sandbox Code Playgroud)
或者是吗?是否有一个空的初始化器与任何不具有初始化器的方式不同?
我的小C手册说明了这一点
对于没有初始化器的变量:具有静态作用域的所有变量都隐式初始化为零(即所有字节= 0).所有其他变量都有未定义的值!
需要注意的是,这并没有明确提到struct
s.
什么时候没有初始化的条件满足了struct
?我做了以下测试:
#include <stdio.h>
struct Struct
{
int a;
int b;
};
int main()
{
struct Struct foo = { };
foo.a = 4;
struct Struct bar; …
Run Code Online (Sandbox Code Playgroud) 我想避免memset()
在这样的结构上使用:
typedef struct {
int size;
float param1;
} StructA;
typedef struct {
StructA a;
unsigned int state;
float param2;
} Object;
Run Code Online (Sandbox Code Playgroud)
我可以做这样的事情(伪代码,我现在无法检查)?
Object obj;
int *adr = (int*)&obj;
for (int i; i < sizeof(obj); i++) {
*adr++ = 0;
}
Run Code Online (Sandbox Code Playgroud)
它会将obj的每个成员设置为零吗?
编辑:回答评论问题.我一直在研究一些情况(使用uni-type结构),其memset
速度比手动初始化慢两倍.所以我会考虑尝试初始化多类型结构.
避免memcpy()
也会很好(避免使用<string.h>
lib).