Solved

some questions about "new" and "delete"

Posted on 2004-10-17
9
247 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

738 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