Need to free memory?

C

Curious

I have code below:

char Cities[128], States[12];

Before I exit the method, do I need to

delete Cities;
delete States;

in order to free the space? Thanks!
 
R

Rolf Magnus

Curious said:
I have code below:

char Cities[128], States[12];

Before I exit the method, do I need to

delete Cities;
delete States;

in order to free the space? Thanks!

No. You must not delete anything you didn't get from new.
 
D

Default User

Curious said:
I have code below:

char Cities[128], States[12];

Before I exit the method, do I need to

delete Cities;
delete States;

in order to free the space? Thanks!

No. If it wasn't allocated with new, then it isn't deallocated with
delete. And it would be delete[] anyway.




Brian
 
C

Curious

Thanks Brian for the comment!

However, I heard that

char Cities[128];

is equivalent to

char *Cities = new char[128];

And therefore, it should be freed with the following:

delete Cities[];

?
 
I

Ian Collins

Curious said:
Thanks Brian for the comment!

However, I heard that

char Cities[128];

is equivalent to

char *Cities = new char[128];
You heard wrong. The two are completely different.

It looks like you should get a better book!
 
D

Default User

Curious said:
Thanks Brian for the comment!

However, I heard that

char Cities[128];

is equivalent to

char *Cities = new char[128];

It's not. If a person told you that, never listen to them again. If a
book told you that, burn it.

In the first example, Cities[128] is an array of type 128 of char. In
the second, Cities is a pointer to char. In the first, the memory
represented by the array can't be resized or relocated. In the second
it can.

There are many other differences. You'd be better off using std::vector
for the most part.




Brian
 
D

daniel

I have code below:

               char Cities[128], States[12];

Before I exit the method, do I need to

               delete Cities;
               delete States;

in order to free the space? Thanks!

if you are allocating Cities[128] in a function , then Cities will be
placed on the stack
and it will be automatically deleted at function end.
 

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
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top