about delete. | | |
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 | | | | re: about delete.
* Franklin Li:[color=blue]
>
> I find one example codes as below:
>
> int send()
> {
> char msg[3000];
> ...
> delete [] msg;
> ..
> }
>
> For my understanding, there should be no delete.[/color]
There should be no delete _if_ 'msg' in the delete statement refers to the
same 'msg' declared earlier.
[color=blue]
> But the codes are still work. ( I use CC 6.1a).[/color]
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.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? | | | | re: about delete.
> Hi All,[color=blue]
>
> 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[/color]
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 | | | | re: about delete.
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) | | | | re: about delete.
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
"Franklin Li" <pengtaoli@hotmai.com> wrote in message
news:ddutuc$qit@netnews.net.lucent.com...[color=blue]
> 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
>
>
>[/color] | | | | re: about delete.
> More detail for the function:[color=blue]
>
> 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[/color]
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 | | | | re: about delete.
Franklin Li wrote:[color=blue]
> 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?
>[/color]
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 | | | | re: about delete.
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 | | | | re: about delete.
> How can I change it? change as below is better than just delete "delete [][color=blue]
> msg;".
>
> int send(void *desc, int size) {
>
> char* msgPtr = new char[3000];
> ...
> memcpy((void *)msgPtr, (void *)desc, size);
> ...
> delete [] msgPtr;
> ...
>
>}
>
> Thanks.
>
> Franklin[/color]
This would do - or...
int send(void* desc, int size) {
char msg[3000];
// ...
memcpy(msg, desc, size);
// ...
// no delete required
}
Srini | | | | re: about delete.
Srini wrote:[color=blue][color=green]
> > 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[/color]
>
> This would do - or...
>
> int send(void* desc, int size) {
> char msg[3000];
> // ...
> memcpy(msg, desc, size);
> // ...
> // no delete required
> }
>
> Srini[/color]
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 | | | | re: about delete.
> > int send(void* desc, int size) {[color=blue][color=green]
> > char msg[3000];
> > // ...
> > memcpy(msg, desc, size);
> > // ...
> > // no delete required
> > }[/color]
>[color=green]
> > Srini[/color]
>
> 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[/color]
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 | | | | re: about delete.
Srini wrote:[color=blue][color=green][color=darkred]
> > > int send(void* desc, int size) {
> > > char msg[3000];
> > > // ...
> > > memcpy(msg, desc, size);
> > > // ...
> > > // no delete required
> > > }[/color]
> >[color=darkred]
> > > Srini[/color]
> >
> > 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[/color]
>
> 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[/color]
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 | | | | re: about delete.
> Um, I believe C99 and the gcc compiler support dynamically-sized arrays[color=blue]
> 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.
>[/color]
Yes, c99 and gcc support dynamic size arrays. And its not standard C++.
Dynamic allocation would be better in such a case.
[color=blue]
> 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[/color]
I apologise. I'll be more careful from now.
Srini | | | | re: about delete.
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 |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|