Click here to Skip to main content
15,566,733 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Can I put a variable length array inside a struct somehow? Pin
CPallini17-Oct-17 3:24
mveCPallini17-Oct-17 3:24 
GeneralRe: Can I put a variable length array inside a struct somehow? Pin
arnold_w17-Oct-17 3:46
arnold_w17-Oct-17 3:46 
GeneralRe: Can I put a variable length array inside a struct somehow? Pin
CPallini17-Oct-17 3:58
mveCPallini17-Oct-17 3:58 
GeneralRe: Can I put a variable length array inside a struct somehow? Pin
jschell17-Oct-17 8:06
jschell17-Oct-17 8:06 
GeneralRe: Can I put a variable length array inside a struct somehow? Pin
Richard MacCutchan17-Oct-17 5:50
mveRichard MacCutchan17-Oct-17 5:50 
GeneralRe: Can I put a variable length array inside a struct somehow? Pin
arnold_w17-Oct-17 9:26
arnold_w17-Oct-17 9:26 
GeneralRe: Can I put a variable length array inside a struct somehow? Pin
leon de boer17-Oct-17 17:30
leon de boer17-Oct-17 17:30 
AnswerRe: Can I put a variable length array inside a struct somehow? Pin
jschell17-Oct-17 8:20
jschell17-Oct-17 8: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 9:33
arnold_w17-Oct-17 9:33 
Questioninet error Pin
Member 1346819116-Oct-17 6:22
Member 1346819116-Oct-17 6:22 
GeneralRe: inet error Pin
Richard MacCutchan16-Oct-17 6:35
mveRichard MacCutchan16-Oct-17 6:35 
QuestionMFC Activex Control Pin
RichardK15115-Oct-17 21:53
RichardK15115-Oct-17 21:53 
GeneralRe: MFC Activex Control Pin
Richard MacCutchan16-Oct-17 4:55
mveRichard MacCutchan16-Oct-17 4:55 
GeneralRe: MFC Activex Control Pin
RichardK15116-Oct-17 5:36
RichardK15116-Oct-17 5:36 
GeneralRe: MFC Activex Control Pin
Richard MacCutchan16-Oct-17 6:37
mveRichard MacCutchan16-Oct-17 6:37 
GeneralRe: MFC Activex Control Pin
leon de boer16-Oct-17 21:34
leon de boer16-Oct-17 21:34 
GeneralRe: MFC Activex Control Pin
RichardK15123-Oct-17 0:49
RichardK15123-Oct-17 0:49 
GeneralRe: MFC Activex Control Pin
leon de boer25-Oct-17 10:18
leon de boer25-Oct-17 10:18 
QuestionTo arrange n numbers in descending order ? Pin
Tarun Jha13-Oct-17 10:02
Tarun Jha13-Oct-17 10:02 
AnswerRe: To arrange n numbers in descending order ? Pin
jeron113-Oct-17 10:08
jeron113-Oct-17 10:08 
QuestionRe: To arrange n numbers in descending order ? Pin
David Crow13-Oct-17 17:06
David Crow13-Oct-17 17:06 
AnswerRe: To arrange n numbers in descending order ? Pin
Richard MacCutchan13-Oct-17 23:44
mveRichard MacCutchan13-Oct-17 23:44 
AnswerRe: To arrange n numbers in descending order ? Pin
«_Superman_»15-Oct-17 19:02
professional«_Superman_»15-Oct-17 19:02 
AnswerRe: To arrange n numbers in descending order ? Pin
CPallini16-Oct-17 4:21
mveCPallini16-Oct-17 4:21 
QuestionTo find biggest of n numbers ? Pin
Tarun Jha13-Oct-17 6:44
Tarun Jha13-Oct-17 6:44 

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.