Hi all,

in a piece of code like this:

void Logger::logMsg( int msgLevel,const char* msg)
	char input[strlen(msg)];

is the input array dynamically allocated?
In this case, do I have to use the delete [] in order not to have a memory leak?

thank you!

Edited by teomurgi: n/a

7 Years
Discussion Span
Last Post by vmanes

You might need to use a dynamic allocation which would then require delete[]. I don't think the compiler will let you do that the way you have because I don't think strlen(msg) is technically considered a constant.

It looks like you want to copy the message before sending it to the log. Depending on what you intend to do, you might want to avoid that step entirely. Simply send msg directly to the log rather than copying it to a new message then sending it to the log, it's a constant anyway so you're not going to hurt it.

Edited by Fbody: n/a


is the input array dynamically allocated?
In this case, do I have to use the delete [] in order not to have a memory leak?

Not really. And strictly speaking, that is not a valid declaration in C++, you should get a compiler error as the array size must be a constant (a literal value or a const variable - argh, I don't like that description.) It must be a value known at compile time, and not changeable.

However, the new C standard does allow such array construction, and some C++ compilers are beginning to support that. If it works in your compiler, you shouldn't need a delete as it's a variable local to the function and will be removed from the stack when the function returns.


Thx for the reply,

my compile (g++ 4.1.3) seems to be fine with it.
I'll use a big constant to avoid the problem, but I'm still really curious
about how the memory is allocated in this particular case.

My opinion is that it is statically allocated on the stack but I'm not sure at all....


g++ is one compiler that is jumping the gun on the variable size array declarator.

"static" and "stack" are two words that generally don't go together. static variables are not located on the stack (nor on the heap, as I'd always thought. Narue, I believe, put out a very good discussion on this a while back.)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.