Using calloc/malloc or new/delete

Discussion in 'General Programming Discussion' started by erpark, Sep 6, 2014.

  1. erpark

    erpark New Member

    Joined:
    Sep 6, 2014
    Messages:
    2
    Likes Received:
    0
    Hi Everyone,

    I have been attempting to port a small application from an Arduino Uno over to the Due integrated into the Udoo and I am having some difficulty solely with allocating dynamic memory blocks.

    On the Uno...

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    unsigned int ** table = NULL;

    void setup()
    {
    Serial.begin(9600);
    table = (unsigned int **) calloc(sizeof(unsigned int *), 10);
    delay(5000);
    Serial.println("Table Allocated");
    }

    ... works fine... the Due however, hangs and I get no output from any of the Serial.print's. I have tried using malloc instead, also I tried the C++, new/delete.

    In any case, an attempt to allocate a dynamic block of memory causes a hang and the Due is non-responsive until I comment out the allocation statement and reupload. I have played around with the size, assuming I may have asked for too big a block (ie was asking for 144 * 4 unsigned int's at first), but even modest sizes, 10 x 4 unsigned int's blew up.

    I don't, unfortunately have an actual Arduino Due laying about to test the code.

    So I am at a loss to explain this. Am I missing something... is there some architectural issue with Due's and calloc/malloc or the avbr-libc???
     
  2. mkopack

    mkopack Member

    Joined:
    Jun 14, 2013
    Messages:
    451
    Likes Received:
    21
    Instead of using Malloc/New, if I understand what you're doing, why not just set up an array?

    unsigned int table [10];

    That will set up an array that will hold 10 unsigned ints... Which seems to be exactly what you're wanting to do...
     

Share This Page