DPRG List  

[DPRG] ANSI C question

Subject: [DPRG] ANSI C question
From: Chuck McManis cmcmanis at mcmanis.com
Date: Fri Oct 29 20:09:59 CDT 2004

At 05:08 PM 10/29/2004, Rick J. Bickle wrote:
>Yes, although you would need text[3][5] in your example.
>If I declare the array as originally stated:
>Char *text [2][5];
>text[0][0] = "Msg 0,0asd";
>text[0][1] = "Msg 0,1   ";
>text[0][2] = "Msg 0,2   ";
>text[0][3] = "Msg 0,3   ";
>text[0][4] = "Msg 0,4   ";
>text[0][5] = "Msg 0,5   ";
>text[1][0] = "Msg 1,0   ";
>text[1][1] = "Msg 1,1   ";
>text[1][2] = "Msg 1,2   ";
>text[1][3] = "Msg 1,3   ";
>text[1][4] = "Msg 1,4   ";
>text[1][5] = "Msg 1,5   ";
>Is it not true that the compiler will only allocate memory space for
>5x2=10 pointers instead of 10 strings?

The above code allocates 11 bytes per string (110 bytes) and either 40 or 
20 bytes worth of pointer space (depending on if you use 16 bit pointers or 
32 bit pointers) so call it 150 bytes worse case.

>With this declaration, how is the compiler to know the length of each

It doesn't it counts the characters between the quotes and adds a zero.

>I suspect that this will leak memory, although the compiler generates no
>warnings or errors.

Actually it doesn't. Memory "leakage" occurs when you dynamically allocate 
memory using the malloc() call and then never call free(). (Another way to 
leak memory is to recursively call some function and never return, but that 
is usually caught early in the debug cycle).

Your code merely consumes 150 bytes at load time.

In a small system implementation this means part of the "data" segment 
(pre-initialized data) and that can consume precious EEPROM or Flash space.

>Alternatively, if I declare the array as:
>Char *text [2][5] = {
>         {
>         "abcd",
>         "efgh",
>         "ijkl",
>         "mnop",
>         "qrst"
>         }
>         {
>         "abcd",
>         "efgh",
>         "ijkl",
>         "mnop",
>         "qrst"  }
>         };
>Then the compiler should be able to determine the size of each string
>and allocate memory correctly. However, are we creating an array of
>pointers to strings, or an array of strings themselves?

This is still an array of pointers to strings and you've got 10 strings there.

Now what you CANNOT do, and this might be your root question, is go back 
and re-use the pointer in the array to put a new string there.

So for example with the above code, if you tried
         *(text[0][0]) = 'F'; /* store F in first string */

It would probably fail because the pointer in that array is probably 
pointing at FLASH not at RAM.

If on the other hand if you had declared it

char text[3][5][5];

Then you have reserved 5 characters in RAM for each of the strings but not 
initialized them. Referring to &(text[0][0][0]) will return a char * 
pointer to the first string, &(text[0][1][0]) the second, etc.

>In a stack of 5 C books, I cannot find a satisfactory answer.

Is one of them Harbison and Steele?


More information about the DPRG mailing list