This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

how to assign the ram continuous without gap?

I define a struct array
struct smp
{ unsigned char attri;
unsigned char data;
} smp[50];
but I find the compiler make a gap between the array. In fact the ram is assigned as
smp[0].attri
smp[0].data
gap[0]
gap[1]
smp[1].attri
smp[1].data
gap[0]
gap[1]
...
so the ram of gap can not be used, then half of the ram is wasted. how can I make the ram of gap to be useful? Can the ram be assign continuous as define a struct array? Can the ram be complied as
smp[0].attri
smp[0].data
smp[1].attri
smp[1].data
smp[2].attri
smp[2].data
...
?

  • Check the alignment and packing controls of your compiler.

  • "half of the ram is wasted"

    As Dan says, the compiler will have options to control this.

    However, the compiler does this because there will be a performance hit (quite probably a serious one) if your data is not correctly aligned.
    So you can save some RAM - but the cost will be reduced performance. You need to carefully consider which is most important to you...

  • Another way to do it if you can deal with not having the strict record or struct approach, is to reorient the data into parallel arrays:

    #define N_SMPS 50
    
    unsigned char smp_attri[N_SMPS];
    unsigned char smp_data[N_SMPS];
    smp_attri[i]
    smp_data[i]

    You are unlikely to waste any memory that way.

  • Godday,

    It seems that you have found a small bug in the CA compiler. The struct size is rounded up to the next multiple of 4, this should not be the case here.

    Until this is fixed, the following workaround avoids wasting memory space:

    #pragma save pack(1)
    struct smp  {
      unsigned char attri;
      unsigned char  data;
    } smp[50];
    #pragma restore
    
    #if 0 // Alternatively, the following is also Ok.:
    __packed struct smp  {
      unsigned char attri;
      unsigned char data;
    } smp[50];
    #endif
    
    void main (void)  {
     //...
    }
    

    Peter

  • Well, so thanks for the replys. My problem always can be solved. The discusser here are so kind and nice.