Solved

some questions about "new" and "delete"

Posted on 2004-10-17
9
213 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

707 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

20 Experts available now in Live!

Get 1:1 Help Now