从双精度型变量转换为整型变量
我在 Dev C++ IDE 中编写了这个程序。我预计它可能会崩溃。但它显示正确的输出。请解释一下内存是如何在这里分配的。为什么这是有效的。
int main()
{
int i=10;
double d=3333333.555 ;
i=d+d;
printf(" Value of I after assignment %d",i);
getch();
}
I wrote this program in Dev C++ IDE. I was expecting it might get crash. but it is displaying the right output. can some please explain how the memory gets allocated here.why this is working.
int main()
{
int i=10;
double d=3333333.555 ;
i=d+d;
printf(" Value of I after assignment %d",i);
getch();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 C 语言中,局部变量和参数保存在寄存器和堆栈中。这意味着只要堆栈中有可用空间,它们就会被放在那里,而无需显式分配。
事实上,所有程序都是从默认分配的堆栈开始的,这就是为什么C程序不需要申请更多内存。
堆栈如何工作?嗯......一般来说,有一个寄存器用于保存指向内存块的指针。每当您进入一个函数时,该寄存器都会被移动,以便它现在指向堆栈的空闲部分,并且当您离开该函数时,堆栈寄存器的旧值将被恢复。
内部工作原理有点棘手,但这就是总体思路。
In C, local variables and parameters are held in registers and in the stack. That means that as long as you have available space in the stack, they will be put there, with no explicit allocation.
In fact, all the programs start with a stack allocated by default, that's why there's no need for the C program to request more memory.
How does the stack work? well... in general there's a register for holding a pointer to a chunk of memory. Whenever you enter in a function, that register is moved so that it now points to a free portion of the stack, and when you leave that function, the old value of the stack register is restored.
The inner workings are a bit more trickier, but that's the general idea.