< 返回版块

leslieDD 发表于 2021-04-20 10:56

use std::{fmt::Pointer, sync::{Arc}};
use std::sync::atomic::{AtomicUsize};
#[derive(Clone)]
struct AppState {
    count: Arc<AtomicUsize>
}

impl Pointer for AppState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        println!("rust ok!");
        let content = format!("point addr: {:p}", *self);
        writeln!(f, "{}", content)
    }
}
fn main() {
    let data = AppState {
        count: Arc::new(AtomicUsize::new(0)),
    };

    let b = data.clone();
    let c = &b;
    println!("{:p}, {:p}, {:p}", data, b, c);

}
thread 'main' has overflowed its stack
error: process didn't exit successfully: `target\debug\art.exe` (exit code: 0xc00000fd, STATUS_STACK_OVERFLOW)

是什么原因导致stack溢出

评论区

写评论
johnmave126 2021-04-20 11:22

data进行{:p}格式化->调用<AppState as Pointer>::fmt(&data)->对*self,即data又进行了{:p}格式化->又调用了<AppState as Pointer>::fmt(&data)->重复到溢出

1 共 1 条评论, 1 页