Declare arrays with different sizes in a C typedef struct
I know how to dynamic allocate a new array with malloc
.
I wonder if there's a way to avoid that in this situation:
#define RX_BUFFER_SIZE 256
#define TX_BUFFER_SIZE 128
typedef struct MyBuffer
{
volatile uint8_t RX[RX_BUFFER_SIZE];
volatile uint8_t TX[TX_BUFFER_SIZE];
volatile uint16_t RX_Head;
volatile uint16_t RX_Tail;
volatile uint16_t TX_Head;
volatile uint16_t TX_Tail;
} MyBuffer_t;
typedef struct MyChannel
{
// other stuff
MyBuffer_t buffer;
} MyChannel_t;
then in my code I create several variables like this:
MyChannel_t ch1;
MyChannel_t ch2;
MyChannel_t ch3;
but I would like to set a different sizes of the arrays for each variable. It's ok to select among a small set, i.e.:
#define RX_BUFFER_SIZE_S 32
#define TX_BUFFER_SIZE_S 16
#define RX_BUFFER_SIZE_M 128
#define TX_BUFFER_SIZE_M 64
#define RX_BUFFER_SIZE_L 256
#define TX_BUFFER_SIZE_L 128
Is there a way to achieve this without using malloc
?
c arrays struct typedef
add a comment |
I know how to dynamic allocate a new array with malloc
.
I wonder if there's a way to avoid that in this situation:
#define RX_BUFFER_SIZE 256
#define TX_BUFFER_SIZE 128
typedef struct MyBuffer
{
volatile uint8_t RX[RX_BUFFER_SIZE];
volatile uint8_t TX[TX_BUFFER_SIZE];
volatile uint16_t RX_Head;
volatile uint16_t RX_Tail;
volatile uint16_t TX_Head;
volatile uint16_t TX_Tail;
} MyBuffer_t;
typedef struct MyChannel
{
// other stuff
MyBuffer_t buffer;
} MyChannel_t;
then in my code I create several variables like this:
MyChannel_t ch1;
MyChannel_t ch2;
MyChannel_t ch3;
but I would like to set a different sizes of the arrays for each variable. It's ok to select among a small set, i.e.:
#define RX_BUFFER_SIZE_S 32
#define TX_BUFFER_SIZE_S 16
#define RX_BUFFER_SIZE_M 128
#define TX_BUFFER_SIZE_M 64
#define RX_BUFFER_SIZE_L 256
#define TX_BUFFER_SIZE_L 128
Is there a way to achieve this without using malloc
?
c arrays struct typedef
If you would declare the sizes in each source file, then include the typedef, then you can.
– Paul Ogilvie
Nov 15 '18 at 9:08
I would like to set the sizes for eachMyChannel_t
declared.
– Mark
Nov 15 '18 at 9:09
You can, if you declare them in different source files as per my suggestion. Otherwise, it is not possible.
– Paul Ogilvie
Nov 15 '18 at 9:15
Sidenote: YourMyBuffer_t
should actually be namedMy2Buffers_t
. I would recommend having only single buffer, head and tail in the structure, and then create distinct RX and TX instances that same type.
– user694733
Nov 15 '18 at 10:35
add a comment |
I know how to dynamic allocate a new array with malloc
.
I wonder if there's a way to avoid that in this situation:
#define RX_BUFFER_SIZE 256
#define TX_BUFFER_SIZE 128
typedef struct MyBuffer
{
volatile uint8_t RX[RX_BUFFER_SIZE];
volatile uint8_t TX[TX_BUFFER_SIZE];
volatile uint16_t RX_Head;
volatile uint16_t RX_Tail;
volatile uint16_t TX_Head;
volatile uint16_t TX_Tail;
} MyBuffer_t;
typedef struct MyChannel
{
// other stuff
MyBuffer_t buffer;
} MyChannel_t;
then in my code I create several variables like this:
MyChannel_t ch1;
MyChannel_t ch2;
MyChannel_t ch3;
but I would like to set a different sizes of the arrays for each variable. It's ok to select among a small set, i.e.:
#define RX_BUFFER_SIZE_S 32
#define TX_BUFFER_SIZE_S 16
#define RX_BUFFER_SIZE_M 128
#define TX_BUFFER_SIZE_M 64
#define RX_BUFFER_SIZE_L 256
#define TX_BUFFER_SIZE_L 128
Is there a way to achieve this without using malloc
?
c arrays struct typedef
I know how to dynamic allocate a new array with malloc
.
I wonder if there's a way to avoid that in this situation:
#define RX_BUFFER_SIZE 256
#define TX_BUFFER_SIZE 128
typedef struct MyBuffer
{
volatile uint8_t RX[RX_BUFFER_SIZE];
volatile uint8_t TX[TX_BUFFER_SIZE];
volatile uint16_t RX_Head;
volatile uint16_t RX_Tail;
volatile uint16_t TX_Head;
volatile uint16_t TX_Tail;
} MyBuffer_t;
typedef struct MyChannel
{
// other stuff
MyBuffer_t buffer;
} MyChannel_t;
then in my code I create several variables like this:
MyChannel_t ch1;
MyChannel_t ch2;
MyChannel_t ch3;
but I would like to set a different sizes of the arrays for each variable. It's ok to select among a small set, i.e.:
#define RX_BUFFER_SIZE_S 32
#define TX_BUFFER_SIZE_S 16
#define RX_BUFFER_SIZE_M 128
#define TX_BUFFER_SIZE_M 64
#define RX_BUFFER_SIZE_L 256
#define TX_BUFFER_SIZE_L 128
Is there a way to achieve this without using malloc
?
c arrays struct typedef
c arrays struct typedef
asked Nov 15 '18 at 9:03
MarkMark
1,09821437
1,09821437
If you would declare the sizes in each source file, then include the typedef, then you can.
– Paul Ogilvie
Nov 15 '18 at 9:08
I would like to set the sizes for eachMyChannel_t
declared.
– Mark
Nov 15 '18 at 9:09
You can, if you declare them in different source files as per my suggestion. Otherwise, it is not possible.
– Paul Ogilvie
Nov 15 '18 at 9:15
Sidenote: YourMyBuffer_t
should actually be namedMy2Buffers_t
. I would recommend having only single buffer, head and tail in the structure, and then create distinct RX and TX instances that same type.
– user694733
Nov 15 '18 at 10:35
add a comment |
If you would declare the sizes in each source file, then include the typedef, then you can.
– Paul Ogilvie
Nov 15 '18 at 9:08
I would like to set the sizes for eachMyChannel_t
declared.
– Mark
Nov 15 '18 at 9:09
You can, if you declare them in different source files as per my suggestion. Otherwise, it is not possible.
– Paul Ogilvie
Nov 15 '18 at 9:15
Sidenote: YourMyBuffer_t
should actually be namedMy2Buffers_t
. I would recommend having only single buffer, head and tail in the structure, and then create distinct RX and TX instances that same type.
– user694733
Nov 15 '18 at 10:35
If you would declare the sizes in each source file, then include the typedef, then you can.
– Paul Ogilvie
Nov 15 '18 at 9:08
If you would declare the sizes in each source file, then include the typedef, then you can.
– Paul Ogilvie
Nov 15 '18 at 9:08
I would like to set the sizes for each
MyChannel_t
declared.– Mark
Nov 15 '18 at 9:09
I would like to set the sizes for each
MyChannel_t
declared.– Mark
Nov 15 '18 at 9:09
You can, if you declare them in different source files as per my suggestion. Otherwise, it is not possible.
– Paul Ogilvie
Nov 15 '18 at 9:15
You can, if you declare them in different source files as per my suggestion. Otherwise, it is not possible.
– Paul Ogilvie
Nov 15 '18 at 9:15
Sidenote: Your
MyBuffer_t
should actually be named My2Buffers_t
. I would recommend having only single buffer, head and tail in the structure, and then create distinct RX and TX instances that same type.– user694733
Nov 15 '18 at 10:35
Sidenote: Your
MyBuffer_t
should actually be named My2Buffers_t
. I would recommend having only single buffer, head and tail in the structure, and then create distinct RX and TX instances that same type.– user694733
Nov 15 '18 at 10:35
add a comment |
1 Answer
1
active
oldest
votes
Declare your buffers as pointers:
typedef struct MyBuffer
{
volatile uint8_t * RX;
volatile uint8_t * TX;
size_t rxSize;
size_t txSize;
volatile uint16_t RX_Head;
...
And then use separate static allocation for the buffers, and use them to initialize your object.
volatile uint8_t ch1_rx_buffer[RX_BUFFER_SIZE_S];
volatile uint8_t ch1_tx_buffer[TX_BUFFER_SIZE_S];
MyChannel_t ch1 = {
.buffer = {
ch1_rx_buffer,
ch1_tx_buffer,
sizeof ch1_rx_buffer,
sizeof ch1_tx_buffer,
...
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315755%2fdeclare-arrays-with-different-sizes-in-a-c-typedef-struct%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Declare your buffers as pointers:
typedef struct MyBuffer
{
volatile uint8_t * RX;
volatile uint8_t * TX;
size_t rxSize;
size_t txSize;
volatile uint16_t RX_Head;
...
And then use separate static allocation for the buffers, and use them to initialize your object.
volatile uint8_t ch1_rx_buffer[RX_BUFFER_SIZE_S];
volatile uint8_t ch1_tx_buffer[TX_BUFFER_SIZE_S];
MyChannel_t ch1 = {
.buffer = {
ch1_rx_buffer,
ch1_tx_buffer,
sizeof ch1_rx_buffer,
sizeof ch1_tx_buffer,
...
}
}
add a comment |
Declare your buffers as pointers:
typedef struct MyBuffer
{
volatile uint8_t * RX;
volatile uint8_t * TX;
size_t rxSize;
size_t txSize;
volatile uint16_t RX_Head;
...
And then use separate static allocation for the buffers, and use them to initialize your object.
volatile uint8_t ch1_rx_buffer[RX_BUFFER_SIZE_S];
volatile uint8_t ch1_tx_buffer[TX_BUFFER_SIZE_S];
MyChannel_t ch1 = {
.buffer = {
ch1_rx_buffer,
ch1_tx_buffer,
sizeof ch1_rx_buffer,
sizeof ch1_tx_buffer,
...
}
}
add a comment |
Declare your buffers as pointers:
typedef struct MyBuffer
{
volatile uint8_t * RX;
volatile uint8_t * TX;
size_t rxSize;
size_t txSize;
volatile uint16_t RX_Head;
...
And then use separate static allocation for the buffers, and use them to initialize your object.
volatile uint8_t ch1_rx_buffer[RX_BUFFER_SIZE_S];
volatile uint8_t ch1_tx_buffer[TX_BUFFER_SIZE_S];
MyChannel_t ch1 = {
.buffer = {
ch1_rx_buffer,
ch1_tx_buffer,
sizeof ch1_rx_buffer,
sizeof ch1_tx_buffer,
...
}
}
Declare your buffers as pointers:
typedef struct MyBuffer
{
volatile uint8_t * RX;
volatile uint8_t * TX;
size_t rxSize;
size_t txSize;
volatile uint16_t RX_Head;
...
And then use separate static allocation for the buffers, and use them to initialize your object.
volatile uint8_t ch1_rx_buffer[RX_BUFFER_SIZE_S];
volatile uint8_t ch1_tx_buffer[TX_BUFFER_SIZE_S];
MyChannel_t ch1 = {
.buffer = {
ch1_rx_buffer,
ch1_tx_buffer,
sizeof ch1_rx_buffer,
sizeof ch1_tx_buffer,
...
}
}
edited Nov 15 '18 at 9:16
answered Nov 15 '18 at 9:11
user694733user694733
11.1k12850
11.1k12850
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315755%2fdeclare-arrays-with-different-sizes-in-a-c-typedef-struct%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If you would declare the sizes in each source file, then include the typedef, then you can.
– Paul Ogilvie
Nov 15 '18 at 9:08
I would like to set the sizes for each
MyChannel_t
declared.– Mark
Nov 15 '18 at 9:09
You can, if you declare them in different source files as per my suggestion. Otherwise, it is not possible.
– Paul Ogilvie
Nov 15 '18 at 9:15
Sidenote: Your
MyBuffer_t
should actually be namedMy2Buffers_t
. I would recommend having only single buffer, head and tail in the structure, and then create distinct RX and TX instances that same type.– user694733
Nov 15 '18 at 10:35