Having a large array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pram29
    New Member
    • Sep 2007
    • 6

    Having a large array

    Hello,

    I want to have a large array. I have a structure which contains 6 fields, 4 of them are unsigned char and 2 of them are unsigned short. I should have an array of 80 elements of type the above said structure. I would like to have it as a static array in a class, which I can refer to, from any function inside this class. I am programming this for an advanced audio amplifier (Embedded system), running on PowerPC and OSEK RTOS. compiler is diab. When I declared the array with 60 elements, it worked fine, but when I increased it to 80 elements, the target system is not booting because of insuffiecient RAM. Is there any way in C++ so that I can make it work? (I have declared the array as const, otherwise the code wont compile)

    Thanks,

    Regards,
    Ramu
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    To star5t with I would not normally call an array of 640 bytes big. However since this is an embedded system (rather than a PC with and excess of resources) it rather depends on how much on board RAM you have.

    If you have run out of RAM then there is nothing C++ can do, it can not magically make your RAM able to contain more data. You have some options
    1. Look for inefficient use of RAM and eliminate this. This could be arrays that are longer than need be (in in 1 case I worked on 2 60kbyte arrays that just weren't used) or making sure you structures and classes are defined so they pack the data efficiently into RAM with little wasted space (I save about 8kbytes once just re-ordering the elements in a structure used in an array of 1800 elements).
    2. Look for potions of the program that use data temporarily, see if you can rework the program to let them share data with another portion of the program or use the heap.
    3. Check the size of your stack and heap see if they could be smaller. Unfortunately C++ tends to require a bigger heap than C in this respect.

    Comment

    • pram29
      New Member
      • Sep 2007
      • 6

      #3
      Thank you Banfa.. Will have a check according to your suggestion.
      Meanwhile, I would like to ask if there is a different way in C++ to program this scenario?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by pram29
        Thank you Banfa.. Will have a check according to your suggestion.
        Meanwhile, I would like to ask if there is a different way in C++ to program this scenario?
        If you have that little RAM left (< 80*8 bytes) I wouldn't program in C++ at all; maybe C can cram it in such little RAM but I'm afraid you have to resort to assembly language.

        kind regards,

        Jos

        Comment

        • pram29
          New Member
          • Sep 2007
          • 6

          #5
          Unfortunately, I have only 80K RAM and in that 16K is untouchable by this application. There are other data types and resources taking lot of memory and I am left with only few KBs which comes less than 80*8 Bytes.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by pram29
            Unfortunately, I have only 80K RAM and in that 16K is untouchable by this application. There are other data types and resources taking lot of memory and I am left with only few KBs which comes less than 80*8 Bytes.
            You are out of luck then: it can't be done the way you want to do it. Read Banfa's tips again and see if they work out for you. Good luck.

            kind regards,

            Jos

            Comment

            • pram29
              New Member
              • Sep 2007
              • 6

              #7
              ok,
              Thank you JosAH and Bafna

              Regards,
              Ramu

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                Do you get the out-of-ram message at run-time or at build-time?

                The system (or linker) decide you've run of RAM by comparing actual RAM usage against constants that indicate the amount of RAM available. You should check that those constants are accurate. If you're lucky, they're set too low -- meaning there is some RAM available that nobody can use. If so, adjust the constants and try again.

                Another technique for saving RAM is change static variables to automatic variables. Of course, sometimes the variables must be static in order for the program to work.

                Comment

                • pram29
                  New Member
                  • Sep 2007
                  • 6

                  #9
                  Hi donbock,

                  I get linker error message that svram is not available (or something else which means that Linker cant link it as it is too large)at build time if I declare the array as just static. So I declared it as static const. With this change, I dont get any linker error message, but the application itself doesnot boot during runtime.
                  If I use it as auto, then I have to declare it globally, then also I will face the same problem. If I declare it inside the class, then for every object of the class, it would create a new array which would be more disastrous (Correct me if I am wrong)
                  Temporarily I thought of following Banfa's suggestion and I cut down the fields in the structure and I declared a different structure and array for those few elements of array which use all the 6 fields of the structure (For some of the elements of the structure, the value of some of fields would be 0).
                  This array is used as a look up table also, array of callbacks, so I cannot skip without this array.
                  Btw, Can you please elaborate which will be those constants I have to look into, so that I can extend it? When I checked MPC 5166's spec, it says, it has 80K memory, but for me only 64K is available, and now, for this resource, I am left with not more than 6.4K!! :(

                  Thank you for the kind response,
                  Ramu

                  Comment

                  • donbock
                    Recognized Expert Top Contributor
                    • Mar 2008
                    • 2427

                    #10
                    Originally posted by pram29
                    I get linker error message that svram is not available (or something else which means that Linker cant link it as it is too large)at build time if I declare the array as just static.
                    ...
                    Can you please elaborate which will be those constants I have to look into, so that I can extend it? When I checked MPC 5166's spec, it says, it has 80K memory, but for me only 64K is available, and now, for this resource, I am left with not more than 6.4K!! :(
                    Somehow the linker knows how much ram is available. Typically, it is informed of this through command-line switches when it is invoked. I don't know what linker you're using. I suggest you review the manual for your linker. The command line switches may be set in your makefile; they might be in a properties page in your IDE; they might be buried in the batch/script you use to run the linker, etc.

                    Comment

                    • pram29
                      New Member
                      • Sep 2007
                      • 6

                      #11
                      Ok, Thank you donbock,
                      Will make a check on that.
                      BTW, I am using Windriver diab compiler and linker. command is dld for linking

                      Regards,
                      Ramu

                      Comment

                      Working...