I've been trying to gain a deeper understanding of how compilers generate machine code, and more specifically how GCC deals with the stack. In doing so I've been writing simple C programs, compiling them into assembly and trying my best to understand the outcome. Here's a simple program and the output it generates: asmtest.c : void main() { char buffer[5]; } asmtest.s : pus
我一直在努力深入理解编译器如何生成机器代码,更具体地说,GCC如何处理堆栈。 在这样做的过程中,我一直在编写简单的C程序,将它们编译成汇编语言,并尽我所能了解结果。 这是一个简单的程序和它生成的输出: asmtest.c : void main() { char buffer[5]; } asmtest.s : pushl %ebp movl %esp, %ebp subl $24, %esp leave ret 令我费解的是为什么24字节被分配给堆栈。 我知道,由于处理器如何处理内存,
My question is if i have some function void func1(){ char * s = "hello"; char * c; int b; c = (char *) malloc(15); strcpy(c,s); } I think the s pointer is allocated on the stack but where is the data "hello" stored does that go in the data segment of the program? As for c and b they are unitialized and since 'c = some memory address' and it doesnt have one
我的问题是如果我有一些功能 void func1(){ char * s = "hello"; char * c; int b; c = (char *) malloc(15); strcpy(c,s); } 我认为s指针是分配在堆栈上的,但是数据“hello”存储的数据在程序的数据段中存在哪里呢? 至于c和b,它们是单元化的,因为'c =一些内存地址'并且它没有一个,但它是如何工作的? 和b也没有内容,所以它不能存储在堆栈上? 然后,当我们用malloc为堆分配内存c时,现在
As I know if the int variable ( value type ) is declared directly within a class ( reference type ), the memory for the variable allocated on the heap . But if there is a method in a class and the variable is declared inside a method, or is it an argument, the memory allocated on the stack . public class A { int x; // heap public void Func(int y) // stack { int z; // stac
据我所知,如果int变量( 值类型 )直接在类 ( 引用类型 )中声明, 堆内分配的变量的内存。 但是,如果在类中有方法并且变量是在方法内声明的,或者它是一个参数,则分配给堆栈的内存。 public class A { int x; // heap public void Func(int y) // stack { int z; // stack } } 我如何看到内存分配的位置? 当你说'内存在哪里'时,我假设你指的是赋予变量的实际虚拟地址,也可能是
This question already has an answer here: Why isn't sizeof for a struct equal to the sum of sizeof of each member? 11 answers actual size of my struct (sum of all the parts) is less than size of the type itself" Because many computer operations are faster when the values they work on are aligned at certain memory boundaries, the C language standard allows implementations to do suc
这个问题在这里已经有了答案: 为什么不是sizeof等于每个成员的sizeof之和? 11个答案 我的结构的实际大小(所有部分的总和)小于类型本身的大小“ 由于许多计算机操作在其工作的值在特定内存边界对齐时更快,因此C语言标准允许实现进行这种对齐。 在大多数系统中,根据int的大小,ints将在4字节或8字节的边界上对齐。 必须对齐结构的大小,以便数组中下一个结构的地址将正确对齐,因此以int对齐4字节边界开始的结构必须
I have a class that stores update statements. I cant execute these statements within my parallel for loop, as this is causing dead locks. The statements are executed once the loop is completed, in a scenario for 100 rows it works okay. But certain scenarios generate over 100,000 statements. Executing these statements on a sequential loop takes too long. What I wish to achieve is, upon addi
我有一个存储更新语句的类。 我无法在我的parallel for循环中执行这些语句,因为这会导致死锁。 这些语句在循环完成后执行,在100行的情况下它可以正常工作。 但某些场景会产生超过100,000条语句。 在顺序循环上执行这些语句需要很长时间。 我希望实现的是,在向类中添加100个语句时,我在一个单独的线程上执行这些语句,并清除语句变量,以便可以添加和执行下一个100语句。 我是多线程新手。 这是可以实现的吗? 如
I'm trying to compile and run following program without main() function in C . I have compiled my program using the following command. gcc -nostartfiles nomain.c And compiler gives warning /usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000400340 Ok, No problem. then, I have run executable file(a.out), both printf statements print successfully, and then get s
我试图在C没有main()函数的情况下编译和运行下面的程序。 我使用以下命令编译了我的程序。 gcc -nostartfiles nomain.c 编译器给出警告 /usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000400340 好的,没问题。 然后,我运行可执行文件(a.out), printf语句都成功打印,然后出现分段错误 。 所以,我的问题是, 成功执行打印语句后为什么分段错误? 我的代码: #include <stdio.
I stumbled over an interesting question in a forum a long time ago and I want to know the answer. Consider the following C function: f1.c #include <stdbool.h> bool f1() { int var1 = 1000; int var2 = 2000; int var3 = var1 + var2; return (var3 == 0) ? true : false; } This should always return false since var3 == 3000 . The main function looks like this: main.c #incl
很久以前,我在一个论坛上偶然发现了一个有趣的问题,我想知道答案。 考虑下面的C函数: 在f1.c #include <stdbool.h> bool f1() { int var1 = 1000; int var2 = 2000; int var3 = var1 + var2; return (var3 == 0) ? true : false; } 这应该始终返回false因为var3 == 3000 。 main功能如下所示: main.c中 #include <stdio.h> #include <stdbool.h> int main() { printf( f1(
I want to know when exactly the memory is cleared in stack which is allocated for local function calls. I have seen in some video tutorial when the function call is returned to main the memory which is allocated for local function is cleared. I have few questions on below program, please explain. #include<stdio.h> void print(){ printf("testing n"); } int* sum(int* a, int* b){ int c = *
我想知道什么时候准确地清除了分配给本地函数调用的堆栈中的内存。 我在某些视频教程中看到,当函数调用返回到main时,为本地函数分配的内存被清除。 下面的程序我有几个问题,请解释一下。 #include<stdio.h> void print(){ printf("testing n"); } int* sum(int* a, int* b){ int c = *a + *b; return &c; } int main(){ int a=3,b=2; int *ptr = sum(&a,&b); print(); printf("sum is: %d",
This seems like a simple question, but I can't find it with the Stack Overflow search or Google. What does a type followed by a _t mean? Such as int_t anInt; I see it a lot in C code meant to deal closely with hardware—I can't help but think that they're related. As Douglas Mayle noted, it basically denotes a type name. Consequently, you would be ill-advised to end variable or
这似乎是一个简单的问题,但我无法通过Stack Overflow搜索或Google找到它。 类型后跟_t是什么意思? 如 int_t anInt; 我在C代码中看到很多与硬件密切相关的东西 - 我忍不住想它们是相关的。 正如Douglas Mayle指出的那样,它基本上表示一个类型名称。 因此,如果用' _t '结束变量名或函数名,你可能会产生混淆。 和size_t ,C89标准定义了wchar_t , off_t , ptrdiff_t ,以及其他一些我已经忘记的标准。 C99
So I am learning MSIL right now to learn to debug my C# .NET applications. I've always wondered: what is the purpose of the stack? Just to put my question in context: Why is there a transfer from memory to stack or "loading?" On the other hand, why is there a transfer from stack to memory or "storing"? Why not just have them all placed in the memory? Is it becaus
所以我现在正在学习MSIL来学习调试我的C#.NET应用程序。 我一直在想: 堆栈的目的是什么? 只要把我的问题放在上下文中: 为什么有从内存转移到堆栈或“加载”? 另一方面,为什么有从堆栈转移到内存或“存储”? 为什么不把它们都放在内存中? 是因为它快吗? 是因为它基于RAM吗? 为了效率? 我试图理解这一点,以帮助我更深入地理解CIL代码。 更新:我非常喜欢这个问题,我于2011年11月18日将它作为我博客的主