小编phi*_*611的帖子

分段错误和指向非现有结构的指针不为空

我有以下结构:

typedef struct treeNode *tree;
typedef struct treeNode {
  int key; 
  tree left, right;
} treeNode;
Run Code Online (Sandbox Code Playgroud)

用这棵树:

我的树

问题:mytree-> left-> left-> left不是NULL.但为什么?!如何检查我是否到达分支的末尾?

  tree mytree = (tree)malloc(sizeof(treeNode));
  mytree->key = 17;
  mytree->left = (tree)malloc(sizeof(treeNode));
  mytree->left->key = 5;
  mytree->left->left = (tree)malloc(sizeof(treeNode));
  mytree->left->right = (tree)malloc(sizeof(treeNode));
  mytree->left->left->key = 20;
  mytree->left->right->key = 2;
  mytree->right = (tree)malloc(sizeof(treeNode));
  mytree->right->key = 1;
  mytree->right->left = (tree)malloc(sizeof(treeNode));
  mytree->right->right = (tree)malloc(sizeof(treeNode));
  mytree->right->left->key = 6;
  mytree->right->right->key = 3;
Run Code Online (Sandbox Code Playgroud)

c struct pointers

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

Rust 中的盒装函数工厂(高阶函数)导致“调用表达式需要函数”

我想传递一个函数工厂,即一个高阶函数,但我收到call expression requires function错误。

// function that takes 4 f32 generates a function which maps one f32 to another f32
// DOESN'T WORK
type FunctionFactoryType = Box<dyn Fn(f32, f32, f32, f32) -> (dyn Fn(f32) -> f32)>;

fn abc(x: FunctionFactoryType) {
    (*x)(1.0, 2.0, 3.0, 4.0);
    // ^ call expression requires function
    // expected function, found `(dyn Fn(f32, f32, f32, f32) -> (dyn Fn(f32) -> f32 + 'static) + 'static)`
}
Run Code Online (Sandbox Code Playgroud)

如果我使用“常规函数”(不是高阶函数),即Box<dyn Fn(f32 -> f32)>它有效。我错过了什么还是这是一个 Rust 错误?

rust

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

如何以方便的方式对齐堆(在盒子中)上的内存?

我想将堆内存对齐到特定的对齐边界。该边界在编译时已知。无论如何,Box抽象不允许我指定某种对齐方式。编写我自己的Box抽象也感觉不对,因为所有 Rust 生态系统都Box已经使用了。实现堆分配对齐的便捷方法是什么?

PS:在我的具体情况下,我需要页面对齐。

heap-memory alignment rust

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

无法使用Spring Boot提供静态index.html

我是Spring的新手,我觉得很难进入它.我想提供一个静态index.html,但它不起作用.在此输入图像描述

我使用教程但无法访问index.html: http:// localhost:8080 /http:// localhost:8080/src/main/public/index.html

IndexHtmlController:

package de.phip1611.springboot_test_1;

import org.springframework.stereotype.Controller;

@Controller
public class IndexHtmlController {}
// due to https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot
// this should be enough..
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

java spring spring-mvc

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

在 Rust 中的线程之间共享函数引用

我想在线程之间共享一个函数引用,但 Rust 编译器说`dyn for<'r> std::ops::Fn(&'r std::string::String) -> std::string::String` cannot be shared between threads safely. 我很了解Send, Sync, 以及Arc<T>在线程之间共享“常规”值时,但在这种情况下,我无法理解问题所在。一个函数在程序运行期间有一个静态地址,因此我在这里看不到问题。

我怎样才能使这项工作?

fn main() {
    // pass a function..
    do_sth_multithreaded(&append_a);
    do_sth_multithreaded(&identity);
}

fn append_a(string: &String) -> String {
    let mut string = String::from(string);
    string.push('a');
    string
}

fn identity(string: &String) -> String {
    String::from(string)
}

fn do_sth_multithreaded(transform_fn: &dyn Fn(&String) -> String) {
    for i in 0..4 {
        let string = format!("{}", i);
        thread::spawn(move || {
            println!("Thread {}: {}", …
Run Code Online (Sandbox Code Playgroud)

rust

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

Rust 会自动将函数返回的所有数据包装在 Box 中吗?

让我们以这个例子为例:

fn hello() -> String {
    String::from("Hello")
}
Run Code Online (Sandbox Code Playgroud)

C 中的一个等效示例是使用 malloc,将字符写入内存并返回指针。为什么 Rust 代码有效?为什么我不必这样写:

fn hello() -> Box<String> {
    Box::from(String::from("Hello"))
}
Run Code Online (Sandbox Code Playgroud)

要在函数内部创建一个值并让该函数返回它,该值必须始终在堆上创建,而不是在堆栈上,这是肯定的。为什么 Rust 使用的语法乍一看就表明您可以返回堆栈变量?

Rust 是否会自动将函数返回的所有数据包装在 a 中Box?或者还有其他一些 Rust 魔法吗?

我的猜测是,不要被迫将返回值包装在Box.

我的问题不仅限于字符串;它是关于从一般函数返回结构。我知道底层向量将其数据存储在堆上。这个问题只是关于结构的元数据(即指向堆上数据的指针)以及函数返回时它们如何返回(在编译器输出中)。

rust

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

标签 统计

rust ×4

alignment ×1

c ×1

heap-memory ×1

java ×1

pointers ×1

spring ×1

spring-mvc ×1

struct ×1