Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 deleting 2d arrays?
Index -> Programming, C++ -> C++ Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Lone]2




PostPosted: Thu Apr 14, 2005 11:54 pm   Post subject: 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;
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Fri Apr 15, 2005 12:16 am   Post subject: (No subject)

Pretty much.

However, if you're trying to create something like a Matrix, I suggest something like:

c++:
template <typename _t>
class matrix_base
{
   private:
      _t * _data;
   
   public:
      const size_t rows, cols;

      matrix_base(size_t r, size_t c)
      : _data(new _t[r * c])
      , rows(r)
      , cols(c)
      { }

      ~matrix_base()
      {
         delete [] _data;
      }

      _t operator()(size_t r, size_t c) const
      {
         return _t[r * c];
      }

      _t& operator()(size_t r, size_t c)
      {
         return _t[r * c];
      }
};
Martin




PostPosted: Fri Apr 15, 2005 10:03 am   Post subject: (No subject)

You should really avoid 2d arrays in general, as addressing them is slower than addressing a 1d array.
rizzix




PostPosted: Fri Apr 15, 2005 2:05 pm   Post subject: (No subject)

aah? correct me i'f i'm wrong but i dont think delete [][] arr is legal.
Martin




PostPosted: Fri Apr 15, 2005 2:25 pm   Post subject: (No subject)

Why would you ever have to delete an array?
rizzix




PostPosted: Fri Apr 15, 2005 2:25 pm   Post subject: (No subject)

huh?
Andy




PostPosted: Fri Apr 15, 2005 3:29 pm   Post subject: (No subject)

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




PostPosted: Fri Apr 15, 2005 4:02 pm   Post subject: (No subject)

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.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Fri Apr 15, 2005 4:08 pm   Post subject: (No subject)

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




PostPosted: Fri Apr 15, 2005 9:57 pm   Post subject: (No subject)

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 Crying or Very sad

by the way my compiler is Borland 5.5 Command-Line C/C++ compiler
wtd




PostPosted: Fri Apr 15, 2005 10:20 pm   Post subject: (No subject)

The elements in the array are not pointers.

so:

code:
arr[element].left = left;


Another thing to note:

When you create an array like:

code:
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




PostPosted: Fri Apr 15, 2005 10:27 pm   Post subject: (No subject)

oh yea..... XD... that. means size is too big Embarassed
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




PostPosted: Fri Apr 15, 2005 10:44 pm   Post subject: (No subject)

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:

code:
class Foo
{
   public:
      int func() { return 42; }
};


Now, I have a dynamically-allocated array of Foo objects.

code:
Foo *arr = new Foo[10];


I can call that member function like so:

code:
arr[0].func();
Lone]2




PostPosted: Fri Apr 15, 2005 10:53 pm   Post subject: (No subject)

code:

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

code:

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




PostPosted: Fri Apr 15, 2005 10:59 pm   Post subject: (No subject)

code:
*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:

code:
*(rects[i].txt.getline(temp));


You would need:

code:
(*rects[i].txt).getline(temp);


But easier on the eyes is:

code:
rects[i].txt->getline(temp);
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 16 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: