Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raw pointers in C++ implementation #410

Open
zigzag312 opened this issue Nov 4, 2023 · 5 comments
Open

Raw pointers in C++ implementation #410

zigzag312 opened this issue Nov 4, 2023 · 5 comments

Comments

@zigzag312
Copy link
Contributor

Rules says no unsafe code blocks. Raw pointers are what other languages consider unsafe. Since smart pointers are available in modern C++, wouldn't replacing raw pointers with smart pointers make the most sense?

@jinyus
Copy link
Owner

jinyus commented Nov 5, 2023

I'm not opposed to that but I'm no C++ expert so that will have to be contributed.

@zigzag312
Copy link
Contributor Author

My knowledge of C++ is rather light as well. I would change RelatedPosts struct like this:

struct RelatedPosts
{
    std::shared_ptr<std::string const> _id;
    std::shared_ptr<std::vector<std::string> const> tags;
    std::array<std::shared_ptr<Post const>, TOPN> related;
};

But then the use of shared_ptr needs to propagate all the way back to the input posts vector. Probably best to wait for someone with more C++ experience to chime in, so we don't create a Frankenstein's monster of an implementation.

@cyrusmsk
Copy link
Contributor

cyrusmsk commented Nov 20, 2023

Btw how differ pointers in Nim, Go and Rust from C++ in that struct?

Go

type RelatedPosts struct {
	ID      string      `json:"_id"`
	Tags    *[]string   `json:"tags"`
	Related [topN]*Post `json:"related"`
} 

Nim

RelatedPosts = ref object
    `"_id"`: string
    tags : ref seq[string]
    related: array[N, ptr Post]

Rust

struct RelatedPosts<'a> {
    #[serde(rename = "_id")]
    id: &'a str,
    tags: &'a [&'a str],
    related: Vec<&'a Post<'a>>,
}

all three are using pointers for tags array and post.. is it safe approach?

@zigzag312
Copy link
Contributor Author

I think Go's default pointers are managed by GC and are therefore safe.

Rust pointers have lifetime 'a attached, so they are also safe?

I don't know about Nim.

@jinyus
Copy link
Owner

jinyus commented Nov 20, 2023

According to https://nim-lang.github.io/Nim/tut1.html#advanced-types-reference-and-pointer-types, ref is GC managed and ptr is not, thus unsafe. That will have to be addressed. @michaelsbradleyjr

kaiwu added a commit to kaiwu/related_post_gen that referenced this issue Sep 8, 2024
kaiwu added a commit to kaiwu/related_post_gen that referenced this issue Sep 8, 2024
jinyus added a commit that referenced this issue Sep 9, 2024
cpp and cpp_con ref #410 and zig improvement
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants