Variable size of uint8_t array [duplicate]












-1
















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!










share|improve this question













marked as duplicate by Max Vollmer, Kninnug, Antti Haapala c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

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 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
















-1
















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!










share|improve this question













marked as duplicate by Max Vollmer, Kninnug, Antti Haapala c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

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 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














-1












-1








-1









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!










share|improve this question















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+






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

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 c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

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 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














  • 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








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












1 Answer
1






active

oldest

votes


















1














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) );





share|improve this answer
























  • That is what I was looking for. Works perfectly. Thank you!

    – Pablo Díaz Amores
    Nov 25 '18 at 0:08




















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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) );





share|improve this answer
























  • That is what I was looking for. Works perfectly. Thank you!

    – Pablo Díaz Amores
    Nov 25 '18 at 0:08


















1














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) );





share|improve this answer
























  • That is what I was looking for. Works perfectly. Thank you!

    – Pablo Díaz Amores
    Nov 25 '18 at 0:08
















1












1








1







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) );





share|improve this answer













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) );






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 23:49









JoseJose

1,231415




1,231415













  • 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







That is what I was looking for. Works perfectly. Thank you!

– Pablo Díaz Amores
Nov 25 '18 at 0:08







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'