I have a vector
std::vector<ClassA*> a_vector;
I need to be able to hold a bunch of ClassA objects in it. I understand it is better to push adress of ClassA objects than the object itself. But i still end up with a bad_alloc since there are about 100,000 of them i need to hold in the vector.
So my question is how can i malloc some space to a_vector based on the size of ClassA Address?
I know this is wrong but , how do i correct the following
ClassA* dummy = new ClassA();int maxEntries = 100000;
std::vector<ClassA*> a_vector = (std::vector<ClassA>*)malloc(maxEntries*sizeof(dum my));
delete dummy;
Thank You
6 10184 Banfa 9,065
Expert Mod 8TB
A vector can quite easily deal with 100,000 entries from a theoretical stand point and since you are storing pointers which are likely to be around 4 bytes in size that would be a total of 400,000 bytes which should be a problem for most systems today. Therefore I suspect the problem is something to do with how you are allocating instances of ClassA or the size of ClassA that is causing the problem.
You shouldn't be using malloc the vector has a function called reserve() which will allow you to reserver a block of memory for the vector (you pass in the number of elements you want to reserve for the vector). You can also call the maxsize() function which will tell you the total number of elements that the vector can hold (usually it will be a really large number). Take a look here for a reference on a few of the vector functions.
I dont think so this line will compile.... -
std::vector<ClassA*> a_vector = (std::vector<ClassA>*)malloc(maxEntries*sizeof(dummy));
-
-
as you are allocating memory for a stack object.
Raghuram
Yeah, you'd need to cast it to a pointer, since malloc() returns a void*. Frankly, why aren't you using new, since this is C++, not C?
Found that creating instances of ClassA was using up all the memory and is the reason for bad_alloc. Looks like ClassA was heavy. So instead of having a collection of 100,000 objects, i gather all the data ClassA needs to use in a struct and create an object at run time.
Thank You for all the replies. Appreciate it.
V
Just for interest, if you ever find that you want to use a different allocation scheme than the default, you can provide a custom allocator to standard library container classes. The definition of a vector is vector<T, Alloc>. The Alloc template parameter is used either to select one of the other allocator types, or you can specify your own.
Regards,
Sign in to post your reply or Sign up for a free account.
Similar topics
by: simon |
last post by:
Hi,
First some background.
I have a structure,
struct sFileData
{
char*sSomeString1;
char*sSomeString2;
|
by: Richard |
last post by:
vector<char*> m_Text;
m_Text.resize(1);
char* foo = "FOO";
char* bar = "BAR";
char* foobar = (char*)malloc(strlen(foo) + strlen(bar) + 1);
if (foobar)
{
strcpy(foobar, foo);
strcat(foobar,...
|
by: Ross A. Finlayson |
last post by:
I'm trying to write some C code, but I want to use C++'s std::vector.
Indeed, if the code is compiled as C++, I want the container to
actually be std::vector, in this case of a collection of value...
|
by: Tao |
last post by:
hi.. Group,
I want to use __m128i with vector, but it seems does not work. The following
code generated a runtime error indicating that there is a memory corruption.
std::vector<__m128ilist;
...
|
by: nw |
last post by:
Hi,
We've been having a discussion at work and I'm wondering if anyone
here would care to offer an opinion or alternative solution. Aparently
in the C programming HPC community it is common to...
|
by: andrey.vul |
last post by:
code:
#include <iostream>
#include <vector>
#include <stdexcept>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
|
by: hyderabadblues |
last post by:
Following is part of my program that I am trying to code to simluate
folder information which contains mp3 files.
At the end of main funcion there is assert, I have tried to debug the
program ,...
|
by: stef |
last post by:
Hello,
Under VS2005 with a console projet:
class Foo
{
private:
static int x;
|
by: Babak |
last post by:
Hi,
I've developed a C program which contains a large number of vectors
and matrices operations. Throughout my code, I used the template from
the Numerical Recipes book to define vectors and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |