Solved

some questions about "new" and "delete"

Posted on 2004-10-17
9
222 Views
Last Modified: 2010-04-01
Hello everybody.
I have a few questions (I'm a beginner in C++)

1)when you have this code:
°°°°°°°°°°°°°°°°°°°°°°°°°°°°
typedef struct _STRING
{
      int *                  buffer;
      unsigned long      length;
}_STRING, *P_STRING;
°°°°°°°°°°°°°°°°°°°°°°°°°°°°
P_STRING p1 = new _STRING;
p1->buffer = new int[p1->length];
°°°°°°°°°°°°°°°°°°°°°°°°°°°
how should you delete the pointer ?
 (->first the buffer and then the _STRING or only the _STRING)

2)When I know a priori the maximal length, is it faster to use values on the stack in stead of working with new and delete ?
I heard that this is the case, if your stack doesn't becomes to big (but I don't know when that's the case... (and I don't know if this is correct)

3)when you make sth with new, and you want to reuse it later, how should you reinitialise the "allocated memory" (is this term correct?) to 0x00's ??

Thanks in advance.


0
Comment
Question by:pisselou
  • 5
  • 4
9 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 12331512
1)

P_STRING p1 = new _STRING;
p1->length = 10;
p1->buffer = new int[p1->length];

// use p1 ...

// release
delete[] p1->buffer;
delete p1;

2) Yes, allocation on stack is faster. Very large arrays should be allocated dynamically, because stack size is restricted. Usually heap has more size.

3) I am not sure what you mean, possibly this:

struct s
{
    int n1;
    int n2;
};

s* ps = new s;
s->n1 = 1;
s->n2 = 2;

...

memset((void*)ps, 0, sizeof(s));    // set all structure to 0
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 12331514
Correction:

s* ps = new s;
ps->n1 = 1;
ps->n2 = 2;
0
 

Author Comment

by:pisselou
ID: 12331529
Thanks for your answers.
For the third question, I mean:
___
buffer->set a value to the buffer
reinitialise it, so that I can set a new value to the buffer.
___
I think that it's indeed done with memset, but is this the c++ solution also (I thought that this was only used in c).
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 12331551
Maybe this?

P_STRING p1 = new _STRING;
p1->length = 10;
p1->buffer = new int[p1->length];

// use p1 ...

p1->length = 20;
delete[] p1->buffer;
p1->buffer = new int[p1->length];

// use p1 ...

// release
delete[] p1->buffer;
delete p1;
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:pisselou
ID: 12331562
Thanks. I think I use that method (however, doesn't this take longer than the other method, since you first "throw away" your allocated memory and then have to search it again (you=the computer)) ??
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 12331572
"Throwing away" of allocated memory creates memory leaks. All allocated memory should be explicitly released. Memory leaks are released by operating system only when program exits. When process with memory leaks runs a long time, it consumes more and more memory and finally crashes. Good programming style requires writing a programs without memory leaks.
memset may be used for structures which don't contain pointers.
0
 

Author Comment

by:pisselou
ID: 12331632
sorry for my ignorance...
but I thought that it is like that, maybe you can correct me (if you have the time...):

method 1:
"delete[] p1->buffer;" ->releases the memory which was reserved for the buffer (so "throws it away" ??).
"p1->buffer = new int[p1->length];"->searches new memory for the buffer, and initializes it.
method 2:
"memset((void*)ps, 0, sizeof(s));"->just set's the memory (you allready searched) to 0x00's (so, you don't have to search it again, since you only reïnitialize it).

So the second method should be faster (??) (my program should be fast, so maybe it makes a difference...)

Is this all correct??

Thanks again for all your help.



0
 
LVL 48

Accepted Solution

by:
AlexFM earned 250 total points
ID: 12331649
delete[] p1->buffer;
p1->buffer = new int[p1->length];

This is right way (release buffer and allocate it again with new length).

memset((void*)p1, 0, sizeof(_STRING));

This is wrong way, because it creates memory leak. p1->buffer address is lost and not released. memset may be used only for structures without pointers (like struct s from my first post).
0
 

Author Comment

by:pisselou
ID: 12331668
OK. So I have to use the first method.
Thanks a lot.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now