Problem with large pointer array
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Problem with large pointer array

  1. #1
    Join Date
    Feb 2013
    Posts
    2

    Problem with large pointer array

    Hello,
    I am working on a program which creates a large pointer array of numbers and then performs several iterations of operations in them.
    Code:
    int * u = new int[N];
    double * nu = new double[N];
    int * nud = new int[N];
    for (int i=0;i<M;i++){
    
         for (int i=0;i<N;i++){
        u[i]=0;
        nu[i]=0;
        nud[i]=0;
         }
    
         (perform some operations on nu and nud, increment a counter)
    
    
    }
    If M is small enough then there are no problems in the program. However once M is large enough I get the "unhanded exception":

    std::bad_alloc at memory location 0x0026f728..

    Since I am just reusing the same arrays, and since I am able to make it through a few iterations, I didn't think it could be a memory issue. If it is, is there a way I can clear the data completely after each iteration?

    Thanks!

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,106

    Re: Problem with large pointer array

    You don't think bad_alloc is a memory issue? How big does N have to be to fail?

  3. #3
    Join Date
    Feb 2013
    Posts
    2

    Re: Problem with large pointer array

    N is only around 4000. What bothers me is that I get bad_alloc after completing the first few iterations. If it was a memory problem I would expect it to fail during the first initialization. If M is set <7 it runs fine, but any greater and I get the unhandled exeption.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,106

    Re: Problem with large pointer array

    I don't see anything. Can you post complete code for a small program that reproduces the problem.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,429

    Re: Problem with large pointer array

    Quote Originally Posted by goliath11 View Post
    Since I am just reusing the same arrays, and since I am able to make it through a few iterations, I didn't think it could be a memory issue.
    Well, let's see the complete program. A naked psuedo function with a call to new[] doesn't tell us anything. What if that missing code is corrupting the heap in some way?

    For example, why are you using the same "i" in the nested loop as a counter in both loops? That in itself is a bug.
    If it is, is there a way I can clear the data completely after each iteration?
    Post a complete program. so that we know how to analyze the problem. So far, what you posted is incomplete, and it has a bug.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center