typedef struct NTC_network{
unsigned char NET_ID;
unsigned char DEV_ADDR;
}NTC_network;
typedef struct NTC_io_trx_link{
enum NTC_IO_LINK own_io;
unsigned char ADDR;
enum NTC_IO_LINK trx_io;
}NTC_io_trx_link;
//struct to hold status of I/O. 0==OFF 1==ON
typedef struct NTC_config_stpio{
unsigned char L1:1;
unsigned char L2:1;
unsigned char L3:1;
unsigned char L4:1;
unsigned char E1:1;
unsigned char E2:1;
unsigned char T1:1;
unsigned char T2:1;
}NTC_config_stpio;
typedef struct NTC_header {
unsigned char NET_ID;
unsigned char DST; //destination address
unsigned char SRC; //source address
unsigned char PCKT;
}NTC_header;
typedef struct NTC_default_setup{
unsigned char DEFAULT_SETUP:1;
}NTC_default_setup;
typedef struct NTC_data{
unsigned char E1; //resistor value to emulate on E1
unsigned char E2; //resistor value to emulate on E2
unsigned char T1; //resistor value of temperature sensor on T1
unsigned char T2; //resistor value of temperature sensor on T2
unsigned char D1; //230VAC detection circuit on/off
unsigned char D2; //230VAC detection circuit on/off
unsigned char ALRM; //alarm on off 0xFF=alarm on, 0x00=alarm off
}NTC_data;
typedef struct NTC_ack{
unsigned char ACK; //ACK field 0x00=no ack 0xFF= ack
}NTC_ack;
typedef struct NTC_config {
NTC_default_setup *DEFAULT_STP;
NTC_config_stpio *STPIO;
NTC_config_network *NETWORK;
NTC_io_trx_link *NTC_link_io;
}NTC_config;
//struct for payload to send via LoRa
typedef struct NTC_payload{
NTC_header *Header;
enum NTC_dc DC;
NTC_config *Config;
NTC_data *Data;
NTC_ack *Ack;
}NTC_payload;
typedef struct NTC_runtime_data{
NTC_payload *pPayload;
}NTC_runtime_data;
并在运行时分配整个事物:
NTC_runtime_data* init_struct_runtime_data(void){
NTC_runtime_data *DATA = calloc(1, sizeof(NTC_runtime_data));
DATA->pPayload = calloc(2, sizeof(NTC_payload));
for(int i = 0 ; i < 2; i++){
DATA->pPayload[i].Header = calloc(1, sizeof(NTC_header));
DATA->pPayload[i].DC = NONE;
DATA->pPayload[i].Config = calloc(1, sizeof(NTC_config));
DATA->pPayload[i].Config->DEFAULT_STP = calloc(1, sizeof(NTC_default_setup));
DATA->pPayload[i].Config->STPIO = calloc(1, sizeof(NTC_config_stpio));
DATA->pPayload[i].Config->NETWORK = calloc(1, sizeof(NTC_config_network));
DATA->pPayload[i].Config->NETWORK->NTC_NETWORK = calloc(1, sizeof(NTC_network));
DATA->pPayload[i].Config->NTC_link_io = calloc(11, sizeof(NTC_io_trx_link));
for(int j = 0; j < 11; j++){
DATA->pPayload[i].Config->NTC_link_io[j].own_io = j + 1;
}
DATA->pPayload[i].Data = calloc(1, sizeof(NTC_data));
DATA->pPayload[i].Ack = calloc(1, sizeof(NTC_ack));
}
return DATA;
}
我正在分配2个pPayload。一个用于TX数据,一个用于RX。 所有这一切都很好,所以如此分配值是没有问题的:
NTC_runtime_data *pDATA;
pDATA = init_struct_runtime_data();
然后我可以通过这个结构,我可以分配如下:
pData->pPayload[0].Header->NET_ID = 0x20;
但是我遇到了尝试从包含数组的结构作为缓冲区分配的问题,如下所示:
typedef struct TRX_buffer{
size_t used;
size_t size;
unsigned char *array;
}TRX_buffer;
并初始化并重新分配:
TRX_buffer* init_struct_trx_buffer(void){
TRX_buffer *trx_buffer = malloc(sizeof(TRX_buffer));
trx_buffer->array = malloc(4 * sizeof(unsigned char));
trx_buffer->size = 4;
trx_buffer->used = 0;
return trx_buffer;
}
//inserts a byte of data in the buffer and dynamically allocates the buffer
void insert_array(TRX_buffer *trxbuffer, unsigned char data){
if(trxbuffer->used == trxbuffer->size){
trxbuffer->size *= 2;
trxbuffer[0].array = realloc(trxbuffer[0].array, trxbuffer->size * sizeof(unsigned char));
}
trxbuffer[0].array[trxbuffer->used] = data;
trxbuffer->used++;
}
所以问题是,在我的程序中,在函数中的某些地方,我在创建缓冲区的新实例并从收音机加载数据,我可以在调试时看到,我想将它加载到我的结构中我这样做
NTC_payload* get_payload(NTC_payload *pPayload){
TRX_buffer *rx_buffer;
rx_buffer = init_struct_trx_buffer();
r_FIFO(rx_buffer);
pDATA->pPayload[1].Header->NET_ID = trx_buffer->array[0];
正确的数据位于我trx_buffer
函数后的r_FIFO()
。但这不起作用。我尝试过不同的方法。但是不能让它从我的缓冲区获取值并将其分配给我的结构。
有什么不对?
答案 0 :(得分:1)
您没有从init_struct_runtime_data()返回任何内容,因此pDATA = init_struct_runtime_data();得到发生在返回登记册中的任何事情。
任何体面的编译器都应该标记这一点,如果你自己没有帮忙并卸载它。
ps:这里有一个常见问题,它涉及发布完整的程序,以便人们可以使用他们的工具来尝试。这是一个好主意,因为简单地将问题简化为可发布版本就可以解决问题。