Re: [Dev-C++] warning when deleting allocated mem
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Kevin W. <ke...@me...> - 2001-12-19 16:00:48
|
As I understand it (I could be wrong) you are deleting the wrong thing.
The array is static - you did not use new to create it, so you shouldn't use
delete to get rid of it. Instead, you need to delete all the character
arrays that are pointed to by the pointers in the static array:
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
delete[] pArray[i][j];
Kev.
----- Original Message -----
From: Julio Lajara <ju2...@ya...>
To: <dev...@li...>
Sent: Wednesday, December 19, 2001 2:08 AM
Subject: [Dev-C++] warning when deleting allocated mem
> I can't figure out why the compiler gives me a warning
> about deleting the array in the following code:
>
>
> #include <iostream>
> #include <fstream>
> #include <cstdlib>
> using namespace std;
>
> ofstream fout;
> ifstream fin;
>
> int main()
> {
> char *pArray[2][2];
>
> for(int i = 0; i < 2; i++)
> for(int j = 0; j < 2; j++)
> pArray[i][j] = new char [10];
>
> strcpy(pArray[0][0],"1234567890");
> strcpy(pArray[0][1],"1112131415");
> strcpy(pArray[1][0],"1617181920");
> strcpy(pArray[1][1],"2122232425");
>
> cout << pArray << "\n\n";
>
> fout.open("writing 2D array from stack.ini",
> ios::out);
>
> if(fout.good())
> {
> fout.write(pArray, sizeof pArray);
> fout.close();
> }
> else
> {
> cout << "\n\nERROR, could not write data to
> file...";
> fout.close();
> }
>
> delete [] pArray;
> system("PAUSE");
> return 0;
> }
>
>
> Of course I can ignore the warning and it still runs
> fine, but I still would like to know why it is giving
> the warning...
>
> =====
> Julio
>
> __________________________________________________
> Do You Yahoo!?
> Check out Yahoo! Shopping and Yahoo! Auctions for all of
> your unique holiday gifts! Buy at http://shopping.yahoo.com
> or bid at http://auctions.yahoo.com
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|