0

I just want to understand the difference in RAM allocation.

Why if i define a variable before function i have a RAM overflow and when i define it inside a function it is ok?

For example:

/*RAM OK*/
void Record(int16_t* current, int i,int n)
{
  float Arr[NLOG2] = {0};
  for(i=0;i<n;i++)
      Arr[i]=current[i*5];
}

/*RAM OVERFLOW*/
static float Arr[NLOG2] = {0};

void Record(int16_t* current, int i,int n)
{
   for(i=0;i<n;i++)
       Arr[i]=current[i*5];
}

This is the message:

unable to allocate space for sections/blocks with a total estimated minimum size of 0x330b bytes (max align 0x8) in <[0x200000c8-0x200031ff]> (total uncommitted space 0x2f38).

3
  • Is it showing message RAM overflow? Commented Aug 14, 2014 at 14:39
  • Where is n declared? Commented Aug 14, 2014 at 14:39
  • This is the massage :unable to allocate space for sections/blocks with a total estimated minimum size of 0x330b bytes (max align 0x8) in <[0x200000c8-0x200031ff]> (total uncommitted space 0x2f38). Commented Aug 14, 2014 at 14:40

2 Answers 2

1

The difference is that in the first case, Arr is declared on the stack; until the function is called, that array doesn't exist. The generated binary contains code for creating the array, but the array itself isn't in the binary.

In the second case, however, Arr is declared outside of any function (aka at file scope). Therefore, it always exists, and is stored in the binary. Because you appear to be working on an embedded platform, this otherwise insignificant difference causes your "RAM overflow" error.

Sign up to request clarification or add additional context in comments.

3 Comments

but both function will create the same result when i would call them? it is ok to use the first case instead off second?
@user3428151 only the first case creates the array. As far as which method is OK depends on what you want to do with Arr. In the first case, as soon as the function returns, Arr no longer exists.
thanks, this array used only by this function so i can create a local definition.
1

In the 2nd case, the array is allocated when the application starts. It remains in the memory until the app quits.

In the 1st case, the array is only allocated when function void Record(int16_t* current, int i,int n) is called. The array is gone after the function finishes its execution.

static keyword doesn't have any impact if you have only a single compilation unit (.o file).

Global variables (not static) are there when you create the .o file available to the linker for use in other files. Therefore, if you have two files like this, you get name collision on a:

a.c:

#include <stdio.h>

int a;

int compute(void);

int main()
{
    a = 1;
    printf("%d %d\n", a, compute());
    return 0;
}

b.c:

int a;

int compute(void)
{
    a = 0;
    return a;
}

because the linker doesn't know which of the global as to use.

However, when you define static globals, you are telling the compiler to keep the variable only for that file and don't let the linker know about it. So if you add static (in the definition of a) to the two sample codes I wrote, you won't get name collisions simply because the linker doesn't even know there is an a in either of the files:

a.c:

#include <stdio.h>

static int a;

int compute(void);

int main()
{
    a = 1;
    printf("%d %d\n", a, compute());
    return 0;
}

b.c:

static int a;

int compute(void)
{
    a = 0;
    return a;
}

This means that each file works with its own a without knowing about the other ones.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.