a really large allocation

Hi, I'm having an issue with an allocation error. I have a very large integer nwk that is an unsigned long.

I want to dynamically allocate an array of length nwk

double *A = new double[nwk+1];

but I am getting this error:

terminate called after throwing an instance of 'std::bad_alloc'
what(): St9bad_alloc

I assume this is because I'm trying to allocate a really long array. But I need all that space to solve this large problem. Is there some way to fix this?
closed account (zb0S216C)
You could try implementing a linked-list, where each node is a block of allocated memory:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct LinkedList
{
    struct Node 
    {
        Node(unsigned int const size = 0u);
       ~Node();

        double *memory;
    };

    LinkedList(unsigned int const node_count = 0u);
   ~LinkedList();

    private:
        Node *next;

    public:
        // Interface...
};

I would suggest a standard container, but I don't know if they are suitable.

Wazzak
closed account (o3hC5Di1)
Hi there,

It's hard to tell without knowing the size of nwk.
If I remember correctly, double's are 8 bytes large, so multiply that by nwk and you'll have an idea of how much memory you're trying to allocate.

Perhaps an STL container (vector, deque,...) would also be a safer way to implement such large storage.

All the best,
NwN
But I need all that space to solve this large problem.
¿are you sure?
closed account (zb0S216C)
@ne555: No, (s)he wants to allocate a large block of memory for kicks. If they need the memory, so be it; there's no need to question their requirements. Besides, we don't even know what the program is or even does.

Wazzak
That's exactly what I'm trying to know (in order to provide an useful answer)

If you run out of memory then download some RAM, xP
Topic archived. No new replies allowed.