Thread: how to remove calloc or malloc from code

  1. #1
    Registered User
    Join Date
    May 2019
    Posts
    3

    how to remove calloc or malloc from code

    Hi

    I have a code in C where I am using external library functions provided for example by GRAMPC optimization solver. The code contains calloc and malloc for memory allocation.

    I want to implement my code on hardware like FPGA as well as ARM processors. The code is working properly on ARM board. but for FPGA implementation I cant have dynamic allocation of memory -so cant use malloc or new either.


    Please suggest a way to achieve that.

    Code:
    typedef struct
    {
        typeGRAMPCparam *param;
        typeGRAMPCopt *opt;
        typeGRAMPCsol *sol;
        typeGRAMPCrws *rws;
        typeUSERPARAM *userparam;
    } typeGRAMPC;
    
    
    (*grampc)->opt = (typeGRAMPCopt *)calloc(1, sizeof(*(*grampc)->opt));
        
        if ((*grampc)->opt == NULL) {
            grampc_error(OPT_ALLOC_FAILED);
        }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Are you sure you only ever have one of those things?
    Like there's not some realloc going on later in the code.

    Then you can do
    Code:
    static typeGRAMPCopt myOpt;
    (*grampc)->opt = &myOpt;

    > but for FPGA implementation I cant have dynamic allocation of memory -so cant use malloc or new either.
    There's nothing to stop you implementing your own.

    If all your allocs are one-time at startup, you can fairly trivially implement.
    Code:
    static unsigned char mem[10000];  // pick a size
    unsigned char *base = mem;
    void *malloc( size_t size ) {
        void *result = base;
        base += (size+3) & 4;  // alignment hack for 32-bit processors (use +7 and &8 for 64-bit processors)
        return result;
    }
    calloc is malloc + memset, but the static buffer will be filled with zeros anyway.
    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.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Salem View Post
    Code:
    static unsigned char mem[10000];  // pick a size
    unsigned char *base = mem;
    void *malloc( size_t size ) {
        void *result = base;
        base += (size+3) & 4;  // alignment hack for 32-bit processors (use +7 and &8 for 64-bit processors)
        return result;
    }
    That's an interesting implementation! (really, I didn't thought about it!).
    But there are some problems:

    1- What if size == 0?
    2- What if size > 10000?
    3- Where's free()?

    1 and 2 malloc should return NULL.

    I think to improve this routine a linked list using an static array would be best, using a structure like this:
    Code:
    struct memblock { unsigned int next:31; unsigned int inuse:1; char mem[0]; }
    Where 'next' is an index of the next block in the array... malloc() can return 'mem' if there is room, or NULL if not (or size=0). The inuse boolean is used by free.
    This makes malloc more complex (need to scan each "block" searching for 'not inuse' blocks big enough to allocate)...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > 1- What if size == 0?
    What about it?
    malloc is allowed to return either NULL, or a valid pointer.
    Either way, you can't dereference it without invoking UB.

    > 2- What if size > 10000?
    That's why the comment says "pick a size".

    > 3- Where's free()?
    There is no free, at least none which makes any sense.

    The context is for an embedded platform with NO memory management, and allocation happening exactly once at program start.
    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.

  5. #5
    Registered User
    Join Date
    May 2019
    Posts
    3
    Quote Originally Posted by patneva View Post

    Code:
    (*grampc)->opt = (typeGRAMPCopt *)calloc(1, sizeof(*(*grampc)->opt));
        
        if ((*grampc)->opt == NULL) {
            grampc_error(OPT_ALLOC_FAILED);
        }
    thanks for the suggestion regarding calloc - one basic doubt here - what is significance of 1 inside calloc? Is it related to number of memory locations allocated? what if it is another variable like

    Code:
    *cs = (typeRNum *)calloc(size, sizeof(typeRNum));

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > what is significance of 1 inside calloc?
    You do know C, and how to read a manual page right?

    Because if you don't, what you're trying to do is going to get difficult in a hurry.

    > cs = (typeRNum *)calloc(size, sizeof(typeRNum));
    It's as if you had done this as a fixed sized array.
    typeRNum cs[size];
    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
    May 2019
    Posts
    3
    Quote Originally Posted by Salem View Post
    > what is significance of 1 inside calloc?
    You do know C, and how to read a manual page right?

    Because if you don't, what you're trying to do is going to get difficult in a hurry.

    > cs = (typeRNum *)calloc(size, sizeof(typeRNum));
    It's as if you had done this as a fixed sized array.
    typeRNum cs[size];

    sorry if my question is sounding stupid - I do know upto arrays , functions, pointers - but here cs[size] will be treated as variable size array right? and does C support that?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    No, C does not support variable sized arrays.

    Or more correctly, only C99 supports variable length arrays.

    Even that might not do you any good.
    - the array might be allocated on the stack, which might be very small on your platform.
    - the array might depend on malloc, which you said you don't have.

    Even then, the array will be limited in scope to the function you declare it in and all it's dependants.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calloc() vs. malloc()
    By peripatein in forum C Programming
    Replies: 12
    Last Post: 06-22-2013, 10:20 AM
  2. malloc ,calloc code problem..
    By transgalactic2 in forum C Programming
    Replies: 37
    Last Post: 10-26-2008, 11:43 AM
  3. malloc, calloc from the FAQ
    By salvadoravi in forum C Programming
    Replies: 10
    Last Post: 01-21-2008, 03:29 AM
  4. Malloc And Calloc
    By pradeepc in forum C Programming
    Replies: 1
    Last Post: 07-28-2007, 12:48 AM
  5. calloc vs malloc
    By Jubba in forum C Programming
    Replies: 2
    Last Post: 02-21-2002, 04:54 PM

Tags for this Thread