
-----------------------------------
Lone]2
Thu Apr 14, 2005 11:54 pm

deleting 2d arrays?
-----------------------------------
sorry if this was on an earlier post (i tried to search for it but didn't find any topics on this)

i know to delete an array is

delete [] ar;

but how do we delete a 2d array?
do we hafta do

for (i = 0 ; i < size; i++)
{
    delete [] ar[i];
}
delete [] ar;

or can we do:
delete [][] ar;

-----------------------------------
wtd
Fri Apr 15, 2005 12:16 am


-----------------------------------
Pretty much.  

However, if you're trying to create something like a Matrix, I suggest something like:

template 
class matrix_base
{
   private:
      _t * _data;
   
   public:
      const size_t rows, cols;

      matrix_base(size_t r, size_t c)
      : _data(new _t

-----------------------------------
Martin
Fri Apr 15, 2005 10:03 am


-----------------------------------
You should really avoid 2d arrays in general, as addressing them is slower than addressing a 1d array.

-----------------------------------
rizzix
Fri Apr 15, 2005 2:05 pm


-----------------------------------
aah? correct me i'f i'm wrong but i dont think delete [][] arr is legal.

-----------------------------------
Martin
Fri Apr 15, 2005 2:25 pm


-----------------------------------
Why would you ever have to delete an array?

-----------------------------------
rizzix
Fri Apr 15, 2005 2:25 pm


-----------------------------------
huh?

-----------------------------------
Andy
Fri Apr 15, 2005 3:29 pm


-----------------------------------
the delete function gets rid of a pointer, but i dont think it actually gets rid of data, which means its kinda pointless. umm it takes a pointer so delete **array should work

-----------------------------------
rizzix
Fri Apr 15, 2005 4:02 pm


-----------------------------------
wow u guys dont know ur c++ very well... the delete arr where arr is a pointer will free memory of the size of the type of the pointer..

but for arrays: delete [] arr, where arr is an array pointer (not the usual pointer) it frees all the allocated array memory and i think its pretty smart on the amount memory it needs to delete. so long as the memory  was allocted with the new operator i think.

-----------------------------------
wtd
Fri Apr 15, 2005 4:08 pm


-----------------------------------
Yes, C++ compilers maintain a magical piece of information on dynamically allocated arrays indicating their length.

When you "delete []" a dynamically allocated array it not only frees the memory for the pointer, but also the entire array.

Better, though, is to simply use a vector.

-----------------------------------
Lone]2
Fri Apr 15, 2005 9:57 pm


-----------------------------------
thanks guyz XD lots of help =P
now i got another problem lol......
i have a struct

struct Somestruct
{
	int left;
	int top;
	int right;
	int bottom;
	SomeObject *p;
};

Say i have an array of 'somestruct' how do i actually get/put the information inside the array?... i tried:

Somestruct *ar;
ar = new Somestruct[size];
ar[size]->left = left;
ar[size]->right = right;
ar[size]->top = top;
ar[size]->bottom = bottom;
ar[size]->p = &something;

but it doesn't seem to work?

My error during compile says: 
"Pointer to structure required on left side of -> or ->*"

i need some help  :cry: 

by the way my compiler is Borland 5.5 Command-Line C/C++ compiler

-----------------------------------
wtd
Fri Apr 15, 2005 10:20 pm


-----------------------------------
The elements in the array are not pointers.  

so:

arr[element].left = left;

Another thing to note:

When you create an array like:

int *foo = new int[5];

The array has five elements, but it starts at 0, so the largest subscript value we can use is 4.

Oh, and I'd suggest checking out GCC (via MinGW) at the earliest opportunity.  Borland 5.5 is old, and likely lacks support for a lot of standards.

-----------------------------------
Lone]2
Fri Apr 15, 2005 10:27 pm


-----------------------------------
oh yea..... XD... that. means size is too big  :oops: 
lol......... okay... one thing doesn't work though

when i try to call an objects function.........
for example: 

*ar[i].p.objectFunction();

it doesn't work?

(i can access the varibles okay... BUT i can't access the dereferenced object's function)????

-----------------------------------
wtd
Fri Apr 15, 2005 10:44 pm


-----------------------------------
First, show us the code for the class/struct in question.

However, I think the problem is that you don't really understand how to use pointers, or the priority of the dereferencing operator.

Let's say I have a simple class:

class Foo
{
   public:
      int func() { return 42; }
};

Now, I have a dynamically-allocated array of Foo objects.

Foo *arr = new Foo[10];

I can call that member function like so:

arr[0].func();

-----------------------------------
Lone]2
Fri Apr 15, 2005 10:53 pm


-----------------------------------

class WwText
{
	int size;
	char *str;
	int line_width;
	int line_start;
public:
	WwText();
	WwText(const char s[]);
	WwText(int width);
	WwText(const char s[], int width);
	WwText(const WwText&);
	bool set (const char s[]);
	bool set (int width);
	bool set (const char s[], int width);
	bool append (const char s[]);
	int findline (int n);
	int getline (char s[]);
	int fwriteln (FILE *fp);
	void getstring (char s[])const;
	int length ()const;
	int position()const;
	char position (int i)const;
	void fwrite (FILE *fp)const;
	int fread (FILE *fp);
	WwText& operator=(const char in[]);
	WwText& operator=(int in);
	WwText& operator=(const WwText& in);
	WwText& operator+=(const char in[]);
	bool operator==(const WwText& right);
	~WwText();
};
int WwText::getline (char s[])
{
	int start = line_start;
	int end=0;
	int i=0;
	end = findEndOfLine(str, start, line_width);
	for (i = 0; start < end; i++, start++)
	{
		s[i] = str[start];
	}
	s[i] = '\0';
	if (str[end] == ' ')
		end++;
	line_start = end;
	return end;
}

here is part of my object code......
now here is what i want to do


struct Rect
{
	int left;
	int top;
	int right;
	int bottom;
	WwText *txt;
};
// note: this is the part of code i want to do 
//not ACTUAL code in my program but theres lapses... so i only 
//show the important part of code
Rect *rects;
rects = new(nothrow) Rect[50];
rects[i].txt = &alreadyInitializedObject;
*rects[i].txt.getline(temp);

now when i try to call the dereferenced object's (*rects[i].txt) function...(getline) i get an error...

-----------------------------------
wtd
Fri Apr 15, 2005 10:59 pm


-----------------------------------
*rects[i].txt.getline(temp);

The problem you're running into here is the priority of the dereferencing operator.  What I've copied and pasted above is equivalent to:

*(rects[i].txt.getline(temp));

You would need:

(*rects[i].txt).getline(temp);

But easier on the eyes is:

rects[i].txt->getline(temp);

-----------------------------------
Lone]2
Fri Apr 15, 2005 11:02 pm


-----------------------------------
omg thanks so much XD.... i can't believe i forgot that... i learned it too lol....
thanks no more problems.....
