Large Amounts of Static Data

This is a discussion on Large Amounts of Static Data within the C Programming forums, part of the General Programming Boards category; Hi, first post here. I'm writing a program for a microcontroller that must load parameters into several peripheral devices over ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    3

    Large Amounts of Static Data

    Hi, first post here.
    I'm writing a program for a microcontroller that must load parameters into several peripheral devices over I2C. The parameters are constant and only used as part of the initialization sequence for the device. The amount of data is on the order of thousands of bytes.

    My problem is I don't know the best way of loading in a large number of constants into a c program.

    I tried just declaring an array

    Code:
    ParameterArray[1024] = {byte0, byte1,....byte1023};
    but once I get about 256 bytes in the array I get a stack overflow error.

    I could break the data up into several smaller arrays but that seems really inefficient, especially if I want to change the data.

    I also tried using malloc(), but I can't figure out how to initialize the data without entering every byte separately.

    Since this is in a micro I'd like all the data and program to be in one file. i.e. I don't want to read from a separate data file.

    If it helps I'm using a Renesas 8C/25 micro with 32K of ROM and 2K RAM.

    I appreciate any help.

    SK

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    When you declared your ParameterArray, was that local to a function or global? Try making it global if you didn't (bad idea generally, but oh well).

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    3
    Hey, that worked!
    Not only did it work, the compiler seems to have placed the array in the ROM instead of RAM.
    I was afraid that it would put the globals in RAM and use it all up, but I have plenty of ROM.

    Thanks!

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    You answered your own question. Just use the keyword "static" when you declare the array. Or make it a global. I'd prefer the former.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you want the array to be placed in ROM permanently [as in, it's a constant set of data that never changes], then you should do this:
    Code:
    static const ParameterArray[1024] = {byte0, byte1,....byte1023};
    That makes the variable local to the function that uses it [which I presume is what you actually want], but it also makes it a constant and have all the benefits of a global variable, in the sense that it's not taking up stackspace and not initialized every time the function is called.

    The static means that it's "not automatic", so it's not on the stack, but permanently placed (like a global variable). But it's also hidden from other functions by being inside the function.

    The const means "there is no intention to change the values in this array".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    3
    Yes, that seems to work as well. I never realized "static const" would actually changed the type of memory allocated .

    I appreciate everyone's replies.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Static & Data Member
    By ecoliteracy in forum C++ Programming
    Replies: 1
    Last Post: 04-16-2007, 08:46 PM
  3. Handling Large Amounts of Data
    By kas2002 in forum C++ Programming
    Replies: 9
    Last Post: 12-21-2006, 03:52 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. accepting large amounts of data
    By Sekti in forum C++ Programming
    Replies: 1
    Last Post: 04-05-2002, 04:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21