473,396 Members | 1,967 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,396 software developers and data experts.

Allocation / Delete problems

Hi,

I have created a DLL which the user is complaining:

"Even when it works ok, my application stays instable (like something is
allocated or deleted in the wrong way)."

What does this mean? The way I have been allocating is:

int * pNew;
pNew = new int;

And deletion:

delete pNew;
pNew = NULL; /* what woudl happen here if i left this line out? or pNew = 0;
*/

Are there any problems with this code?
Jul 22 '05 #1
2 1239
Hamish Dean wrote:
Hi,

I have created a DLL which the user is complaining:

"Even when it works ok, my application stays instable (like something is
allocated or deleted in the wrong way)."

What does this mean? The way I have been allocating is:

int * pNew;
pNew = new int;

And deletion:

delete pNew;
pNew = NULL; /* what woudl happen here if i left this line out? or pNew = 0;
*/

Are there any problems with this code?


Only of you expect 'pNew = NULL;' to set to null ALL the pointer that
pointed to the deleted memory. If that's not the case, the problem is
probably elsewhere. Explicit memory management opens the door for all
kinds of errors, which is why it is best avoided.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #2
Hamish Dean wrote:
I have created a DLL which the user is complaining:

"Even when it works ok, my application stays instable (like something is
allocated or deleted in the wrong way)."

What does this mean? The way I have been allocating is:

int * pNew;
pNew = new int;

And deletion:

delete pNew;
pNew = NULL; /* what woudl happen here if i left this line out? or pNew = 0;
*/
Nothing would happen, and "0" and "NULL" are, roughly speaking,
the same. I prefer "0" because it's supported directly by the
language and doesn't depend on a macro definition in whatever
header it is.

You set a pointer to 0 in order to indicate that it doesn't point
to an object. This is sometimes useful. Sometimes the actual check
can be elided if all you want to do is "delete the object pointed to,
if any" (since "delete 0;" is a no-op). Sometimes you'll do an explicit
comparison.

If the pointer goes out of scope immediately after deletion,
setting it to 0 is a waste of time (but not much time).
Are there any problems with this code?


Not as written. But then, as written, it doesn't do anything.
Perhaps the control flow is not exactly as you think, and the
"delete" statement is not executed, or is executed more than
once, in some circumstances. You should probably be using RAII.

--
Regards,
Buster.
Jul 22 '05 #3

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

Similar topics

1
by: Riadh Elloumi | last post by:
Hi, I have some problems when debugging memory allocation (for both malloc and new) in a program that uses standard lists and vectors. When I add an element to the vector, the overloaded...
2
by: Hamish Dean | last post by:
Hi, I have created a DLL which the user is complaining: "Even when it works ok, my application stays instable (like something is allocated or deleted in the wrong way)." What does this...
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
13
by: xian_hong2046 | last post by:
Hello, I think dynamic memory allocation is supposed to be used when one doesn't know in advance how much memory to allocate until run time. An example from Thinking in C++ is to dynamically...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
7
by: ucfcpegirl06 | last post by:
Hello, Maybe someone can help me with this. I believe I have a memory allocation problem. The program crashes w/ a debug error. cpp file: #include <cstdio> #include <cstring> #include...
4
by: Tomassus | last post by:
Hi there, I have a problem with dynamic memory allocation. I know that it would have been easier to use vectors methods, but i want to know what i do here wrong. This is one of my methods in...
12
by: whitehatmiracle | last post by:
Hi all Im not quite sure how to use the new and delete for a whole array dynamically. Actually i want that, a user inputs a char in a single char array. Everytime he inputs a char he creates a...
31
by: Markus Pitha | last post by:
Hello, I'm using a template to simulate a LinkedList from Java.It works without problems, but when I want to use strings in main.cpp instead of char*, I get the following error message: $...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.