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.
#dereference shared_ptr
73 messages · Page 1 of 1 (latest)
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!
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
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?
Also: If you are a beginner, which you are, DO NOT USE LLMs!!!!
LLMs are incredibly stupid, but answer with complete confidence even when wrong. Since you are a beginner you don't know when you're being lied to by the LLM.
Especially in C++, which is a very complicated language, it's not only common for LLM answers to be wrong/incorrect, but it's actually expected for them to be wrong
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**
From the example, I think I can tell it is used for purposes of memory efficient sorting. So instead of swapping the string values all you do is swap the pointer addresses. That way when you iterate through the vector you get back the vector in sorted order. Honestly, it seems like more of an academic exercise.
you don't even need to set words to NULL after free-ing it, but at the same time it doesn't make it incorrect, it's just redundant
Yeah, but a std::vector internally uses std::unique_ptrs already, so swapping elements is easy/efficient anyway.
Also that does not explain why std::shared_ptrs are used over std::unique_ptrs
You should always set your pointer to nullptr because if you later dereference words again by accident you'll get an error. If you dereference nullptr it does nothing. Your program won't crash. But this is as of C23
[As of C23, if] you dereference [NULL] it does nothing. Your program won't crash.
Really? It'd be very surprising to me if dereferencingNULLwould be anything but UB
Again, it's just an academic exercise on the use of pointers lol. I would also use std::unique_ptr.
It is better to now use nullptr because the NULL macro is defined differently in different headers. In some places it's 0 and others it's \0 or (void*)0. So nullptr is consistent.
Okay, but dereferencing that is still UB, isn't it?
Unfortunately, yes. It's still best practice to set a free'd allocation to nullptr or NULL simply because you can still perform a null check later.
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
*words[i], not words[i]
I'm a beginner in C
You still have valid takes with proper explanations, so all good
In this case setting it to nullptr is not required because the local variable would've been destroyed immediately afterwards anyway, but yeah, if the pointer would continue to exist afterwards this might not be a bad decision
Oh, I always assume someone might come back and add more to the code later lol. I guess it's just a maintenance choice.
Two things that can definitely be improved however:
- You don't need to cast a
void *toT *(or vice versa), i.e. you don't need to cast the result ofmallocand - 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
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
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
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
thank you
int main()
{
char **myText = malloc(sizeof(char) * 128);
*myText = "Hello this is a test";
printf("%c\n", *myText[0]);
return 0;
}
char **myText = malloc(sizeof(char) * 128);
```you do realize what's wrong here yourself, don't you?
of yeah, size of char *
I see what you're trying to do. Yes, *myText[0] will indeed return a character. There are actually 2 levels of indirection here. myText[0] returns a pointer to the first character to the const char* "hello this is a test". Then you dereference that memory location which returns the char H.
Yes thats why I was expecting to get a char to with *Words[i]
@hasty sequoia Has your question been resolved? If so, type !solved :)
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?
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])
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
I don't think you understand what you did here. What you've created as an array of one element. That one element itself is an array of 128 char*
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
Why are you trying to emulate std::vector < std::shared_ptr< std::string > > in C?
but i suppose thats why you get a string with *Words[0] Im pointing to the first element of the vector
yes
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
Wdym by "this feature"? You mean smart pointers?
not only smartpointers, also vectors
I.e. std::unique_ptr and std::shared_ptr + std::weak_ptr?
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
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++.
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.
C++ was trying to do what Rust ended up achieving lol
I've never used Rust, what did it do/invent?
Came out with a novel memory model through ownership and lifetimes.
Memory management without a garbage collector
C++ has RAII and also doesn't have a GC, so how is Rust different ?
Rust will not compile memory errors. Rust is able to check for memory issues at compile time. I think at least that's the biggest difference.