将内存分配给结构指针时出错
Error while allocating memory to a structure pointer
我正在尝试为全局声明的结构指针分配内存。但是我收到了这个错误 --> error: initializer element is not constant
typedef struct A {
uint32_t arr[30][4096];
uint32_t var1;
uint8_t var2;
bool var3;
}B;
B *x = (B*)malloc(sizeof(B));
谁能解释一下我哪里做错了。
另外,除了动态内存分配,是否有直接的方法将内存分配给结构指针?
提前致谢。
您不能像声明变量那样在全局范围内使用 malloc
。
您必须使用常量来初始化全局范围内的变量。
您可以在函数内部使用它(它是一个函数调用)
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef struct {
uint32_t arr[30][4096];
uint32_t var1;
uint8_t var2;
bool var3;
}A;
A *x;
int main()
{
x = malloc(sizeof(A));
if (x != NULL)
{
free(x);
}
return 0;
}
形成c99标准。
6.7.8 Initialization
- All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string
literals.
常量定义如下:
6.4.4 Constants
Syntax
constant:
integer-constant (e.g. 4, 42L)
floating-constant (e.g. 0.345, .7)
enumeration-constant (stuff in enums)
character-constant (e.g. 'c', '[=11=]')
标准定义常量表达式如下:
6.6 Constant expressions
(7) More latitude is permitted for constant expressions in initializers. Such a constant expression shall be, or evaluate to, one
of the following:
— an arithmetic constant expression,
— a null pointer constant,
— an address constant, or
— an address constant for an object type plus or minus an integer constant expression.
(8) An arithmetic constant expression shall have an arithmetic type and shall
only have operands that are integer constants, floating constants,
enumeration constants, character constants, and sizeof expressions.
Cast operators in an arithmetic constant expression shall only convert
arithmetic types to arithmetic types, except as part of an operand to
a sizeof operator whose result is an integer constant.
C 不允许您在全局范围内(函数之外)使用泛型语句或表达式,甚至对于初始值设定项也不行。在全局范围内只允许编译时常量作为初始值设定项。
如果你想初始化指针,那么你需要在函数内将其作为赋值来完成,也许是 main
函数中的第一件事:
// Definition of variable
B *x;
int main(void)
{
// Initialization of variable through assignment
x = malloc(sizeof *x);
...
}
我正在尝试为全局声明的结构指针分配内存。但是我收到了这个错误 --> error: initializer element is not constant
typedef struct A {
uint32_t arr[30][4096];
uint32_t var1;
uint8_t var2;
bool var3;
}B;
B *x = (B*)malloc(sizeof(B));
谁能解释一下我哪里做错了。 另外,除了动态内存分配,是否有直接的方法将内存分配给结构指针? 提前致谢。
您不能像声明变量那样在全局范围内使用 malloc
。
您必须使用常量来初始化全局范围内的变量。
您可以在函数内部使用它(它是一个函数调用)
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef struct {
uint32_t arr[30][4096];
uint32_t var1;
uint8_t var2;
bool var3;
}A;
A *x;
int main()
{
x = malloc(sizeof(A));
if (x != NULL)
{
free(x);
}
return 0;
}
形成c99标准。
6.7.8 Initialization
- All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.
常量定义如下:
6.4.4 Constants
Syntax
constant:
integer-constant (e.g. 4, 42L) floating-constant (e.g. 0.345, .7) enumeration-constant (stuff in enums) character-constant (e.g. 'c', '[=11=]')
标准定义常量表达式如下:
6.6 Constant expressions
(7) More latitude is permitted for constant expressions in initializers. Such a constant expression shall be, or evaluate to, one of the following:
— an arithmetic constant expression,
— a null pointer constant,
— an address constant, or
— an address constant for an object type plus or minus an integer constant expression.
(8) An arithmetic constant expression shall have an arithmetic type and shall only have operands that are integer constants, floating constants, enumeration constants, character constants, and sizeof expressions. Cast operators in an arithmetic constant expression shall only convert arithmetic types to arithmetic types, except as part of an operand to a sizeof operator whose result is an integer constant.
C 不允许您在全局范围内(函数之外)使用泛型语句或表达式,甚至对于初始值设定项也不行。在全局范围内只允许编译时常量作为初始值设定项。
如果你想初始化指针,那么你需要在函数内将其作为赋值来完成,也许是 main
函数中的第一件事:
// Definition of variable
B *x;
int main(void)
{
// Initialization of variable through assignment
x = malloc(sizeof *x);
...
}