OiO.lk Blog C# How to store a large amount of data in an array or
C#

How to store a large amount of data in an array or


I am trying to generate c-code for a large number of samples to be used in a microcontroller program. Because internal ram is limited I need to force the table into flash memory.
The code below is used to generate a .C source file.

The variables "first" and "last" have low values for the example but will eventually result in hundreds of sample lines.

    FILE *fp = stdout; // fopen("/home/hennep/arrays.c","w+");
    const int first=5, last=8;
    fprintf( fp, "const uint8_t samples[%d][] = { { ", last-first+1 );
    for( int j = first; j <= last; j++ ) {
        if( j != first ) {
            fprintf( fp, "                              , { ");
        }
        for( int i = 0; i < j; i++ ) {
            uint8_t byt = sin( 3.141592654 * 360.0 * i / j / 180.0 ) * 127.5 + 127.5;
            if( i ) {
                fprintf( fp, ", %d", byt );
            } else {
                fprintf( fp, "%d", byt );
            }
        }
        fprintf( fp, " }\n" );
    }
    fprintf( fp, "                              };\n\n" );

Obviously this is not going to work because:

const uint8_t samples[4][] = { { 127, 248, 202, 52, 6 }
                             , { 127, 237, 237, 127, 17, 17 }
                             , { 127, 227, 251, 182, 72, 3, 27 }
                             , { 127, 217, 255, 217, 127, 37, 0, 37 }
                             };

note: declaration of 'samples' as multidimensional array must  
have bounds for all dimensions except the first  

I could rotate the matrix by 90 degrees clockwise, but for readability I prefer not to do that.

const uint8_t samples[][4] = { { 127, 127, 127, 127 }
                             , { 217, 227, 237, 248 }
                             , { 255, 251, 237, 202 }
                             , { 217, 182, 127, 52  }
                             , { 127, 72,  17,  6   }
                             , { 37,  0,   17       }
                             , { 3,   27            }
                             , { 37                 }
                             };

I would normally use a vector to store this but then data will be forced into ram where it doesn’t fit.
I don’t have much experience with large amounts of data, so is there another way to store data like this in a source file?



You need to sign in to view this answers

Exit mobile version