473,395 Members | 1,670 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

Aug 17 '05 #1
13 1644
* 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.

--
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?
Aug 17 '05 #2
> 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

Aug 17 '05 #3
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)

Aug 17 '05 #4
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" <pe*******@hotmai.com> wrote in message
news:dd********@netnews.net.lucent.com...
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

Aug 17 '05 #5
> 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

Aug 17 '05 #6
Franklin Li wrote:
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
Aug 17 '05 #7
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

Aug 17 '05 #8
> 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

Aug 17 '05 #9

Srini wrote:
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

Aug 17 '05 #10
> > 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


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

Aug 17 '05 #11

Srini wrote:
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


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

Aug 18 '05 #12
> 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

Aug 18 '05 #13
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
Aug 19 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Philip Lawatsch | last post by:
Hi, I have some questions about whats written in http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.14 (Describing some memory pool) #1 From what i understand this will also work for new...
2
by: DarkSpy | last post by:
Question 1: class A { public: operator int () { return 10; } operator int () const { return operator int(); } operator std::string() { return "abcd"; } operator std::string() const { return...
8
by: Randy Gordon | last post by:
Say I do the following: char **names = new char*; for (int i = 0; i < 100; i++) { names = new char; } When I'm done with the array of character pointers, how should I delete it? Like this:
11
by: Lloyd Dupont | last post by:
(not I use 2.0, so new return a "normal" pointer and gcnew return a managed one, my question below regarding new concern plain standart C++ allocator) - if I use the default new operator, are all...
9
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new...
15
by: cedgington | last post by:
I wanted to take advantage of the large set of functionality offered by the framework, so for my latest project I'm using managed C++ with .NET v2. I'm using the gcnew operator in two different...
1
by: Gonçalo Rodrigues | last post by:
Hi all, I am a little confused about the delete operator, so I have a question. Suppose we have something like class Base { public: void* operator new(std::size_t size); void operator...
17
by: SoftEast | last post by:
Hi Buddies, I have read a lot of stuffs regarding not using GOTO statements to opt a good programming style http://david.tribble.com/text/goto.html]. Can anybody give a particular lines of code...
7
by: Carlo Milanesi | last post by:
Hello, I just completed writing an online book about developing efficient software using the C++ language. You can find it here: http://en.wikibooks.org/wiki/Optimizing_C%2B%2B It is a wiki, that...
7
by: EManning | last post by:
Using A2003. I'm receiving this error when returning from a "DoCmd.OpenReport..." statement. I have a tab control with a subform on every tab. The user selects an item from a combobox at the top...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.