Click here to Skip to main content
15,891,033 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: C++ Queue and Stacks Pin
CPallini22-Oct-17 5:02
mveCPallini22-Oct-17 5:02 
AnswerRe: C++ Queue and Stacks Pin
Richard MacCutchan22-Oct-17 11:15
mveRichard MacCutchan22-Oct-17 11:15 
AnswerRe: C++ Queue and Stacks Pin
David Crow22-Oct-17 17:02
David Crow22-Oct-17 17:02 
AnswerRe: C++ Queue and Stacks Pin
jschell23-Oct-17 7:30
jschell23-Oct-17 7:30 
QuestionFind out optimum volume for packing different boxes Pin
Member 1347639321-Oct-17 18:25
Member 1347639321-Oct-17 18:25 
AnswerRe: Find out optimum volume for packing different boxes Pin
Richard MacCutchan21-Oct-17 20:48
mveRichard MacCutchan21-Oct-17 20:48 
GeneralRe: Find out optimum volume for packing different boxes Pin
Sascha Lefèvre21-Oct-17 21:01
professionalSascha Lefèvre21-Oct-17 21:01 
AnswerRe: Find out optimum volume for packing different boxes Pin
CPallini22-Oct-17 5:07
mveCPallini22-Oct-17 5:07 
GeneralRe: Find out optimum volume for packing different boxes Pin
David Crow22-Oct-17 17:03
David Crow22-Oct-17 17:03 
QuestionCan I put a variable length array inside a struct somehow? Pin
arnold_w17-Oct-17 0:38
arnold_w17-Oct-17 0:38 
AnswerRe: Can I put a variable length array inside a struct somehow? Pin
Peter_in_278017-Oct-17 0:57
professionalPeter_in_278017-Oct-17 0:57 
GeneralRe: Can I put a variable length array inside a struct somehow? Pin
arnold_w17-Oct-17 1:08
arnold_w17-Oct-17 1:08 
GeneralRe: Can I put a variable length array inside a struct somehow? Pin
Peter_in_278017-Oct-17 1:43
professionalPeter_in_278017-Oct-17 1:43 
AnswerRe: Can I put a variable length array inside a struct somehow? Pin
CPallini17-Oct-17 1:54
mveCPallini17-Oct-17 1:54 
GeneralRe: Can I put a variable length array inside a struct somehow? Pin
arnold_w17-Oct-17 2:02
arnold_w17-Oct-17 2:02 
GeneralRe: Can I put a variable length array inside a struct somehow? Pin
CPallini17-Oct-17 2:24
mveCPallini17-Oct-17 2:24 
GeneralRe: Can I put a variable length array inside a struct somehow? Pin
arnold_w17-Oct-17 2:46
arnold_w17-Oct-17 2:46 
GeneralRe: Can I put a variable length array inside a struct somehow? Pin
CPallini17-Oct-17 2:58
mveCPallini17-Oct-17 2:58 
GeneralRe: Can I put a variable length array inside a struct somehow? Pin
jschell17-Oct-17 7:06
jschell17-Oct-17 7:06 
GeneralRe: Can I put a variable length array inside a struct somehow? Pin
Richard MacCutchan17-Oct-17 4:50
mveRichard MacCutchan17-Oct-17 4:50 
GeneralRe: Can I put a variable length array inside a struct somehow? Pin
arnold_w17-Oct-17 8:26
arnold_w17-Oct-17 8:26 
GeneralRe: Can I put a variable length array inside a struct somehow? Pin
leon de boer17-Oct-17 16:30
leon de boer17-Oct-17 16:30 
AnswerRe: Can I put a variable length array inside a struct somehow? Pin
jschell17-Oct-17 7:20
jschell17-Oct-17 7:20 
arnold_w wrote:
an embedded microcontroller (ARM)


Because of that I am going to presume that maybe you can't rely on the newest features (other suggestions).

Didn't see anyone mention the old school approach which should work on any C compiler unless perhaps one goes back to the 70s (perhaps even then.)

I am not going to try to even create pseudo code for this (way too long for me) but basic outline
- Add attributes for the size of each array
- All attributes EXCEPT arrays must be before arrays themselves. So arrays are at the end.
- Alloc the struct based on the size of the array PLUS the appropriate sizes of the arrays.
- Create helper methods that use and OFFSET to access the array pointer based on the sizes (attribute above) for each array.

Google for the following for examples. Look for examples that have something like "int[] array" at the end of the structure.

C programming language struct dynamic array


One gotcha which is probably still relevant is that, maybe, C compilers attempt to 'align' attributes in the struct, these days. That means it might add filler that you are unaware of. If so there should be a compiler option, to remove that.

This solution means that you cannot treat the array attributes as pointers. Specifically do not try to set them to another pointer. That is because the array storage is in the struct allocated block itself. So you cannot free it. And assignment would mean you couldn't free the assigned block either.
GeneralRe: Can I put a variable length array inside a struct somehow? Pin
arnold_w17-Oct-17 8:33
arnold_w17-Oct-17 8:33 
Questioninet error Pin
Member 1346819116-Oct-17 5:22
Member 1346819116-Oct-17 5:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.