Ram2

theboot900

Well-known member
Simple question.

Does using malloc automatically use ram2?

If not is it possible to add a dynamicly sized array to ram2?
 
Great thanks Kurt!

I needed to make some temporarily arrays dynamically sized at run time to do some one of calculations at setup.

Because they're dynamically sized they're on the heap (dont think you can do new and have it on the stack?) and I realised when I disposed of them they would possibly be causing large fragmentation holes in RAM1. I figure nothing else touches ram2 at the time so they should be cleaned up nicely without any fragmentation
 
You create "new" objects on the stack simply by declaring them inside functions.
My object I create has an internal array that is dynamically set to a size by calling a function called Init on the object

So the object will be on the stack.
The internal array would be on the heap?
 
My object I create has an internal array that is dynamically set to a size by calling a function called Init on the object

So the object will be on the stack.
The internal array would be on the heap?
You have two options to create stuff on the stack:

1) You can use a variable sized array. It may be simplest to move the main of the code into a function, and pass in the length as an argument:

Code:
void inner (size_t n)
{
  uint32_t buffer[n];

  // do stuff here with buffer

  // when inner returns, the space is automatically returned to the stack
  return;
}

void outer (void)
{
  size_t n;

  // calculate n here

  // Use variable sized array
  inner (n);
}

2) You can use the __builtin_alloca function to allocate stuff on the stack:

Code:
void stuff (void)
{
  size_t n;

  // Calculate n

  uint32_t *array = (uint32_t *) __builtin_alloca (sizeof (uint32_t) * n);

  // Do stuff

  // space for array will disappear when stuff exits.
  return;
}
 
You have two options to create stuff on the stack:

1) You can use a variable sized array. It may be simplest to move the main of the code into a function, and pass in the length as an argument:

Code:
void inner (size_t n)
{
  uint32_t buffer[n];

  // do stuff here with buffer

  // when inner returns, the space is automatically returned to the stack
  return;
}

void outer (void)
{
  size_t n;

  // calculate n here

  // Use variable sized array
  inner (n);
}

2) You can use the __builtin_alloca function to allocate stuff on the stack:

Code:
void stuff (void)
{
  size_t n;

  // Calculate n

  uint32_t *array = (uint32_t *) __builtin_alloca (sizeof (uint32_t) * n);

  // Do stuff

  // space for array will disappear when stuff exits.
  return;
}
You legend thanks for that!
 
Back
Top