By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,193 Members | 826 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,193 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Sims" <si*********@hotmail.com> wrote in message
news:bp*************@ID-162430.news.uni-berlin.de...
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
Jul 22 '05 #2

P: n/a
Sims wrote:
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

Jul 22 '05 #3

P: n/a

Should be delete [] sz;
indeed, i should have tested it first. :)

Regards,
Robi


Sims
Jul 22 '05 #4

P: n/a

"Sims" <si*********@hotmail.com> wrote in message
news:bp*************@ID-162430.news.uni-berlin.de...

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
Jul 22 '05 #5

P: n/a

"Sims" <si*********@hotmail.com> wrote in message
news:bp*************@ID-162430.news.uni-berlin.de...
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
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.