One question about array memory alloct&free

W

wavelet

If I new one array struct.How can I
easily free the whole array when I don't
need it? Have to delete one by one?Does
it related to compiler? Such as VC vs g++?

eg:
#include <stdio.h>
#include <stdlib.h>
main()
{
struct node
{
int i;
struct node *p;
};

struct node *q,*q1;
q=new struct node[10]; --->How can I free the array later?
q1=q+1;

q->i=1;
(q+1)->i=2;

delete q;

printf("\nq->i=%d",q->i); --->Here is a snap,right?
printf("\nq1->i=%d",q1->i); --->Can here access and get correct value?why

}
 
M

Mike Wahler

wavelet said:
If I new one array struct.How can I
easily free the whole array when I don't
need it? Have to delete one by one?Does
it related to compiler? Such as VC vs g++?

eg:
#include <stdio.h>
#include <stdlib.h>
main()
{
struct node
{
int i;
struct node *p;
};

struct node *q,*q1;
q=new struct node[10]; --->How can I free the array later?

Just as you do below: delete q;
q1=q+1;

q->i=1;
(q+1)->i=2;

delete q;

printf("\nq->i=%d",q->i); --->Here is a snap,right?
printf("\nq1->i=%d",q1->i); --->Can here access and get correct value?

This will print the value stored at q[1]. Only you know
if that's the 'correct' value.

Because that's what you told it to do. :)

What I don't understand is why you're using those pointers at all.
You can access any of your array items by indexing the array name
'q', e.g. q[0].

-Mike
 
J

Jakob Bieling

struct node *q,*q1;
q=new struct node[10]; --->How can I free the array later?

Just as you do below: delete q;

That should be delete[], if I'm not mistaken. q was allocated with
new[], so you must use delete[].
q1=q+1;

q->i=1;
(q+1)->i=2;

delete q;

printf("\nq->i=%d",q->i); --->Here is a snap,right?
printf("\nq1->i=%d",q1->i); --->Can here access and get correct
value?

This will print the value stored at q[1]. Only you know
if that's the 'correct' value.

Actually, this is undefined behaviour, since q (thus also the memory
q1 is pointing to) was already freed (assuming we got rid of the bug
above).

regards
 
B

ben

That should be delete[], if I'm not mistaken. q was allocated with
new[], so you must use delete[].

I think what the OP wants to do is to delete the first element in the array,
which is pointed by q, without deleting subsequent elements.

ben
 
B

ben

Arrays are not designed to be used that way. Usually you create a array and
dispose the whole array afterwards.

If you want to delete some part of a collection of objects, try std::list.

ben
 
J

Jakob Bieling

ben said:
That should be delete[], if I'm not mistaken. q was allocated
with new[], so you must use delete[].

I think what the OP wants to do is to delete the first element in the
array, which is pointed by q, without deleting subsequent elements.

But that is something you cannot do with new[] and delete[].

regards
 

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

Forum statistics

Threads
474,294
Messages
2,571,507
Members
48,193
Latest member
DannyRober

Latest Threads

Top