简体   繁体   English

If a static variable is declared out side of a function, will the memory address be the same as if it's declared in a function

[英]If a static variable is declared out side of a function, will the memory address be the same as if it's declared in a function

I was asked these 2 questions during an interview.我在面试的时候被问到这两个问题。 I guess the address of a static variable will be the same no matter where it is declared.我猜 static 变量的地址将是相同的,无论它在哪里声明。 It will also have the same address from run to run.从运行到运行,它也将具有相同的地址。 Correct me if I am wrong.如果我错了,请纠正我。

  1. If a static variable is declared out side of a function, will the memory address be the same as if it's declared in a function? If a static variable is declared out side of a function, will the memory address be the same as if it's declared in a function?

  2. If a static variable is declared out side of a function, will it have the same address every time you run the program?如果 static 变量在 function 之外声明,那么每次运行程序时它是否具有相同的地址?

Cunningham's Law will ensure quick correction of my mistakes:坎宁安定律将确保快速纠正我的错误:

  1. global and local static variables are treated similarly so they may indeed end up at the same address when moved:全局和局部 static 变量的处理方式相似,因此它们在移动时可能确实最终位于相同的地址:
#include <stdio.h>

#ifdef GLOBAL
static int i;
#endif
// static int j;

#ifndef GLOBAL
void f(void) {
    static int i;
    printf("local:  %p\n" , (void *) &i);
}
#endif

int main() {
#ifdef GLOBAL
    printf("global: %p\n" , (void *) &i);
#else
    f();
#endif
}

You need to disable address randomization to observe it:您需要禁用地址随机化来观察它:

$ sudo bash -c 'echo 0 >  /proc/sys/kernel/randomize_va_space';\
gcc 1.c && ./a.out &&\
gcc -DGLOBAL 1.c && ./a.out;\
sudo bash -c 'echo 2 >  /proc/sys/kernel/randomize_va_space'
local:  0x555555558034
global: 0x555555558034

If you add another global static int j (commented out above) after the global i then the effect goes away:如果在全局i之后添加另一个全局static int j (上面注释掉),那么效果就会消失:

local:  0x555555558038
global: 0x555555558034

So call me maybe ??所以打电话给我吧??

  1. No. Modern operating systems, specially the program loader, use address space layout randomization (ASLR) so the address changes on every execution:不。现代操作系统,特别是程序加载器,使用地址空间布局随机化(ASLR),因此每次执行时地址都会改变:
$ ./a.out && ./a.out
global: 0x563bcce14034
global: 0x55cd4a497034

static variables may or may not have the same address in memory when declared globally or locally in the body of a function.当在 function 的主体中全局或局部声明时, static变量在 memory 中可能具有相同的地址,也可能不同。 The fact is nothing can be assumed about these addresses: the C Standard only guarantees that they compare different from the null pointer and different from the addresses of other data objects defined in a C module (albeit compiler extensions can force some symbols to alias one another).事实上,对于这些地址没有任何假设:C 标准仅保证它们比较不同于 null 指针,并且不同于在 Z0D61F8370CAD1D412F80B84D143E125 中定义的其他数据对象的地址(可以强制某些符号为另一个编译器模块扩展名) )。

Modern operating systems use address randomisation to make it more difficult for attackers to exploit software flaws, so the addresses may indeed differ from one run to another.现代操作系统使用地址随机化使攻击者更难利用软件缺陷,因此地址可能确实会因运行而异。

Answering such questions in an interview is tricky: the interviewer's assumptions might be outdated, but a substantiated answer should get you a good mark.在面试中回答这样的问题是很棘手的:面试官的假设可能已经过时,但一个有根据的答案应该会给你一个好分数。

暂无
暂无

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

相关问题 函数内部声明的静态变量的存储空间是多少? - Which is the memory space for static variable declared inside a function? 传递声明为函数指针的变量与传递未声明为指针的变量的地址一样吗? - Is passing a variable declared to be a pointer to a function the same as passing the address of a variable not declared to be a pointer? 相互声明的变量具有相同的内存地址模式 - Variable declared interchangebly has the same pattern of memory address 将非数组变量的地址传递给声明为“Type ptr[static 1]”的 function 参数是否有效? - Is it valid to pass the address of a non-array variable to a function parameter declared as `Type ptr[static 1]`? 函数声明为静态但从未定义 - function declared static but never defined 在函数中声明的返回数组返回C中局部变量的地址? - Returning array declared in function returns address of local variable in C? 如何/何时在C语言中声明函数中的静态变量? - how/when is a static variable in a function declared within C language? 如何在不调用声明的函数的情况下修改局部静态变量? - How to modify a local static variable without calling the function where it is declared? 是否可以在声明的 function 之外访问 static 局部变量? - Is it possible to access static local variable outside the function where it is declared? 当函数中声明相同的变量时,全局变量不起作用 - global variable not working when the same variable works when declared in a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM