|
Will you please stop reposting the same question again and again ?
Actually what are you trying to do exactly ? What do you mean exactly by ímport'??
|
|
|
|
|
|
Hi,
CString* strData;
When I am calling "delete [] strData" , I am getting a crash . What are the possible reasons for getting the crashes.
Taruni
|
|
|
|
|
you didn't initialise strdata as new?
CString *strData = new CString();
then use
delete strData;
|
|
|
|
|
I have initialised. I forgot to mention.
Before deleting , I am checking whether strData exists or not as follows.
if(strData)
{
delete [] strData;
}
The if condition is satisfied , but a crash when deleting strData.
Taruni
|
|
|
|
|
check *strData: that a pointer in your code.
Greetings from Germany
|
|
|
|
|
Why you need [] in delete as it is a single pointer variable ?
It is applicable when you are trying to delete an array of pointer.
CString* strData = new CString;<br />
<br />
if(NULL != strData)<br />
{<br />
delete strData;<br />
}
Regards,
Paresh.
|
|
|
|
|
Thank You Paresh.
But I am trying to delete an array of pointers of CString type.
Taruni
|
|
|
|
|
How did you allocate memory to strData pointer ? Please post the code snippet.
Regards,
Paresh.
|
|
|
|
|
CString* strData;
int nCount;
if(nCount > 0)
{
strData = new CString[nCount];
}
.
.
.
delete[] strData;
I have understood the problem , Paresh . The nCount is 0. So never the memory is allocated . But what should be the condition to test whether strData is actually initialised (strData is an array of pointers of CString type) before it is actually deleted.
Taruni
|
|
|
|
|
Taruni wrote: But what should be the condition to test whether strData is actually initialised
There is such a condition only if you're disciplinate programmer:
CString* strData = NULL;
int nCount;
if(nCount > 0)
{
strData = new CString[nCount];
}
...
if (strData)
{
delate [] strData;
strData = NULL;
}
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Taruni wrote: But I am trying to delete an array of pointers of CString type.
So, you did something like this ?
CString pArray = new CString[10];
If yes, then you need to use delete[] instead:
delete[] pArray;
|
|
|
|
|
Thank you Cedric Moonen.
CString* strData;
int nCount;
if(nCount > 0)
{
strData = new CString[nCount];
}
.
.
.
delete[] strData;
I have understood the problem . The nCount is 0. So never the memory is allocated . But what should be the condition to test whether strData is actually initialised (strData is an array of pointers of CString type) before it is actually deleted.
Taruni
|
|
|
|
|
Initialise strData with NULL and check strData against NULL before deleting.
CString* strData = NULL;<br />
int nCount;<br />
if(nCount > 0)<br />
{<br />
strData = new CString[nCount];<br />
}<br />
<br />
.<br />
.<br />
.<br />
if(NULL != strData)<br />
{<br />
delete[] strData;<br />
}
Regards,
Paresh.
|
|
|
|
|
Paresh Chitte wrote: Initialise strData with NULL
Yes.
Paresh Chitte wrote: and check strData against NULL before deleting.
That's not necessary. Deleting NULL is guaranteed to be a no-op.
|
|
|
|
|
Nemanja Trifunovic wrote: That's not necessary. Deleting NULL is guaranteed to be a no-op.
The test maybe useful to mark again with NULL the deleted pointer:
if (strData)
{
delete [] strData;
strData = NULL;
}
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Paresh Chitte wrote: if(NULL != strData)
{
delete[] strData;
}
I suggest to replace the above with:
if(NULL != strData)
{
delete[] strData;
strData = NULL;
}
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Initialize your pointer to NULL:
CString* strData = NULL;
You should always always initialize your pointers to NULL.
|
|
|
|
|
Cedric Moonen wrote: You should always always initialize your pointers to NULL.
Unless you initialize them with something else
I.e., this is acceptable:
<br />
CString* strData = new CString[count];<br />
In any case this new/delete construct is overused and leads to code that is not exception-safe and is hard to maintain. Wouldn't it be easier and safer to simply write something like:
<br />
vector<CString> strData;<br />
or even:
<br />
CStringArray strData;<br />
|
|
|
|
|
Nemanja Trifunovic wrote: Unless you initialize them with something else
I.e., this is acceptable:
CString* strData = new CString[count];
I agree on the above and suggest the following (weaker than the Cedric Moonen one)
gold rule of the disciplined coder:
You should always initialize your pointers
Nemanja Trifunovic wrote: In any case this new/delete construct is overused and leads to code that is not exception-safe and is hard to maintain. Wouldn't it be easier and safer to simply write something like:
I don't blame for new/delete usage. You've to be disciplined though.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
CPallini wrote: I don't blame for new/delete usage. You've to be disciplined though.
A LOT of discipline and hard work is needed to make this code exception-safe:
A* a = new A;
...
B* b = new B;
...
C* c = new C;
...
delete c;
delete b;
delete a;
|
|
|
|
|
Of course, one can choose the proper data depending on the coding scenario. BTW at the very end of your reasonment there's managed code!!
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
CPallini wrote: BTW at the very end of your reasonment there's managed code!!
Nope!
Managed code would help in this particular scenario, but if we were allocating resources other than memory (files, handles, db connections...) it wouldn't help at all.
What I am advocating here is the use of a simple and effective idiom known as RAII[^]. It enables automatic release of the allocated resources in a deterministic way without use of a GC.
|
|
|
|
|
Managed code works in the same direction.
What I am advocating, instead, it is developer freedom to deallocate resources by itself.
Please note: it always depends on the coding scenario: complex projects usually need some paradigm like the one you suggest.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Hello All,
I have an Application in VC++6.0 using MFC. I have to build this application in VC++2005. I tried to build this but so many errors are coming. How can I port my applicaion. What steps I should follow to do this. The common bug is given below :-
error C4335: Mac file format detected: please convert the source file to either DOS or UNIX format.
This is comming many times. Can you tell me how can i debug this.
Regards,
Yudhisthira
|
|
|
|