My apologies in advance for the bad-looking code. It's just a proof of concept.
The purpose of the program is to fill the "datapacket", but by individually accessible pointers of the potmeter struct.
The program works with a complete type of *potPointers[3]. It also works with incomplete type *potPointer[]. But then after a successful run, I get:
*** stack smashing detected ***: terminated Aborted (core dumped)
How do I successfully deal with incomplete pointer arrays to a struct?
I can of course put a #define, but is there another way?
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#define BYTES_DATAWORD_POTMETER 2
typedef struct
{
uint8_t test;
uint8_t *datawordPointer[2];
}potmeter;
typedef struct
{
uint8_t amountOfPots;
size_t datapacket_size;
uint8_t *datapacket; // needs to be malloc'ed, don't forget to free it.
// Sort of working with *** stack smashing detected ***: terminated Aborted (core dumped)
potmeter *potPointers[];
// Works without stack smashing.
// potmeter *potPointers[3];
}overall;
void initPointersToDatapackett(overall *allePots)
{
uint8_t counter = 0;
for(int i = 0; i < allePots->amountOfPots; i++)
{
// Allocate memory per pot
allePots->potPointers[i] = malloc(sizeof(potmeter));
// Point pointers to dataword of overall
allePots->potPointers[i]->datawordPointer[0] = &allePots->datapacket[counter++];
allePots->potPointers[i]->datawordPointer[1] = &allePots->datapacket[counter++];
}
}
int main()
{
overall AllePotMeters;
AllePotMeters.amountOfPots = 3;
AllePotMeters.datapacket = malloc(AllePotMeters.amountOfPots*BYTES_DATAWORD_POTMETER);
initPointersToDatapackett(&AllePotMeters);
//Test content of pointers to dataword
*AllePotMeters.potPointers[0]->datawordPointer[0] = 'A';
*AllePotMeters.potPointers[0]->datawordPointer[1] = 'B';
*AllePotMeters.potPointers[1]->datawordPointer[0] = 'C';
*AllePotMeters.potPointers[1]->datawordPointer[1] = 'D';
*AllePotMeters.potPointers[2]->datawordPointer[0] = 'E';
*AllePotMeters.potPointers[2]->datawordPointer[1] = '\0';
printf("String test %s\n", AllePotMeters.datapacket);
free(AllePotMeters.potPointers[0]);
free(AllePotMeters.potPointers[1]);
free(AllePotMeters.potPointers[2]);
free(AllePotMeters.datapacket);
return 0;
}
potmeter *potPointers[]is weird. In a data packet I would expect a type likepotmeter potPointers[], because you want a contiguous memory buffer to send somewhere. This code makes little sense to me in that regard. You also don't seem to allocate memory fordatawordPointeranywhere.datawordPointerpoints to thedatawordof the overall struct, which is malloc'd.[]doesn't mean "this array will grow on demand" or "this array magically has enough elements for everything". What do you think it does mean?potPointera double pointer:potmeter **potPointers;and then malloc the amount of pointers it need.