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
Solved

some questions about "new" and "delete"

Posted on 2004-10-17
9
237 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

861 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