-1

I'm able to create a dynamically sized array of integers like this:

int *cacheL2 = new int[L2/B2];

and I'm also able to create an object of type Data like this:

Data one(12,12);

and now I want a dynamically sized array of Data:

Data * vlaObj = new Data[L2/B2];

but it does not work...

Also, if you can tell me how to get hashes working for c++ that would be great. I was looking for examples, but everything just says #include "hash_map" however when I try to use the library it cant seem to find any of them.

M.M
  • 138,810
  • 21
  • 208
  • 365
Blackdragon1400
  • 413
  • 4
  • 18
  • 3
    How does it "not work"? – juanchopanza Feb 18 '13 at 00:47
  • I suspect your class doesn't have a default constructor? Oh, and as for a hash map, if you can use C++11, use std::unordered_map. – Corbin Feb 18 '13 at 00:47
  • 2
    Is there a reason you can't use the STL? – Aesthete Feb 18 '13 at 00:47
  • Why not use std::vector for the VLA and std::map for the hash? – Adam27X Feb 18 '13 at 00:47
  • @Adam27X: Note that `std::map` is not a hash table. You might want to read [Why is `std::map` implemented as red-black tree?](http://stackoverflow.com/questions/5288320/why-is-stdmap-implemented-as-red-black-tree) – LihO Feb 18 '13 at 00:52
  • pedantic point, what you are doing is called a [dynamic array](http://stackoverflow.com/a/2672106/1520364) and not a [Variable Length Array](http://en.wikipedia.org/wiki/Variable-length_array) – Karthik T Feb 18 '13 at 00:53
  • Thanks for letting me know about the unordered map, Im going to try to use that instead! – Blackdragon1400 Feb 18 '13 at 00:55
  • @user2036351 Are you going to include a description of _what_ problems or errors you get? – jogojapan Feb 18 '13 at 01:16

3 Answers3

2
#include <vector>

// ...
std::vector<Data> v;
v.emplace_back( 12, 12 );
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • 2
    Worth to note that `emplace_back` is C++11 feature. C++03 replacement could be `v.push_back(Data(12,12));`. – LihO Feb 18 '13 at 00:58
1

There is no reason to not use STL containers here. It is recommended to use std::vector instead of raw pointers:

#include <vector>

//...
std::vector<Data> vlaObj(L2/B2);
vlaObj.push_back(one)

Edit: BTW is there any chance that L2 or even B2 value can be 0?

Sergey
  • 627
  • 1
  • 9
  • 16
0

One of the most probable reasons why Data* arr = new Data[len]; wouldn't work is because type Data has no default constructor, i.e. Data::Data().

But no matter whether Data has a default constructor or not, it's not a good idea to try to create an array like this anyway. Once you dynamically allocate it with new[] you commit yourself to take care of ugly memory management connect with it. It's much better idea to use one of STL containers such as std::vector (#include <vector> required) that will take care of memory management for you.

Then you have several options:

std::vector<Data> v;                    // option 1
v.reserve(len);
// in loop:
    v.push_back(Data(x, y));            // there could be different values

std::vector<Data> v2(len);              // option 2

std::vector<Data> v3(len, Data(12,12)); // option 3

First option will fit almost any situation. It prepares the chunk of memory big enough to hold len elements and then you can just fill v in convenient but still very efficient manner. Option 2 requires Data to have default constructor, which is solved by option 3 that uses your custom constructor to construct elements.

All mentioned options result in std::vector object with automatic storage duration being created. Note that all elements are stored in a continuous block of memory, so you can use &v[0] to initialize the pointer to its first element and work with it in a same way you would work with dynamically allocated array.

LihO
  • 41,190
  • 11
  • 99
  • 167