Variable size of uint8_t array [duplicate]
This question already has an answer here:
C compile error: “Variable-sized object may not be initialized”
8 answers
I am programming a communication protocol over UART with an STM32 board. I need a variable size array of uint8_t values inside the following function:
void sendDataToSlave_UART(UART_HandleTypeDef *huart, uint8_t destinationSlave, uint8_t bytesToSend, uint8_t data)
{
uint8_t masterTxBuffer[bytesToSend+4] = {0};
...
}
I tried the expression above but am getting the error "variable-sized object may not be initialized".
What can I do to fix this issue?
Thank you in advance!
c arrays stm32 cortex-m0+
marked as duplicate by Max Vollmer, Kninnug, Antti Haapala
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 25 '18 at 4:05
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
C compile error: “Variable-sized object may not be initialized”
8 answers
I am programming a communication protocol over UART with an STM32 board. I need a variable size array of uint8_t values inside the following function:
void sendDataToSlave_UART(UART_HandleTypeDef *huart, uint8_t destinationSlave, uint8_t bytesToSend, uint8_t data)
{
uint8_t masterTxBuffer[bytesToSend+4] = {0};
...
}
I tried the expression above but am getting the error "variable-sized object may not be initialized".
What can I do to fix this issue?
Thank you in advance!
c arrays stm32 cortex-m0+
marked as duplicate by Max Vollmer, Kninnug, Antti Haapala
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 25 '18 at 4:05
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
You can dynamically allocate array usingmalloc(sizeof(uint8_t)*bytesToSend+4)
.
– brokenfoot
Nov 24 '18 at 23:47
@brokenfoot Dynamic memory allocation is best avoided on an embedded system.
– duskwuff
Nov 24 '18 at 23:55
@duskwuff I would avoid the use of VLA's too...
– Jose
Nov 24 '18 at 23:56
add a comment |
This question already has an answer here:
C compile error: “Variable-sized object may not be initialized”
8 answers
I am programming a communication protocol over UART with an STM32 board. I need a variable size array of uint8_t values inside the following function:
void sendDataToSlave_UART(UART_HandleTypeDef *huart, uint8_t destinationSlave, uint8_t bytesToSend, uint8_t data)
{
uint8_t masterTxBuffer[bytesToSend+4] = {0};
...
}
I tried the expression above but am getting the error "variable-sized object may not be initialized".
What can I do to fix this issue?
Thank you in advance!
c arrays stm32 cortex-m0+
This question already has an answer here:
C compile error: “Variable-sized object may not be initialized”
8 answers
I am programming a communication protocol over UART with an STM32 board. I need a variable size array of uint8_t values inside the following function:
void sendDataToSlave_UART(UART_HandleTypeDef *huart, uint8_t destinationSlave, uint8_t bytesToSend, uint8_t data)
{
uint8_t masterTxBuffer[bytesToSend+4] = {0};
...
}
I tried the expression above but am getting the error "variable-sized object may not be initialized".
What can I do to fix this issue?
Thank you in advance!
This question already has an answer here:
C compile error: “Variable-sized object may not be initialized”
8 answers
c arrays stm32 cortex-m0+
c arrays stm32 cortex-m0+
asked Nov 24 '18 at 23:45
Pablo Díaz AmoresPablo Díaz Amores
225
225
marked as duplicate by Max Vollmer, Kninnug, Antti Haapala
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 25 '18 at 4:05
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Max Vollmer, Kninnug, Antti Haapala
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 25 '18 at 4:05
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
You can dynamically allocate array usingmalloc(sizeof(uint8_t)*bytesToSend+4)
.
– brokenfoot
Nov 24 '18 at 23:47
@brokenfoot Dynamic memory allocation is best avoided on an embedded system.
– duskwuff
Nov 24 '18 at 23:55
@duskwuff I would avoid the use of VLA's too...
– Jose
Nov 24 '18 at 23:56
add a comment |
1
You can dynamically allocate array usingmalloc(sizeof(uint8_t)*bytesToSend+4)
.
– brokenfoot
Nov 24 '18 at 23:47
@brokenfoot Dynamic memory allocation is best avoided on an embedded system.
– duskwuff
Nov 24 '18 at 23:55
@duskwuff I would avoid the use of VLA's too...
– Jose
Nov 24 '18 at 23:56
1
1
You can dynamically allocate array using
malloc(sizeof(uint8_t)*bytesToSend+4)
.– brokenfoot
Nov 24 '18 at 23:47
You can dynamically allocate array using
malloc(sizeof(uint8_t)*bytesToSend+4)
.– brokenfoot
Nov 24 '18 at 23:47
@brokenfoot Dynamic memory allocation is best avoided on an embedded system.
– duskwuff
Nov 24 '18 at 23:55
@brokenfoot Dynamic memory allocation is best avoided on an embedded system.
– duskwuff
Nov 24 '18 at 23:55
@duskwuff I would avoid the use of VLA's too...
– Jose
Nov 24 '18 at 23:56
@duskwuff I would avoid the use of VLA's too...
– Jose
Nov 24 '18 at 23:56
add a comment |
1 Answer
1
active
oldest
votes
The compiler doesn't know the size of a VLA at compilation time, so you have to initialize it at runtime, something like:
memset(masterTxBuffer, 0, (bytesToSend+4)*sizeof(uint8_t) );
That is what I was looking for. Works perfectly. Thank you!
– Pablo Díaz Amores
Nov 25 '18 at 0:08
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The compiler doesn't know the size of a VLA at compilation time, so you have to initialize it at runtime, something like:
memset(masterTxBuffer, 0, (bytesToSend+4)*sizeof(uint8_t) );
That is what I was looking for. Works perfectly. Thank you!
– Pablo Díaz Amores
Nov 25 '18 at 0:08
add a comment |
The compiler doesn't know the size of a VLA at compilation time, so you have to initialize it at runtime, something like:
memset(masterTxBuffer, 0, (bytesToSend+4)*sizeof(uint8_t) );
That is what I was looking for. Works perfectly. Thank you!
– Pablo Díaz Amores
Nov 25 '18 at 0:08
add a comment |
The compiler doesn't know the size of a VLA at compilation time, so you have to initialize it at runtime, something like:
memset(masterTxBuffer, 0, (bytesToSend+4)*sizeof(uint8_t) );
The compiler doesn't know the size of a VLA at compilation time, so you have to initialize it at runtime, something like:
memset(masterTxBuffer, 0, (bytesToSend+4)*sizeof(uint8_t) );
answered Nov 24 '18 at 23:49
JoseJose
1,241415
1,241415
That is what I was looking for. Works perfectly. Thank you!
– Pablo Díaz Amores
Nov 25 '18 at 0:08
add a comment |
That is what I was looking for. Works perfectly. Thank you!
– Pablo Díaz Amores
Nov 25 '18 at 0:08
That is what I was looking for. Works perfectly. Thank you!
– Pablo Díaz Amores
Nov 25 '18 at 0:08
That is what I was looking for. Works perfectly. Thank you!
– Pablo Díaz Amores
Nov 25 '18 at 0:08
add a comment |
1
You can dynamically allocate array using
malloc(sizeof(uint8_t)*bytesToSend+4)
.– brokenfoot
Nov 24 '18 at 23:47
@brokenfoot Dynamic memory allocation is best avoided on an embedded system.
– duskwuff
Nov 24 '18 at 23:55
@duskwuff I would avoid the use of VLA's too...
– Jose
Nov 24 '18 at 23:56