Multidimensional array with 1 element in all dimensions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alexis4
    New Member
    • Dec 2009
    • 113

    #1

    Multidimensional array with 1 element in all dimensions

    The question may look funny, but are the following declarations equivalent?

    unsigned char a;
    unsigned char b[1];
    unsigned char c[1][1][1];

    My compiler stores one byte for a, one for b and one for c. So is it OK to declare an array like line 3 or is this a catch somewhere? For example will c[0][0][0] be processed slower than a?

    Thanks.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Thos three definitions are different otherwise they would look the same. A table is not a cow just because they both have four legs. Therefore, the size of the variable is irrelevant.

    Read this: http://bytes.com/topic/c/insights/77...rrays-revealed

    Comment

    • alexis4
      New Member
      • Dec 2009
      • 113

      #3
      That's a very good article, congratulations by the way. I have read it a long time ago but still it does not include an answer for my question. I agree about the comparison between cow and table, that's why I asked. Since they all store 1 byte, what's the difference?

      I wrote this piece of code below:

      Code:
      a = 1;
      b[0]=2;
      c[0][0][0][0][0]=0;

      The disassembly window is this:

      Code:
        a = 1;
          004BF8   E001             LDI     R16,0x01
          004BFA   9300 031A        STS     a,R16
       
       b[0]=2;
          004BFE   E002             LDI     R16,0x02
          004C00   9300 031B        STS     b,R16
      
       c[0][0][0][0][0]=0;
          004C04   E000             LDI     R16,0x00
          004C06   9300 031C        STS     c,R16

      The generated assembly code is identical for all of them. It seems to be the same after all(?).

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Look at it this way:

        Code:
        a = 1;
        stores a char becuase a is a char. Ergo: 1 byte

        Code:
         b[0]=2;
        stores a char because array elements of b are char. Ergo: 1 byte.

        Code:
          c[0][0][0][0][0]=0;
        stores a char becuse elements of c are char. Ergo: 1 byte.

        All you really have here are three unsigned char variables with names of a, b[0] and c[0][0][0][0][0].
        Last edited by weaknessforcats; Aug 26 '11, 04:59 PM. Reason: typo

        Comment

        • alexis4
          New Member
          • Dec 2009
          • 113

          #5
          Yes weaknessforcats , I thought so too.

          The basic idea is to have some string messages (embedded C) and I have some routines displaying the messages on LCD. But for multilanguage support I think I will use something like:

          Code:
          #define _LANGUAGES 2
          
          #define _GREEK   0
          #define _ENGLISH 1
          
          uint8_t currentLanguage = _ENGLISH;
          uint8_t message[_LANGUAGES][7] = {{"GREEK  "}, {"ENGLISH"}};
          uint8 *p;
          p = &message[currentLanguage][0];
          lcdPrint(p);
          But in the case that the defined language is just one, then I would have an array like:

          Code:
          uint8_t message[_LANGUAGES][7] = {"GREEK  "};
          But since _LANGUAGES=1, I would then had an array like:
          Code:
          uint8_t message[1][7] = {"GREEK  "};

          My main concern in that case, is to have a code as effective as one dimension array with 7 elements. The routines are ready and it would be very convenient to go like this, but I will not give priority to development time against code performance, so I wanted to be sure about it.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            You might consider building for each supported language.

            Your code would be:

            Code:
            #include "languagedef.h"
            printf("%s",GREETING);
            where languagedef.h is something like:

            Code:
            #ifdef ENGLISH
            #define GREETING "Hello"
            #endif
            #ifdef GERMAN
            #define GREETING "Guten tag"
            #endif
            #ifdef SPANISH
            #define GREETING "Buenos dias"
            #endif

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              So you just compile using the correct language build flag and off you go.

              You no not ever want any language specific code inside your application.

              Comment

              • alexis4
                New Member
                • Dec 2009
                • 113

                #8
                Thanks weaknessforcats , that's exactly what I'm doing when only one language is needed and defined during compile time. But if I want the language to be changed during runtime by user, then I thought to go like I mentioned. If you have something better in mind, please feel free to suggest.

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  Here I don't know what kind of reources you have but you can implement a resource table that you access. Using GREETING plus a language code, an interface function returns the correct phrase.

                  Microsoft does not do this with Windows. Too much overhead.

                  Instead, what they do is build a database where GREETING is translated into various languages (enter Unicode) using the correct glyphs. Then they build a resource table for a specific language. This resource table ships with the product. When he product needs GREETINGS, access code in the product uses what is in the resource table. All the product knws how to do is call a function that acesses the table and returns a Unicode string which is then displayed by functions that know how to display Unicode.

                  You change languages on the fly by switching to a different resource table.

                  Comment

                  • alexis4
                    New Member
                    • Dec 2009
                    • 113

                    #10
                    Here I don't know what kind of reources you have but you can implement a resource table that you access. Using GREETING plus a language code, an interface function returns the correct phrase.
                    Well that makes perfect sence. Let's see if a similar solution can be implemented at a lower level of embedded programming.

                    Thank you weaknessforcats for your support!

                    Comment

                    • HUSTer
                      New Member
                      • Aug 2011
                      • 2

                      #11
                      see the code below:
                      Code:
                      #include <time.h>
                      #include <iostream>
                      #define TEST_TIMES 1000000000
                      
                      using namespace std;
                      
                      int main()
                      {
                      	unsigned char a = 'c';
                      	unsigned char b[1] = {'c'};
                      	unsigned char c[1][1][1] = {'c'};;
                      	unsigned char temp;
                      	clock_t ct = clock();
                      	for(int i = 0;i < TEST_TIMES;++i)
                      		temp = a;
                      	cout<<clock() - ct<<endl;
                      
                      	ct = clock();
                      	for(int i = 0;i < TEST_TIMES;++i)
                      		temp = b[0];
                      	cout<<clock() - ct<<endl;
                      
                      	ct = clock();
                      	for(int i = 0;i < TEST_TIMES;++i)
                      		temp = c[0][0][0];
                      	cout<<clock() - ct<<endl;
                      }
                      result:
                      2484
                      2391
                      2531

                      or

                      2422
                      2375
                      2343
                      Last edited by HUSTer; Aug 29 '11, 03:24 AM. Reason: there is something wrong with my answer!

                      Comment

                      • alexis4
                        New Member
                        • Dec 2009
                        • 113

                        #12
                        Hi HUSTer!

                        Thank you too for your answer.

                        I don't think that your code proves something. There is a balance point (at about 2400) and the results vary above or below it randomly.

                        You could understand that, if you observe the results:

                        At results 1 the bigger time is for c while the smallest time is for b! At results 2 the bigger time is for a!!! The results don't follow an algorithm that makes sence.

                        Thanks for your answer though!

                        Comment

                        Working...