char [] vs char* who is doing the cleaning up?

S

Sims

Hi,

If i have something like

void foo1()
{
char sz[10];
strcpy( sz, "123456789" );
}
//
// and
//
void foo2()
{
char *sz = new char[10];
strcpy( sz, "123456789" );
delete sz; sz = NULL;
}

In foo2() I allocate memory and de-allocate it later, so i know what is
going on.
But in foo1(), when does it get de-allocated?
Is declaring char sz[10] within a function implying that the compiler will
release the memory on exiting the function?

Is using void foo2() better practice than using void foo1()?

Thanks for your input.

Sims
 
M

Mike Wahler

Sims said:
Hi,

If i have something like

void foo1()
{
char sz[10];
strcpy( sz, "123456789" );
}

//
// and
//
void foo2()
{
char *sz = new char[10];
strcpy( sz, "123456789" );
delete sz; sz = NULL;
}

In foo2() I allocate memory and de-allocate it later, so i know what is
going on.
But in foo1(), when does it get de-allocated?

When it goes out of scope (in this case at the end of
the function). Look up 'automatic storage duration'.

Is declaring char sz[10] within a function implying that the compiler will
release the memory on exiting the function?
Yes.


Is using void foo2() better practice than using void foo1()?

I recommend to 'keep it simple'. Only use dynamic allocation
when necessary, e.g. if you don't know until run-time how
large the array is to be. And I'd also prefer a container
(e.g. a vector) over an array anyway, since a container will
handle its own memory management automatically.

-Mike
 
R

Robert Sabocanec

Sims said:
Hi,

If i have something like

void foo1()
{
char sz[10];
strcpy( sz, "123456789" );
}
//
// and
//
void foo2()
{
char *sz = new char[10];
strcpy( sz, "123456789" );
delete sz; sz = NULL;
}

Should be delete [] sz;

Regards,
Robi
 
M

Mike Wahler

Sims said:
Should be delete [] sz;

indeed, i should have tested it first. :)

A 'test' would not really have proved anything, since
what you had would cause 'undefined behavior', which
might or might not have manifested itself, and might
or might not have the same behavior every time it was
run. Use containers. :)

-Mike
 
S

Sridhar

Sims said:
Hi,

If i have something like

void foo1()
{
char sz[10];
strcpy( sz, "123456789" );
}
//
// and
//
void foo2()
{
char *sz = new char[10];
strcpy( sz, "123456789" );
delete sz; sz = NULL;
}

In foo2() I allocate memory and de-allocate it later, so i know what is
going on.
But in foo1(), when does it get de-allocated?
Is declaring char sz[10] within a function implying that the compiler will
release the memory on exiting the function?

Whenever you declare a local variable(and arrays), it is allocated in stack,
whereas the dynamically allocated memory are allocated in heap. Since local
variables are allocated in stack, they will be popped up (de-allocated) upon
exiting the block. So it is always best to have static local arrays unless
if there is any specific need to dynamically resize the array.
Is using void foo2() better practice than using void foo1()?

- Sridhar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,833
Members
47,380
Latest member
AlinaBlevi

Latest Threads

Top