about delete.

F

Franklin Li

Hi All,

I find one example codes as below:

int send()
{
char msg[3000];
...
delete [] msg;
..
}

For my understanding, there should be no delete. But the codes are still
work. ( I use CC 6.1a).

Who can tell me the reason and if the codes are dangous for memory link?

Thanks.

Franklin
 
A

Alf P. Steinbach

* Franklin Li:
I find one example codes as below:

int send()
{
char msg[3000];
...
delete [] msg;
..
}

For my understanding, there should be no delete.

There should be no delete _if_ 'msg' in the delete statement refers to the
same 'msg' declared earlier.

But the codes are still work. ( I use CC 6.1a).

Under the assumption above it's undefined behavior, and it's UB of the sort
that really counts, which means it can apparently work -- until Something
Bad (TM) happens, which can seemingly be somewhere else and for no reason.
 
S

Srini

Hi All,
I find one example codes as below:

int send()
{
char msg[3000];
...
delete [] msg;
..

}

For my understanding, there should be no delete. But the codes are still
work. ( I use CC 6.1a).

Who can tell me the reason and if the codes are dangous for memory link?

Thanks.

Franklin

This is illegal. You cannot delete stuff statically allocated on stack.
The compiler might give a warning in this regard. But the runtime
behavior is undefined. *Anything* could happen - but its plain bad
code!

Srini
 
K

kolari

I think it may be because , in C array are are like pointers . where
name will have pointing base address. may be because of this the
compilar is not giving any error.

hint:a[1] =*(a+1)
 
F

Franklin Li

More detail for the function:

int send(void *desc, int size)
{
char msg[3000];
char* msgPtr;
msgPtr = msg;
...
memcpy((void *)msgPtr, (void *)desc, size);
...
delete [] msg;
...
}

Should I remove delete line to avoid potential problem?

Thanks.

Franklin
 
F

Franklin Li

How can I change it? change as below is better than just delete "delete []
msg;".

int send(void *desc, int size)

char* msgPtr = new char[3000];
...
memcpy((void *)msgPtr, (void *)desc, size);
...
delete [] msgPtr;
...
}


Thanks.

Franklin
 
S

Srini

More detail for the function:
int send(void *desc, int size)
{
char msg[3000];
char* msgPtr;
msgPtr = msg;
...
memcpy((void *)msgPtr, (void *)desc, size);
...
delete [] msg;
...

}

Should I remove delete line to avoid potential problem?

Thanks.

Franklin

Yes, its better you remove the delete statement. As Alf here has
pointed out, make sure that there are no other identifiers named 'msg'
that the delete in the function might be referring to.

Srini
 
J

John Ratliff

Franklin said:
More detail for the function:

int send(void *desc, int size)
{
char msg[3000];
char* msgPtr;
msgPtr = msg;
...
memcpy((void *)msgPtr, (void *)desc, size);
...
delete [] msg;
...
}

Should I remove delete line to avoid potential problem?

Yes. You could also remove msgPtr and just use msg in the memcpy. It
would implicitly cast it for you, or you could add an explicit
static_cast if you don't like that.

--John Ratliff
 
S

Srini

How can I change it? change as below is better than just delete "delete []
msg;".

int send(void *desc, int size) {

char* msgPtr = new char[3000];
...
memcpy((void *)msgPtr, (void *)desc, size);
...
delete [] msgPtr;
...

}

Thanks.

Franklin

This would do - or...

int send(void* desc, int size) {
char msg[3000];
// ...
memcpy(msg, desc, size);
// ...
// no delete required
}

Srini
 
G

Greg

Srini said:
How can I change it? change as below is better than just delete "delete []
msg;".

int send(void *desc, int size) {

char* msgPtr = new char[3000];
...
memcpy((void *)msgPtr, (void *)desc, size);
...
delete [] msgPtr;
...

}

Thanks.

Franklin

This would do - or...

int send(void* desc, int size) {
char msg[3000];
// ...
memcpy(msg, desc, size);
// ...
// no delete required
}

Srini

Except of course you have just set the program up for a classic buffer
overrun problem whenever size > 3000.

There are advantages to dynamically allocated memory: one is that it
can be sized at runtime to fit the data, and another is that the memory
allocated is not (usually) on the stack.

Greg
 
S

Srini

int send(void* desc, int size) {
char msg[3000];
// ...
memcpy(msg, desc, size);
// ...
// no delete required
}

Except of course you have just set the program up for a classic buffer
overrun problem whenever size > 3000.

There are advantages to dynamically allocated memory: one is that it
can be sized at runtime to fit the data, and another is that the memory
allocated is not (usually) on the stack.

Greg

Ya, I thought about it - but the other code is also dynamically
allocating just 3000 chars. Even it suffers from the same problem. I
just avoided the dynamic allocation which may have caused performance
problems in case the function is heavily called.

int send(void *desc, const int size)
{
char msg[size + 1];
// or
// char *msg = new char[size + 1];
// ...
memcpy(msg, desc, size);
// ...
// If dynamically allocated
// delete []msg;
}

Srini
 
G

Greg

Srini said:
int send(void* desc, int size) {
char msg[3000];
// ...
memcpy(msg, desc, size);
// ...
// no delete required
}

Except of course you have just set the program up for a classic buffer
overrun problem whenever size > 3000.

There are advantages to dynamically allocated memory: one is that it
can be sized at runtime to fit the data, and another is that the memory
allocated is not (usually) on the stack.

Greg

Ya, I thought about it - but the other code is also dynamically
allocating just 3000 chars. Even it suffers from the same problem. I
just avoided the dynamic allocation which may have caused performance
problems in case the function is heavily called.

int send(void *desc, const int size)
{
char msg[size + 1];
// or
// char *msg = new char[size + 1];
// ...
memcpy(msg, desc, size);
// ...
// If dynamically allocated
// delete []msg;
}

Srini

Um, I believe C99 and the gcc compiler support dynamically-sized arrays
as used in your example, but it's not part of the C++ standard as far
as I am aware. Besides, there would still be a problem for very large
values of size. At least a heap-based allocation will return 0 and the
program can deal with the allocation failure, in the case of a stack
based allocation that fails, the program will simply crash.

Since you had noticed the flaw in the original question, you answer
would have been more constructive had you pointed it out. Otherwise how
is the poster going to know it is there? We should not be posting
examples with problems that we would not have in our own code, and
which we neglect to point out. Doing so just teaches bad programming.

Greg
 
S

Srini

Um, I believe C99 and the gcc compiler support dynamically-sized arrays
as used in your example, but it's not part of the C++ standard as far
as I am aware. Besides, there would still be a problem for very large
values of size. At least a heap-based allocation will return 0 and the
program can deal with the allocation failure, in the case of a stack
based allocation that fails, the program will simply crash.

Yes, c99 and gcc support dynamic size arrays. And its not standard C++.
Dynamic allocation would be better in such a case.
Since you had noticed the flaw in the original question, you answer
would have been more constructive had you pointed it out. Otherwise how
is the poster going to know it is there? We should not be posting
examples with problems that we would not have in our own code, and
which we neglect to point out. Doing so just teaches bad programming.

Greg

I apologise. I'll be more careful from now.

Srini
 
F

Franklin Li

Hi Srini,

char msg[size + 1];

This sentence is wrong. We can't define one array using dynamic size.

I decide change to below:

int send(void *desc, int size)

char* msgPtr = new char[size+1];
...
memcpy((void *)msgPtr, (void *)desc, size);
...
delete [] msgPtr;
...
}


Thanks for all of you!

Best!

Franklin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top