From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

array initialize

Hi,

 

I am helping in troubleshoot a software issue (developed in LabWindows/CVI 2015), and one thing I have noticing and find weird is that the software code initializes the array by assigning an empty bracket or empty string.  For example

 

double Array[10] = {0};

char Buffer[1024] = "";

myMap NameMap = {0, "", "", ""}   //myStructure contains - int, char[1024], char [1024], char [1024]

 

I wonder would that method is truly initialize the array?  or if that could cause memory leak in the software as my assumption is that "" and {} are consider as a constant in the memory, and assigning array to that constant would forces the pointer to change.  And, as the constant does not have sufficient memory allocated, an update to the array would cause memory leak?!

 

Thanks.

 

Peggy

 

0 Kudos
Message 1 of 3
(3,240 Views)

double y [ 10 ] = { 0 }; is a valid and standard way to initialize all elements with zero. You can check this e.g. with 'Variable View" (Shift + F7)

0 Kudos
Message 2 of 3
(3,219 Views)

Hi,

 

partial explicit initializations like double Array[10] = {0} are working in two steps:

  1. Array[0] == 0 because of the explicit zero as the first element of the initialization array {0}
  2. Array[1..9] == 0 because they are not explicitly initialized and thus the rules for objects with static storage apply (in short: use zero)

You can read more here: http://en.cppreference.com/w/c/language/struct_initialization or obviously in the C standard.

-----------------------
/* Nothing past this point should fail if the code is working as intended */
0 Kudos
Message 3 of 3
(3,189 Views)