小编Alb*_*chi的帖子

缺少堆栈更新

我试图深入研究堆栈分配,所以我打算获得这样的堆栈配置:

0x000009 -> 0,
0x000005 -> 1,
0x000001 -> 2,
...
Run Code Online (Sandbox Code Playgroud)

这是代码:

int main(){

    int i;
    int j;
    int pseudoarray;

    printf("address of i \t\t%p\n",&i);
    printf("address of j \t\t%p\n",&j);
    printf("address of pseudoarray \t%p\n\n",&pseudoarray);

    for(i =0;i<10;i++){

        *((&pseudoarray)-i)=i;
        printf("Written at \t%p value \t%d\n\n",(&pseudoarray)-i, i);

        for(j =0; j<10;j++){
            printf("Read at \t%p value \t%d\n",(&pseudoarray-j), *((&pseudoarray-j)));
        }
        printf("\n\n\n");
    }

    printf("\n%d times done",i);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,它只写入第三个单元,然后不写入任何内容。

这是它打印的内容(从第6次写入操作到10的打印与5相同):

C:\Users\Halib\OneDrive\Documents\Corsi\Corso C>plain.exe
address of i            0061FF1C
address of j            0061FF18
address of pseudoarray  0061FF14

Written at      0061FF14 value  0 …
Run Code Online (Sandbox Code Playgroud)

c stack pointers

5
推荐指数
1
解决办法
91
查看次数

函数体中创建的std::string_view可以返回吗?

假设你有这段代码

#include <iostream>

using namespace std;

std::string_view foo(){
    char arr[3];
    arr[0]='0';
    arr[1]='1';
    arr[2]='\0';
    
    std::string_view sv = arr;
    return sv;
}

int main(){
    cout<<foo()<<endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

由于arr是 在堆栈中,因此在创建期间svsv 应该指向堆栈中的某个位置,因此,由于 string_view 不会复制内部 char 数组的内容(与 std::string 上发生的情况相反),我会预计这里会出现错误,但它打印正确01

c++ c++17

1
推荐指数
1
解决办法
159
查看次数

运算符等于重载 c++ 显然不起作用

有人能解释一下为什么cout这里没有被调用吗?

#include <iostream>

using namespace std;

class Test {
public:
    int a = 1;

    Test &operator=(const Test &other) {
        cout << "overloading here" << endl;
        return *this;
    }
};


int main() {
    Test t1;
    Test &t3 = t1;
    return 0;
}

Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
78
查看次数

标签 统计

c++ ×2

c ×1

c++17 ×1

pointers ×1

stack ×1