#dereference shared_ptr

73 messages · Page 1 of 1 (latest)

hasty sequoia
#

Hello, did someone knows a video or where I can get more examples about this?
Im confuse because when I see for example "*words[i]" in c this usually will means that you will get a character, but here I get the word.
I ask chat gpt and the answer was because words is of type std::vector<std::shared_ptrstd::string> so using "*words" dereferences the shared pointer and gives you a std::string, someone knows where I can find more examples of this or a video explaning things like this?, thanks in advanced.

stuck hatchBOT
#

When your question is answered use !solved or the button below to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

#

@hasty sequoia

Screenshots!

Your message appears to contain screenshots but no code. Please send code and error messages in text instead of screenshots if applicable!

gleaming saffron
#

Well, you have a vector of shared_ptr where each shared_ptr references a string on the heap. So your explanation is literally in the type definition. So when you dereference words[i] what you're getting back is a string and not a character. However, if you did (*words[i])[0]) for example, you should get back the first letter of the string referenced by words[i]. However, I don't know much C++ as I am primarily a beginner in C. So wait for the experts lol

full jetty
#

probably more suited for #1013107104678162544 (this is #1013104018739974194 )

#

As for the explanation, Sabrina is pretty correct here.

As for the code:

std::vector<std::shared_ptr<std::string>>
```Why would you even want to have a vector of shared pointers to strings in the first place?
full jetty
gleaming saffron
#

Also, words[i] in C does not necessarily mean you will get back a character because it depends on the type of words. For example, in C if words was a char**

gleaming saffron
full jetty
full jetty
gleaming saffron
full jetty
#

[As of C23, if] you dereference [NULL] it does nothing. Your program won't crash.
Really? It'd be very surprising to me if dereferencing NULL would be anything but UB

gleaming saffron
gleaming saffron
full jetty
gleaming saffron
#

I'm a beginner in C so don't take anything I say at any value lol

#

It all comes down to how aggressive you want to be in your safety checks

full jetty
gleaming saffron
full jetty
# gleaming saffron Also, `words[i]` in `C` does not necessarily mean you will get back a character ...

Two things that can definitely be improved however:

  • You don't need to cast a void * to T * (or vice versa), i.e. you don't need to cast the result of malloc and
  • instead of doing:
char **words = malloc(sizeof(char *) * 2);
```you can do: 
```c
char **words = malloc(sizeof(*words) * 2);
```This way whenever you change the datatype of `words`, it will automatically update itself appropriately in the `sizeof` expression
gleaming saffron
#

I actually didn't know that.

#

Will write it in my notes.

gleaming saffron
# hasty sequoia *words[i], not words[i]

My point is still valid. It depends on the type of words. In your example, std::vector < std::shared_ptr< std::string > > is a different type from an array of strings in C. words[i] returns a shared_ptr. *words[i] returns a string NOT a character

hasty sequoia
#

I was expecting something like this

#

``

#

int main()
{
char** myText = malloc(sizeof(char) * 128);
(ptr)myText = "Hello this is a test";
printf("%c\n", *myText[0]);
return 0;
}

#

``

#

i know shared_ptr. *words[i] return a string and not a char but I want to know why

full jetty
# hasty sequoia ``

3 backticks, and they need to be in the same message as the code:

```
YOUR CODE
```

Optionally exchange the first 3 backticks for ```c
to have C highlighting or cpp for C++ highlighting, etc

hasty sequoia
#

thank you

#

int main()
{
    char **myText =  malloc(sizeof(char) * 128);
    
*myText = "Hello this is a test";
    printf("%c\n", *myText[0]);
    return 0;
}
full jetty
hasty sequoia
#

of yeah, size of char *

gleaming saffron
hasty sequoia
#

Yes thats why I was expecting to get a char to with *Words[i]

stuck hatchBOT
#

@hasty sequoia Has your question been resolved? If so, type !solved :)

hasty sequoia
#

so thiis with vector

#

will be

#

int main()
{
    char **myText[1]; /* = malloc(sizeof(char**) * 128);*/
    *myText = malloc(sizeof(char) * 128);

    *myText[0] = "Hello this is a test";
    printf("%s\n", *myText[0]);
    return 0;
}
#

something like this?

gleaming saffron
# hasty sequoia Yes thats why I was expecting to get a char to with `*Words[i]`

You wouldn't because you're trying to equate std::vector < std::shared_ptr< std::string > > with a char**. *Words[0] means you first access the shared_ptr located at index 0 of the vector since subscript has higher precedence. Then you dereference that specific shared_ptr which returns a string object. So you have to dereference once again to get the character in your example *(*Words[i])

hasty sequoia
#

with char**[1] i trying to look this as *** instead of a double ptr, if see that in this way make sense to me

#

so i thinking a double pointer for the string and another pointer for the vector

gleaming saffron
hasty sequoia
#

yes

#

so that of vector<shared_ptr<string>>

#

is basically an array where you have a lot of string so a char**

#

well not an array, an a vector

gleaming saffron
#

Why are you trying to emulate std::vector < std::shared_ptr< std::string > > in C?

hasty sequoia
#

but i suppose thats why you get a string with *Words[0] Im pointing to the first element of the vector

hasty sequoia
#

to understand what It supposed to do this feature

#

I think I need to implement my own vector and my own shared_ptr

#

Thank youu Sabrina and thank you Monke, Thinks he does java

#

I think I understand a little more:D

full jetty
hasty sequoia
#

not only smartpointers, also vectors

full jetty
#

I.e. std::unique_ptr and std::shared_ptr + std::weak_ptr?

hasty sequoia
#

specifically shared_ptr and vector

#

In the book they only see shared_ptr but im pretty sure that most of the time I will see unique_ptr for my toy projects

full jetty
#

std::shared_ptr is honestly really difficult. So far, I have never used it myself, I didn't even need to use it for any homework exercise when I was learning C++.

gleaming saffron
#

You need to understand the concept of indirection that is your fundamental issue. It has nothing to do with vector or shared_ptr. Every time you dereference, ask yourself what object you are accessing in memory.

gleaming saffron
full jetty
gleaming saffron
#

Memory management without a garbage collector

full jetty
gleaming saffron
full jetty
#

ah, does that have to do with these "safe" & "unsafe" modes in Rust?

#

(that "unsafe" thing is about the only thing I know about the language)