[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

some questions about "new" and "delete"

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
pisselou
Asked:
pisselou
  • 5
  • 4
1 Solution
 
AlexFMCommented:
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
 
AlexFMCommented:
Correction:

s* ps = new s;
ps->n1 = 1;
ps->n2 = 2;
0
 
pisselouAuthor Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
AlexFMCommented:
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
 
pisselouAuthor Commented:
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
 
AlexFMCommented:
"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
 
pisselouAuthor Commented:
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
 
AlexFMCommented:
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
 
pisselouAuthor Commented:
OK. So I have to use the first method.
Thanks a lot.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now