DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Sudipto Banerjee Guest

    Heap Problems: Typo in previous mail


    Hi,

    There was a typo in my previous mail: I was using "delete [] v" instead of
    delete v as was typed there. I give below my full earlier mail with the correction.

    Thanks and hoping to hear from someone.

    Hi all,

    I actually write numerical simulations as console applications for my research
    in C++. I have encountered the following problem with VC++ 6.0 which some
    have called a bug in the Microsoft compiler. I present here just a small
    test program that will, I hope, elucidate the point.

    Before you look at the program: This worked fine with g++ (on Linux) and
    with Borland C++ on Windows 2000. But in VC++6.0 on Windows 2000, for l=8,
    j = 31, i = 10169 and k = 105 in the nested loop in "main" it threw an exception
    break point complaining about sometinhg in _heap_alloc_dbg(,) and also looking
    for a dbgheap.c file!!! Initially I thought it was some memory leakage but
    I checked for them using the CRT functions and also assert. There was no
    such stuff going on. In any case, it is a simple test function where I dynamically
    allocate (using new) and almost immediately deallocate using delete! Also,
    a more C-like version using malloc and free gives the exact same problem.


    May anyone suggest some way of going around this problem in VC++ 6.0? Any
    help/suggestions will be greatly appreciated.

    Thanks for the attn. The code snippet follows below.

    #include <iostream>
    #include <cassert>

    double test (int i, int j);

    using namespace std;

    int main ()
    {
    double u = 0.0;
    int i, j, k, l;

    for (l = 0; l < 26; l++) {
    for (j = 0; j < 99; j++) {
    for (i = 0; i < 17325; i++) {
    for (k = 0; k < 301; k++) u = test(i,k);
    }
    }
    }
    }

    return 0;
    }

    double test (int i, int k)
    {
    int j;
    double *v, temp;

    v = new double [10];
    assert (v);

    for (j = 0; j < 10; j++) v[j] = 2*j+1;

    temp = v[0]*v[5];

    delete [] v;
    v = 0;

    return temp
    }


  2. #2
    ralph Guest

    Re: Heap Problems: Typo in previous mail


    "Sudipto Banerjee" <sudiptob@biostat.umn.edu> wrote:
    >
    >Hi,
    >
    >There was a typo in my previous mail: I was using "delete [] v" instead

    of
    >delete v as was typed there. I give below my full earlier mail with the

    correction.
    >
    >Thanks and hoping to hear from someone.
    >
    >Hi all,
    >
    >I actually write numerical simulations as console applications for my research
    >in C++. I have encountered the following problem with VC++ 6.0 which some
    >have called a bug in the Microsoft compiler. I present here just a small
    >test program that will, I hope, elucidate the point.
    >
    >Before you look at the program: This worked fine with g++ (on Linux) and
    >with Borland C++ on Windows 2000. But in VC++6.0 on Windows 2000, for l=8,
    >j = 31, i = 10169 and k = 105 in the nested loop in "main" it threw an exception
    >break point complaining about sometinhg in _heap_alloc_dbg(,) and also looking
    >for a dbgheap.c file!!! Initially I thought it was some memory leakage but
    >I checked for them using the CRT functions and also assert. There was no
    >such stuff going on. In any case, it is a simple test function where I dynamically
    >allocate (using new) and almost immediately deallocate using delete! Also,
    >a more C-like version using malloc and free gives the exact same problem.
    >
    >
    >May anyone suggest some way of going around this problem in VC++ 6.0? Any
    >help/suggestions will be greatly appreciated.
    >
    >Thanks for the attn. The code snippet follows below.
    >
    >#include <iostream>
    >#include <cassert>
    >
    >double test (int i, int j);
    >
    >using namespace std;
    >
    >int main ()
    >{
    > double u = 0.0;
    > int i, j, k, l;
    >
    > for (l = 0; l < 26; l++) {
    > for (j = 0; j < 99; j++) {
    > for (i = 0; i < 17325; i++) {
    > for (k = 0; k < 301; k++) u = test(i,k);
    > }
    > }
    > }
    > }
    >
    > return 0;
    >}
    >
    >double test (int i, int k)
    >{
    > int j;
    > double *v, temp;
    >
    > v = new double [10];
    > assert (v);
    >
    > for (j = 0; j < 10; j++) v[j] = 2*j+1;
    >
    > temp = v[0]*v[5];
    >
    > delete [] v;
    > v = 0;
    >
    > return temp
    >}
    >


    There are several more syntax errors: An extra Brace in Main() and a missing
    semicolon in the 'return()' statement in test().

    Once these were corrected the program ran to completion without error on
    my system (Win2k, 512 ram).

    _heap_alloc_dbg() is the malloc function called when using new for a debug
    compile. It is responsible for not only allocating memory but also filling
    the memory with bogus values. Your program does a "Ton" of allocations and
    the fact _heap_alloc_dbg() was named as the source of the error means simply
    that an allocation from the heap eventually failed. This would be consistent
    with having used "delete v", instead of "delete [] v".



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links