简体   繁体   中英

Using char * in a struct or collection

I'm a little confused how a char * is handled in memory for a collection or struct, that is, what is the responsibility to manage the memory. I've heard that in general it is not wise to rely on resource management and pointers when you have classes like string available instead.

std::tr1::unordered_map<char*, Vars> elements;

or

struct example{
 char*name;
}

If you are technically passing a pointer to a character array, then this implies the "owning" collection or struct does not manage the memory for that piece of text, but rather just a pointer to it. So is it possible for that text to disappear from memory and the pointer to dangle?

For example:

char * text="yo";
example myStruct;
myStruct.name=text; // how safe is this?

A bare pointer does not encode any information about ownership/responsibility of deallocation. It all depends on where the pointer/ struct came from. This is exactly why smart pointers and classes like std::string exist.

As for the example,

char * text="yo";

This is deprecated, and it has nothing to do with memory allocation. Use a char const* to point to a literal. It is not possible for this string to disappear in a well-behaved program; most likely, it lives in read-only memory.

what is the responsibility to manage the memory

That's very simple - a raw pointer never manages memory. It simply tells you where an object is in memory.

So is it possible for that text to disappear from memory and the pointer to dangle?

Absolutely, which is why you should avoid using raw pointers if possible, and be careful when you do.

If you need an "owning" reference to an object, then use smart pointers or containers to manage the object's lifetime. If you need a "non-owning" reference, then either use something like std::weak_ptr , or arrange things carefully so that the object can't be destroyed while that reference is still in use.

Your final example is fine because the pointer refers to "yo" , a string literal. This lasts until the end of the program, so the pointer will never be left dangling. (Although it's worth pointing out that string literals are constant, so you shouldn't use a non- const pointer to refer to one, even though the language does let you do that for weird historical reasons).

In your particular case you are not allocating memory, but pointing to a location in the read-only part of your program containing the string "yo". You wouldn't need to free this memory.
Whereas if you had

char * text= new char[10];
memset(text,0,10);
strncpy(text,3, "yo");
example myStruct;
myStruct.name=text; // how safe is this?

you would be responsible for deallocating the memory used by text . There is no "ownership", if you want to deallocate the memory using the struct then this fine. Just make sure to to touch the pointer afterwards as it might trigger undefined behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM