Thread: Variable memory allocation?

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    21

    Variable memory allocation?

    I implemented a circular buffer using variables already written in a program. All I needed to use was ONE new bool variable. This code is so damn full I have no more memory for ONE more bool variable on my chip! Any ideas about how to get around this? I will (of course) be looking for ways to get rid of variables being used, and to use code already written to replace my bool variable, but past programmers (much more experienced than myself) have already went through this code thoroughly trying to reduce its size.
    ***Is there type of variable or process I am missing which us used for situations like this? A way to use RAM for this particular part of the program, or replace another variable I know will not be used when this portion of code is executed, or anything simple I might be missing? Thanks for the help all you experts : )

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You really ought to provide us with more info. You know, useful stuff like your code. Then we can actually help you eliminate crap you don't need. There are lots of things that can be done. Without your code though, I can only guess that you're using all (or at least a lot of) global variables. Globals get programmed on the chip like the code itself. In general, they are frowned upon (read Global Variables Are Bad), so try moving them into functions. Local variables live on the stack (in RAM) and don't take up space on the chip*.

    * As long as you don't use the 'static' keyword that is, which you rarely need to do.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You can use a union for things which you know will be mutually exclusive.

    How is your circular buffer implemented at the moment? Is it just an array with two indices, or a circular double linked list?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    21
    anduril, not sure what part of the code you want since there are 20+ thousand lines. All I have for variables is an array, 2 indexes and a bool which switches the head and tail of the circular buffer based upon where the loading value of the array is. I need the buffer in case the code loads the characters much faster than rs_232 transmits them. Salem, thanks I will look into a union, I will look into this a bit longer before I send the code. I will at least implement the watchdog, enable/disable so the rest makes sense to everyone. I will look into your comments anduril, thanks again.

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    21
    Actually I do have one more question though. I have to define a global if the two sections of code below are defined in two different header files correct? If I try to define a bool anywhere besides the header file Var and initialize it in the header file init, the second portion of code does not find the variable.

    Code:
    //DANS CORCULAR BUFFER MODIFICATION
    // Called by printf()
    /*
    int putchar( u8 ch )
    {
            // Disable UART2 TX interrupt while manipulating buffer to avoid inconsistency 
            UART2_ITConfig( (UART2_IT_TXE | UART2_IT_TC), DISABLE ); 
            if(flag_rs232_catch == 1)              /*This impliments start of circular buffer, if data store becomes larger than array, store index
                                                        transmits and transmit index stores*/
                {
                uart2_tx[uart2_tx_size++] = ch;
                flag_rs232_catch = 1;
                uart2_tx_index = 0;
                UART2_ITConfig( (UART2_IT_TXE | UART2_IT_TC), ENABLE );  
                UART2->DR = uart2_tx[uart2_tx_index]; //UART2_SendData8(uart2_tx[uart2_tx_index++]);
                
                    if(uart2_tx_size == 63)               //If head reaches end of array, it loops to become tail
                        flag_rs232_catch = 0;
                }
            else if(flag_rs232_catch = 0)
                {
            uart2_tx[uart2_tx_index] = ch;  
            uart2_tx_index++;
            UART2_ITConfig( (UART2_IT_TXE | UART2_IT_TC), ENABLE ); 
                } 
            
        else
            ch = 0;                                   //***Jacob, is this the returning stop bit???***
        return ch;
    }
    */
    //END OF DANS CIRCULAR BUFFER MODIFICATION FOR COMMAND, BELOW IS FORMER CODE BY JACOB QUANT
    
    
    //***COMMENT FOR C BOARD*** CALLED BY 2ND HEADER FILE
    #endif /* _COSMIC_ */
    {
        if( UART2_GetFlagStatus(UART2_FLAG_TXE) == SET )
        { 
                if(flag_rs232_catch == 0)
                    {
                        UART2->DR = uart2_tx[uart2_tx_size]; //UART2_SendData8(uart2_tx[uart2_tx_index++]);
                        uart2_tx_size++;
                    }
            else
            {
                UART2_ITConfig( UART2_IT_TXE, DISABLE );
                uart2_tx_index = 0;
            }
            // TXE flag is cleared by hardware when new data is pushed into DR
        }
    
    
        // Transmission complete
    As stated before, some of this code should not make sense yet so focus on memory
    Last edited by Salem; 07-06-2012 at 02:29 PM. Reason: fixed code formatting

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    At least explain what it is you're doing in general. Is this a USB to serial device or what? What uC you're using? Speed? A little hardware info might be helpful too. Maybe you don't need the massive buffer you think you do.

    Most embedded compilers have options for controlling how big the global data and code sections are, so you could look into tweaking those. Also, if you aren't already, you can look for compiler flags that optimize your code for size instead of speed. It will probably cost you a small amount of speed, but it's not likely it will be critical. Your bottleneck will probably still be the RS232 line itself, not the slightly slower code. If all you have is the array, 2 indexes and a bool, I don't think a struct will help, since all of those seem to be necessary for proper function of your circular buffer. Can you shrink the array a tiny bit? Taking off just one byte should free up enough space for your bool.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Whether it needs to be a global depends on exactly what you're doing with it. Ideally, you declare it in the appropriate function and pass it to the other functions as necessary, as a parameter. In many embedded applications though, especially at the hardware level, you are more likely to need globals.

    When using globals, never define them in a header file. Defining means the variable is actually created, i.e. space is reserved for it. If it's defined in a header file, every .c file that #includes that header file will get a copy of the global variable. You want to define it in one .c file and declare it as an extern in the header file. The extern keyword tells the .c files that #include that header what type the variable is, and that the actual variable is in another .c file (but it's okay if the .c file with the definition also #includes the header with the extern declaration).

  8. #8
    Registered User
    Join Date
    Jun 2012
    Posts
    21
    Aduril, thanks for the response again. I should have thought of that, and it works! I would explain what is going on up there but you already solved the problem : )

  9. #9
    Registered User
    Join Date
    Jun 2012
    Posts
    21
    I just took a small chunk off the array...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable Allocation Problem
    By bavetta in forum C Programming
    Replies: 3
    Last Post: 11-14-2009, 12:52 AM
  2. Relate memory allocation in struct->variable
    By Niara in forum C Programming
    Replies: 4
    Last Post: 03-23-2007, 03:06 PM
  3. Variable allocation, when?
    By Magos in forum C++ Programming
    Replies: 9
    Last Post: 05-19-2003, 06:53 PM
  4. memory allocation
    By afotoohi in forum C Programming
    Replies: 1
    Last Post: 03-04-2003, 10:21 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM