简体   繁体   English

局部变量和静态变量

[英]Local variable and static variables

I just want to understand the difference in RAM allocation. 我只想了解RAM分配的区别。

Why if i define a variable before function i have a RAM overflow and when i define it inside a function it is ok? 为什么在函数之前定义变量会导致RAM溢出,而在函数内部定义它却可以吗?

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). 无法为<[0x200000c8-0x200031ff]>中的总估计最小大小为0x330b字节(最大对齐0x8)的段/块分配空间(未承诺的总空间0x2f38)。

The difference is that in the first case, Arr is declared on the stack; 区别在于,在第一种情况下, Arr在堆栈上声明; 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). 但是,在第二种情况下, Arr在任何函数之外(也称为文件作用域)声明。 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. 因为您似乎正在嵌入式平台上工作,否则这种微不足道的差异会导致“ RAM溢出”错误。

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. 在第一种情况下,仅在调用函数void Record(int16_t* current, int i,int n)时分配数组。 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). 如果您只有一个编译单元(.o文件),则static关键字不会有任何影响。

Global variables (not static ) are there when you create the .o file available to the linker for use in other files. 当创建.o文件时,全局变量(不是static )存在于链接器中,供其他文件使用。 Therefore, if you have two files like this, you get name collision on a : 因此,如果您有两个这样的文件,则会在上a名称冲突:

ac: 交流电:

#include <stdio.h>

int a;

int compute(void);

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

bc: 公元前:

int a;

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

because the linker doesn't know which of the global a s to use. 因为链接不知道哪个全局的a来使用s。

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: 所以,如果你添加static (以定义a )我写这两个示例代码,你不会得到名称冲突只是因为链接甚至不知道有一个a在这两个文件中:

ac: 交流电:

#include <stdio.h>

static int a;

int compute(void);

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

bc: 公元前:

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. 这意味着每个文件都可以使用自己的a而无需了解其他文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM