Domanda

need your help in three questions (which regard more or less to the same subject I guess).

1) I have a LARGE array of int's which is initialized in the following manner:

int arr [] = {.....}; // allot of values !!

within the program there is only one function that "uses" this array for "read only" operations. We have two options regarding this array:

a) Declare that array as a local array in the that function.

b) Declare it as a global array outside of this function.

How will the image file of the program will be modified for both these cases?

How will the program speed of execution will be modified ?

2) Regarding the TI MSP430 micro controller:

I have in my program a very large global array of C style string as follows:

char *arr [] = {"string 1","string2",.......}; // allot of strings

Usually, at the beginning of the main program I use a command to stop the "Watch Dog" timer. As I see it , it is needed , for instance, to cases where there is , for example, a very large array that needs to be initialized ....so my question is:

Does it the case? (having the large array of "strings" ) ? When does the array gets initialized?

Will it matter if I declare it in a different manner?

3) How (if so) the answer to questions 1 & 2 will be different in C++?

Thanks allot , Guy.

È stato utile?

Soluzione

"How will the image file of the program be modified for each of these cases?":

  1. If you declare it as a local variable, then the total size of your executable will remain the same, but every time you call the function, a large amount of data-copy operations will take place before the rest of the function code is executed.

  2. If you declare it as a global variable, then the total size of your executable will increase, but there will be no additional data-copy operations during runtime, as the image values will be hard-coded into the executable itself (the executable loading-time will increase, if that makes any difference).

So option #1 is better in terms of size, and option #2 is better in terms of performance.

HOWEVER, please note that in the first option, you will most likely have a stack-overflow during runtime, which will cause your program to perform a memory access violation and crash. In order to avoid it, you will have to increase the size of your stack, typically defined in your project's linker-command file (*.lcf). Increasing the size of the stack means increasing the size of the executable, hence option #1 is no better than option #2 in any aspect, leaving you with only one choice to take (declaring it as a global variable).

Another issue, is that you might wanna declare this array as const, for two reasons:

  1. It will prevent runtime errors (and give you compilation errors instead), should you ever attempt to change values within this read-only array.

  2. It will tell the linker to allocate this array in the RO section of your program, which is possibly mapped to an EPROM on the MSP430. If you choose not to use const, then the linker will allocate this array in the RW section of your program, which is probably mapped to the RAM. So this consideration is really a matter of - which memory you're shorter of, RAM or EPROM. If you're not sure, then you can check it in your project's linker-command file, or in the map file that is generated every time you build the project.

"When does the global array get initialized?":

It is initialized during compilation, and the actual values are hard-coded into the executable.

So there is no running-time involved here, and there is something else causing your watch-dog to perform a HW reset (my guess - some memory access violation which causes your program to crash).

NOTE:

An executable program is typically divided into three sections:

  • Code (read-only) section, which contains the code and all the constant variables.

  • Data (read-write) section, which contains all the non-constant global/static variables.

  • Stack (read-write) section, where all the other variables are allocated during runtime.

The size and base-address of each section, can be configured in the linker-settings of the project (or in the linker-command file of the project).

Altri suggerimenti

You have a third option; declare it within the function, but with the static keyword:

void func()
{
  static int arr[] = {...};
  ...
 }

This will set aside storage in a different memory segment (depends on the architecture and the executable file format; for ELF, it will use the .data segment), which will be initialized at program startup and held until the program terminates.

Advantages:

  • The array is allocated and initialzed once, at program startup, rather than every time you enter the function;
  • The array name is still local to the function, so it's not visible to the rest of the program;
  • The array size can be quite a bit larger than if allocated on the stack;

Disadvantages:

  • If the array is not truly read-only, but is updated by the function, then the function is no longer re-entrant;

Note that if the array is truly meant to be read-only, you might want to declare it

static const int arr[] = {...}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top