这两个表达式在C ++中是否相同?

时间:2018-11-24 13:18:53

标签: c++ oop

表达式1-

int a;
int* ap = &a;

表达式2-

int a;
int*ap = new a;

2 个答案:

答案 0 :(得分:3)

否。

表达式1定义类型df = pd.DataFrame({'time':['10:20:30','20:03:04'], 'a':[2,3], 'b':[-4,5]}) df['time'] = pd.to_datetime(df['time']) hour_col = df['time'] hour_col = hour_col.apply(lambda t: t.hour) df = pd.concat([df, hour_col], axis=1) #converting to list because columns are immutable cols = df.columns.tolist() #set 4th value cols[3] = 'hour' #assign back df.columns = cols print (df) time a b hour 0 2018-11-24 10:20:30 2 -4 10 1 2018-11-24 20:03:04 3 5 20 的变量a和类型int的变量ap。还将int*初始化为先前定义的变量ap的内存地址。


由于a无效的语法,因此无法编译表达式2。

假设您的意思是new a,则表达式2定义类型new int的变量a和类型int的变量ap。它还会初始化int*以指向动态分配的ap


在表达式1中,您有1 int和1 int

在表达式2中,您有2个int*,一个为自动存储(即ints),另一个为动态存储,还有1个a int*ap指向通过动态存储空间存储的ap

答案 1 :(得分:2)

表达式1-

int a;
int* ap = &a;

Ans:这是一个int指针,将保留在堆栈中。

表达式2-

int a;
int*ap = new a; **// syntax error**

回答:new将在堆中分配内存,这需要手动清理(使用delete),而在表达式1中,超出范围时它将自动清理。

相关问题