'for loop' 在硬件层面发生了什么?内存是自动分配的吗? (C++)
What occurrs in a 'for loop' at the hardware level? Is memory automatically assigned? (C++)
我这里有一些代码可以反转字符数组:
#include <iostream>
#include <string>
using namespace std;
char s[50];
void reverseChar(char * s)
{
for (int i=0; i<strlen(s)/2; ++i)
{
char temp = s[i];
s[i] = s[strlen(s)-i-1];
s[strlen(s)-i-1] = temp;
}
}
int main() {
cout << "Hello, this program reverses words." << endl << endl;
cout << "Enter in a word, no spaces please:" << endl;
cin.getline(s, 50);
cout << "This is the word, it has now been reversed:" << endl;
reverseChar(s);
cout << s;
return 0;
}
在 for 循环中,谁能解释一下硬件级别的情况。我理解'temp'是分配给一个byte,也就是分配给s[i]的值。
内存分配给所有东西了吗?
等号、s[i]等?
将字节赋给s[i]中的值后,s[i]被赋给数组s中的某个其他值。然后将此其他值分配给 temp.
我无法理解所有这些字节的去向,以及 C++ 如何操纵它们。
我明白这行:
s[i] = s[strlen(s)-i-1];
正在交换占位符值?
这一行:
s[strlen(s)-i-1] = temp;
'copied' 值被发送到 'temp'。但是这个临时值之后会发生什么,一旦 for 循环重复,它会变成新的 'temp' 吗?
In the for loop can someone explain what is going on at the hardware
level. I understand 'temp' is allocated to a byte, which is assigned
to the value of s[i].
Is memory allocated to everything?
是的,它被分配了,但是在栈上。你需要了解栈和堆的区别
是的,所有变量都以某种形式的内存表示。但是话又说回来,根据上下文,它可以是堆栈也可以是堆。
The equals sign, s[i], etc?
Equals 是一个运算符,用于通知编译器正在进行赋值操作,因此不需要内存。
另一方面,s[i] 是一个数组对象,在堆栈内存中表示。
The 'mirrored' value is sent to 'temp'. But what happens to this temp
value afterwards, does it become the new 'temp' once the for loop
reiterates?
因为在循环中您已将 temp 声明为,
char temp;
每次迭代都会在堆栈中创建一个新变量 temp。
我强烈建议您阅读典型操作系统中的内存组织。您可以从 here.
的基础知识开始
编辑:另请注意,根据变量的范围,编译器会根据其范围自动取消分配堆栈变量的内存。对于上面的 temp 变量,它的范围在 for 循环结束时结束,因此它在那个时候被销毁。因此,编译器最终不会为每次迭代分配 space 个字符的内存。
我这里有一些代码可以反转字符数组:
#include <iostream>
#include <string>
using namespace std;
char s[50];
void reverseChar(char * s)
{
for (int i=0; i<strlen(s)/2; ++i)
{
char temp = s[i];
s[i] = s[strlen(s)-i-1];
s[strlen(s)-i-1] = temp;
}
}
int main() {
cout << "Hello, this program reverses words." << endl << endl;
cout << "Enter in a word, no spaces please:" << endl;
cin.getline(s, 50);
cout << "This is the word, it has now been reversed:" << endl;
reverseChar(s);
cout << s;
return 0;
}
在 for 循环中,谁能解释一下硬件级别的情况。我理解'temp'是分配给一个byte,也就是分配给s[i]的值。
内存分配给所有东西了吗?
等号、s[i]等?
将字节赋给s[i]中的值后,s[i]被赋给数组s中的某个其他值。然后将此其他值分配给 temp.
我无法理解所有这些字节的去向,以及 C++ 如何操纵它们。
我明白这行:
s[i] = s[strlen(s)-i-1];
正在交换占位符值?
这一行:
s[strlen(s)-i-1] = temp;
'copied' 值被发送到 'temp'。但是这个临时值之后会发生什么,一旦 for 循环重复,它会变成新的 'temp' 吗?
In the for loop can someone explain what is going on at the hardware level. I understand 'temp' is allocated to a byte, which is assigned to the value of s[i].
Is memory allocated to everything?
是的,它被分配了,但是在栈上。你需要了解栈和堆的区别
是的,所有变量都以某种形式的内存表示。但是话又说回来,根据上下文,它可以是堆栈也可以是堆。
The equals sign, s[i], etc?
Equals 是一个运算符,用于通知编译器正在进行赋值操作,因此不需要内存。 另一方面,s[i] 是一个数组对象,在堆栈内存中表示。
The 'mirrored' value is sent to 'temp'. But what happens to this temp value afterwards, does it become the new 'temp' once the for loop reiterates?
因为在循环中您已将 temp 声明为,
char temp;
每次迭代都会在堆栈中创建一个新变量 temp。
我强烈建议您阅读典型操作系统中的内存组织。您可以从 here.
的基础知识开始编辑:另请注意,根据变量的范围,编译器会根据其范围自动取消分配堆栈变量的内存。对于上面的 temp 变量,它的范围在 for 循环结束时结束,因此它在那个时候被销毁。因此,编译器最终不会为每次迭代分配 space 个字符的内存。