473,327 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Vector malloc

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
Apr 3 '08 #1
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.
Apr 3 '08 #2
Studlyami
464 Expert 256MB
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.
Apr 3 '08 #3
gpraghuram
1,275 Expert 1GB
I dont think so this line will compile....

Expand|Select|Wrap|Line Numbers
  1. std::vector<ClassA*> a_vector =  (std::vector<ClassA>*)malloc(maxEntries*sizeof(dummy));
  2.  
  3.  
as you are allocating memory for a stack object.


Raghuram
Apr 3 '08 #4
Laharl
849 Expert 512MB
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?
Apr 3 '08 #5
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
Apr 4 '08 #6
pootle
68
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,
Apr 5 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

24
by: simon | last post by:
Hi, First some background. I have a structure, struct sFileData { char*sSomeString1; char*sSomeString2;
13
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,...
8
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...
1
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; ...
7
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...
2
by: andrey.vul | last post by:
code: #include <iostream> #include <vector> #include <stdexcept> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring>
7
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 ,...
3
by: stef | last post by:
Hello, Under VS2005 with a console projet: class Foo { private: static int x;
10
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...
0
isladogs
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...
1
isladogs
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...
0
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...
0
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...
0
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...
1
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....
0
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
0
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...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.