0

I am trying to use malloc to dynamically allocate an array. After I use it, I only end up with an array of one element. Code is as follows:

uint8_t *arraystuffs;
uint16_t update_size;
//at somepoint, update_size get set to 2448

arraystuffs = (uint8_t *) malloc(update_size);

I then try to assign things to the array that I think I should have. When I look in the debugger, I only have a 1 element array, with the value 0x58 in it. I have used malloc many times before, just as above, and suddenly it does not work. I double checked several examples online of how malloc should work, and I believe that I have it set up right.

I have tried using smaller values for update_size, like 50, but that has not solved the problem.

I have tried taking the typecast out, but that doesn't help either.

I am working on an stm32F4 MCU in the Keil IDE. Any insight?

Owl_Prophet
  • 362
  • 5
  • 15
  • 1
    Side note: in C the cast of the ``malloc`` result is unnecessary and even dangerous. See http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – hivert Jul 25 '13 at 20:35
  • 1
    How are you looking at the array? If uint8_t is interpreted as a character string in the debugger and the second position in the array is zero, then the display is truncated at the 'nul'. Use memory dump to look at memory pointed to by 'arraystuffs'. – KeithSmith Jul 25 '13 at 20:36
  • That code looks fine. Are you remembering that `malloc` returns uninitialised memory so the initial content of your array will be unpredictable? What happens if you try to write to `arraystuffs[1]...arraystuffs[update_size-1]`? – simonc Jul 25 '13 at 20:36
  • @simonc - Later on in the code I do that. I suppose I did not make it clear. I go through in a for loop(update_size counts) and assign each element. ->KeithSmith - I am using the keil debugger to look at the contents of the array, and it shows only one element. – Owl_Prophet Jul 25 '13 at 20:38
  • shouldn't it be update_size*type_of_whats_in_array ? For the sake of clarity if nothing else. – Jiminion Jul 25 '13 at 20:39

2 Answers2

3

This is because the debugger does not know the actual length of the array. From the debugger's perspective, arraystuffs is simply a pointer. That's why it only shows one element.

There is nothing wrong in the code you showed, so if your program is not behaving as you expected, the problem lies somewhere else.

Benawii
  • 133
  • 4
  • Good Call. The debugger was not showing me the proper size of the array. How odd. – Owl_Prophet Jul 25 '13 at 21:47
  • Not odd. What information does the debugger have to know the amount of memory that arraystuffs points to? All malloc() did was return a pointer. You know the pointer points to a block of memory, does the debugger? You didn't declare arraystuff[20]. You declared *arraystuff. – KeithSmith Jul 25 '13 at 22:26
-1

in my experience is always useful, when you ask for memory space, to put the size of what you are going to store multiplied by the amount of items you'll have. For example if you want an array of ints I would do:

int *my_ints; my_ints = (int*) malloc(20*sizeof(int));

in your case arraystuffs = (uint8_t *) malloc(update_size*sizeof(uint8_t));

pmoretti
  • 36
  • 3