"delete []" in MSDN 
Author Message
 "delete []" in MSDN

I'm learning VC++ 6, and have an example program with a strange but
repeatable error.

I have the July 2000 MSDN CD, but can't figure out how to search the
knowledgebase for
   delete []

The "[]" is ignored, and the returned results list is huge and
useless...


<g>).

   -- Mark



Wed, 05 Mar 2003 03:00:00 GMT  
 "delete []" in MSDN

Quote:
> I'm learning VC++ 6, and have an example program with a strange but
> repeatable error.

> I have the July 2000 MSDN CD, but can't figure out how to search the
> knowledgebase for
>    delete []

> The "[]" is ignored, and the returned results list is huge and
> useless...


> <g>).

The MSDN help states that punctuation is ignored in full text searches. Try
a search for:

"delete operator"

(notice quotes) instead.

NeilB



Wed, 05 Mar 2003 03:00:00 GMT  
 "delete []" in MSDN
.. continuing ...

Quote:
> I'm learning VC++ 6, and have an example program with a strange but
> repeatable error.

I'll describe the error -- maybe someone knows the problems.

To learn about overloading the =, I wrote a program that uses a
"dynamic array" class, dynarray.

In main()

   dynarray d(4);   // Create array w/4 elements
   dynarray e(6);
   e = d;   // Copies d's 0-3 to e's 0-3

works fine.  However, changing e to e(3)

   dynarray d(4);   // Create array w/4 elements
   dynarray e(3);
   e = d;   // Copies d's 0-2 to e's 0-2

works fine, but after main has returned an error shows up in
   C:\...\Vc98\Crt\Src\Dbgheap.c

If I don't call the operator= function or the destructor, there is no
error in Dbgheap.c.  The relevant code:

class dynarray {
   int *m_p;
   int m_size;
public:
   dynarray(int nElements);
   ~dynarray() {delete [] m_p;}
   dynarray &operator=(dynarray &obj);
   ...

Quote:
};

dynarray::dynarray(int nElements) {
   m_size = nElements;
   m_p = new int[m_size];
   if (!m_p) {
      cout << "Allocation error\n";
      exit (1);
   }
   for (int i=0; i<m_size; i++)   // Zero the array
      m_p[i]=0;

Quote:
}

dynarray &dynarray::operator=(dynarray &obj) {
   int i,j;

   for (i=0,j=0; i<m_size, j<obj.m_size; i++,j++)
      m_p[i] = obj.m_p[j];

   return *this;

Quote:
}



Wed, 05 Mar 2003 03:00:00 GMT  
 "delete []" in MSDN
Neil,

Quote:
> Try
> a search for:

> "delete operator"

> (notice quotes) instead.

Thanks.  None of the KB articles seem to apply to my error, though.

   -- Mark



Wed, 05 Mar 2003 03:00:00 GMT  
 "delete []" in MSDN


Quote:
> .. continuing ...

> > I'm learning VC++ 6, and have an example program with a strange but
> > repeatable error.

> I'll describe the error -- maybe someone knows the problems.

> To learn about overloading the =, I wrote a program that uses a
> "dynamic array" class, dynarray.

[snip]

Quote:
> dynarray &dynarray::operator=(dynarray &obj) {
>    int i,j;

>    for (i=0,j=0; i<m_size, j<obj.m_size; i++,j++)
>       m_p[i] = obj.m_p[j];

>    return *this;
> }

The problem is in the for() loop:

Quote:
>    for (i=0,j=0; i<m_size, j<obj.m_size; i++,j++)

The statement

Quote:
> i<m_size, j<obj.m_size;

uses the comma operator. This operator causes evaluation to produce the last
expression in the statement. Therefore what happens is:

evaluate: i<m_size            and throw away result
evaluate: j<obj.m_size      and always use this as the result of the test

You want to look at using the && and || operators, which  do short-circuited
evaluation.

NeilB



Wed, 05 Mar 2003 03:00:00 GMT  
 "delete []" in MSDN

Neil,

Quote:
> The statement

> > i<m_size, j<obj.m_size;

> uses the comma operator. This operator causes evaluation to
> produce the last
> expression in the statement. Therefore what happens is:

Thanks!  I _should_ have known that ;-)

   -- Mark



Wed, 05 Mar 2003 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Bug: VC++ 6 "forgets" about MSDN

2. Question about MSDN Example called "ChatSrvr"

3. Why can only undo "Delete"?

4. Delete "/" from char pointer

5. Deleting with "fwrite()"

6. ODBC, VFP and "real" deletes

7. how to use "delete"?

8. Question about the operator "delete"

9. "scalar deleting destructor"

10. C++ question with "delete"

11. Error when "delete[] m_pHashTable" in CMap

12. SUMMARY: "Deleting every other byte"

 

 
Powered by phpBB® Forum Software