A R
A R

Reputation: 2803

Query in using "char*" variable in c++

I am using char* variable in my cpp code. My query is that should we allocate memory for char* variable using new() or malloc()?

std::string str;
char* chrarray = NULL;
chrarray = str.c_str();

Can anyone tell whether the above code snippet have some memory leak?

What should be the good practice to use such variables?

char* error;
error = dlerror();

I have seen the above code in linux man page for dlopen. Why memory is not allocated for error variable?

Upvotes: 0

Views: 149

Answers (3)

Ferenc Deak
Ferenc Deak

Reputation: 35408

std::string str;
char* chrarray = NULL;
chrarray = str.c_str();

contains no memory leak. chrarray will point to the internal data of the string (regardless that it is empty right now) and highly possible it cannot be trusted after there are modifications to the string.

char* error;
error = dlerror();

will initialize the error variable to the last error of the dl* family of functions. Unless you forget to properly close the dynamic library you used there should be no memory leak... Unless you run in the situation found at Memory leak reported by valgrind in dlopen?

Upvotes: 0

tadman
tadman

Reputation: 211560

You have memory leaks when you allocate memory or assume responsibility for previously allocated memory and fail to de-allocate this memory when you no longer need it.

In your example you're receiving pointers to already allocated structures but you are not assuming responsibility for freeing that memory.

In the case of c_str() you're getting a pointer to the internal memory of the string. If that string falls out of scope and gets destroyed, that pointer is no longer valid. It does not need to be freed since you don't own it. There are likely other things that will invalidate your pointer, so you'll need to be very careful when using pointers like this.

A better way to write this is:

const char* chrarray = str.c_str();

There's no need to initialize the pointer to NULL and then immediately re-write it to something else.

The second case describes what happens in a lot of old C libraries where they will return pointers to shared memory that you do not own. Some of these are not thread safe since all threads will refer to the same block, so read the documentation very carefully when you're receiving pointers. If you are to assume ownership of it, you will have to destroy using the correct method or you will leak.

Upvotes: 3

pankaj
pankaj

Reputation: 1336

You haven't allocated any memory in this snippet, so no question of memory leak I guess

std::string str;
char* chrarray = NULL;
chrarray = str.c_str();

And here is what man page says for dlerror()

dlerror()

The function dlerror() returns a human readable string describing the most recent error that occurred from dlopen(), dlsym() or dlclose() since the last call to dlerror(). It returns NULL if no errors have occurred since initialization or since it was last called.

Which means, you need not allocate memory while using dlerror, the string will be returned back to you. But, you need to take care if the returned value is NULL

Upvotes: 0

Related Questions