Thread: porting C code in MIPS

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    4

    porting C code in MIPS

    Hi,

    i am new to C programming, and want to advice you in general about good tips in order to port C code in small processors like MIPS.

    for example what is a good practice in order to replace functions like malloc?

    Also , is there any easy way to monitor if the program exceeds the available memory during runtime?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Generally, the advice is to write code that is portable. You achieve that with a series of "don't"s. Don't make assumptions about things like what size an integer is. Don't make assumptions about how many digits of precision a floating point variable supports. Don't make assumptions that two variables are beside each other in memory. Don't make assumptions about how data structures are laid out. Don't write complex expressions with lots of side-effects (make your expressions simple, and only change one thing at a time). Don't try to access element 5 of a 3-element array. The list of "don't" you will need to learn is quite long.

    There is no good practice in C for replacing functions like malloc(). Your choices are to either use them as is, wrap them in some way that does more error checking before and after calling malloc(), not use them at all, or write dedicated code (which will probably be non-standard and non-portable) to do a similar thing. Each of those approaches has advantages and disadvantages. There is no "best".

    Making the huge assumption that your code is not misbehaving in some way (e.g. not molesting pointers, so not corrupting data structures that malloc() uses internally) then malloc() will return NULL if it cannot allocate memory. If memory used exceeds memory available, then malloc() cannot allocate memory. If you want more precision than that (for example, a report how how much memory is remaining) then the techniques are non-standard and non-portable (i.e. specific to an operating system).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    4
    Thanks for the answer

    basically i try to remove malloc statements by declaring normal variables of fixed size. Supposing that i know the size of the required space is it ok to change a pointer that is allocated x mount of memory into a varilable of size x? Also i want to ask what happens when malloc is used inside a function...is it correct that memory is allocated EACH time the function is called? so it would increase the memory needs siginificantly?

    Quote Originally Posted by grumpy View Post
    Generally, the advice is to write code that is portable. You achieve that with a series of "don't"s. Don't make assumptions about things like what size an integer is. Don't make assumptions about how many digits of precision a floating point variable supports. Don't make assumptions that two variables are beside each other in memory. Don't make assumptions about how data structures are laid out. Don't write complex expressions with lots of side-effects (make your expressions simple, and only change one thing at a time). Don't try to access element 5 of a 3-element array. The list of "don't" you will need to learn is quite long.

    There is no good practice in C for replacing functions like malloc(). Your choices are to either use them as is, wrap them in some way that does more error checking before and after calling malloc(), not use them at all, or write dedicated code (which will probably be non-standard and non-portable) to do a similar thing. Each of those approaches has advantages and disadvantages. There is no "best".

    Making the huge assumption that your code is not misbehaving in some way (e.g. not molesting pointers, so not corrupting data structures that malloc() uses internally) then malloc() will return NULL if it cannot allocate memory. If memory used exceeds memory available, then malloc() cannot allocate memory. If you want more precision than that (for example, a report how how much memory is remaining) then the techniques are non-standard and non-portable (i.e. specific to an operating system).

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by george_g
    Supposing that i know the size of the required space is it ok to change a pointer that is allocated x mount of memory into a varilable of size x?
    Yes, with the caveat that if the space allocated is too large, it may exceed the given size of the stack (if the variable does not have static storage duration).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    4
    Thanks again. Another (rather basic i guess) question:

    When i compile a c program in my environment i get a message which looks like : size program + min stack = 50K

    so because the total memory is configurable if i select 32K is doesn't fit , if i select i.e. 64K it's ok

    But what it this space of 50K contain? Is it only the instructions of the program, or together with the space allocated for the variables?

    when a program features the command int a ; how it that handled by the compiler? it is allocated on runtime only? compile time? both?





    Quote Originally Posted by laserlight View Post
    Yes, with the caveat that if the space allocated is too large, it may exceed the given size of the stack (if the variable does not have static storage duration).

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > But what it this space of 50K contain? Is it only the instructions of the program, or together with the space allocated for the variables?
    It probably means everything (except dynamic memory allocation), if you're just loading this onto a bare board.

    Example.
    Code:
    $ cat foo.c
    #include <stdio.h>
    int global = 0;
    char msg[] = "hello world";
    int main ( ) {
        int local = 1;
        printf("%d %d %s\n", local, global, msg );
    }
    $ gcc -c foo.c
    $ size foo.o
       text	   data	    bss	    dec	    hex	filename
        115	     12	      4	    131	     83	foo.o
    So in this example, the generated assembler of main() occupies 115 bytes (the text section), the global array msg is 12 bytes(data) and the global variable(bss) is 4 bytes.

    Finding out how much stack space (at compile time) is trickier.
    For simple code with a straightforward call relationship, it's pretty easy to work out.

    It gets harder if you have like many 20+ nested function paths, as you need to sum each call path in turn to work out what the maximum depth is.
    a() -> b() -> c() -> d() -> e() might be less than x() -> y(), if say y has lots of local variables.

    Things get messy though if you have recursive calls, or variable length arrays on the stack.

    Your default stack size might be quite large, but you should be able to configure it down to a really small size if your code is simple enough.
    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.

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    4
    So when we declare a varilable, the compiler allocates memory to the stack at that point, or just gathers information of how much memory will be allocated in runtime?


    Quote Originally Posted by Salem View Post
    > But what it this space of 50K contain? Is it only the instructions of the program, or together with the space allocated for the variables?
    It probably means everything (except dynamic memory allocation), if you're just loading this onto a bare board.

    Example.
    Code:
    $ cat foo.c
    #include <stdio.h>
    int global = 0;
    char msg[] = "hello world";
    int main ( ) {
        int local = 1;
        printf("%d %d %s\n", local, global, msg );
    }
    $ gcc -c foo.c
    $ size foo.o
       text       data        bss        dec        hex    filename
        115         12          4        131         83    foo.o
    So in this example, the generated assembler of main() occupies 115 bytes (the text section), the global array msg is 12 bytes(data) and the global variable(bss) is 4 bytes.

    Finding out how much stack space (at compile time) is trickier.
    For simple code with a straightforward call relationship, it's pretty easy to work out.

    It gets harder if you have like many 20+ nested function paths, as you need to sum each call path in turn to work out what the maximum depth is.
    a() -> b() -> c() -> d() -> e() might be less than x() -> y(), if say y has lots of local variables.

    Things get messy though if you have recursive calls, or variable length arrays on the stack.

    Your default stack size might be quite large, but you should be able to configure it down to a really small size if your code is simple enough.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Porting code - Override Functions in STDLIB
    By NightWolf8800 in forum C Programming
    Replies: 2
    Last Post: 10-18-2009, 07:16 PM
  2. Help with porting some Perl code to C
    By cmac in forum C Programming
    Replies: 3
    Last Post: 01-11-2006, 07:08 PM
  3. Porting code from VC++6 to VC .NET??
    By dug in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2005, 11:42 AM
  4. Porting Code to C++ -- Need Help
    By uberdog in forum Linux Programming
    Replies: 5
    Last Post: 04-28-2004, 12:51 PM
  5. Porting code form *nix to win
    By Merlion in forum C Programming
    Replies: 2
    Last Post: 11-23-2001, 02:42 PM