0

I read a sample code from a development kits. The global array is declared without indicating the array size. I wonder is it safe?

Here is the code:

#include <stdio.h>                                           // For Used Function printf 

// UART Buffer
char uart0_buf[];                                           // "sprint" UART[0] Buffer
char uart2_buf[];                                           // "sprint" UART[2] Buffer
char uart3_buf[];                                           // "sprint" UART[3] Buffer


int main()
{
  // UART[0] Print String //
  sprintf(uart0_buf,"Testing1234 - Uart 0\n\r");    // Print Message String 

  // UART[2] Print String //
  sprintf(uart2_buf,"Testing1234 - Uart 2\n\r");    // Print Message String 

  // UART[3] Print String //
  sprintf(uart3_buf,"Testing1234 - Uart 3\n\r");    // Print Message String 
}
2
Contributors
3
Replies
24
Views
3 Years
Discussion Span
Last Post by deceptikon
0

I wonder is it safe?

As written it won't compile, so if you want to get technical it's perfectly safe as it'll never run. However, I suspect that you're paraphrasing. If definitions of those arrays are provided somewhere else then the code will compile and run.

Assuming a more complete example, it isn't necessarily safe because there are no provisions to avoid overflowing the arrays with a string that's too long. This is a classic problem with sprintf(), and the usual answer is snprintf(). However, snprintf() isn't a standard function prior to the C99 standard, so your compiler may not support it.

0

The problem is it is able to compile. I'm design for embedded the toolchain is Code Red MCU Tools (NXP). I searched every files and can't find any definitions at other files.

Nevermind. I try email NXP. May be their compiler support this feature.

0

Nevermind. I try email NXP. May be their compiler support this feature.

That seems best. If there are no definitions anywhere then you may be dealing with a compiler extension where an empty array size defaults to something.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.