C++ :: Custom Dynamic Array Class And Allocator
May 10, 2014
I attempted to create a dynamic array class for use in my engine (due to problems regarding a dll-interface with the standard library), so I tried at making a standard-compatible allocator template class first. After I "finished" that, I went on to work on the dynamic array class itself.So I finish the dynamic array class, and test it with the standard allocator. It works perfectly, but when I test it with my custom allocator class, it fails terribly.
To make sure it wasn't my DynamicArray class that was causing issues, I tried using the custom allocator on the std::vector class template, and it didn't work either. IMy DynamicArray class code:
// Represents a dynamic array, similar to the standard library's "vector" class.
template<typename T, typename A>
class DynamicArray
{
public:
DynamicArray() :
data(nullptr),
elements(0),
capacity(0)
[code].....
The "Request" and "Free" functions are my engine's equivalent of malloc and free (or new and delete). I allocate a large buffer (16 mb), and through those functions I distribute the memory to where it's needed.
View 9 Replies
ADVERTISEMENT
Feb 7, 2014
I'm trying to write a custom allocator that I can use with the STL. Here's what I have so far :
Code:
#include <cstddef>
#include <iostream>
#include <memory>
#include <vector>
template<class T>
struct customallocator {
[Code].....
I'm doing and currently, my push_back doesn't seem to do anything.
View 12 Replies
View Related
Mar 4, 2012
Does the requirements have changed for writing custom allocators with C++ 0x11?
A custom allocator (that uses a fixed array as its memory pool) that I have been using successfully for some time, now throws an exception!
It runs fine under VS2008 but bombs out with VS2010.
View 14 Replies
View Related
Oct 15, 2014
I am currently working on a custom "Array" class for a project, and I have run into an error I don't quite understand the source of. The relevant code is as follows:
template<typename T> class Array {
private:
T errValCopy;
public:
T __errVal__;
uint16 __size__;
T* __ptr__;
Array(const T& errorValue);
[Code] ....
When I try to run the following code:
Array<Array<int>> a(Array<int>(-1));
The error log tells me there is no appropriate default constructor available. If I understand it correctly, "default constructor" refers to the constructor which lets you just write Array<int> a; instead of Array<int> a(...);, but I can't see where in the code such a situation occurs...
View 2 Replies
View Related
Apr 15, 2013
If I have an array of some class, and that class has const members, is there some way I can call a custom constructor on elements of the array?
I can't seem to reinitialize an element in foos in the example below. A thread on stack overflow mentioned the copy constructor show allow it, but I get "no match for call to '(Foo) (Foo&)'" when I try it.
Code:
class Foo {
public:
Foo();
Foo(int x, int y);
[Code] .....
View 4 Replies
View Related
Apr 3, 2013
How do I copy from a dynamic array initialized in a class but with a different memory address. For example if my array is a dynamic array initialized in a class...
Code:
const int CAPACITY=5;
class Array{
public:
Array();//constructor
[Code] .....
How would i copy this array to a another array but have a different memory address so when i deallocate array a my copy array also isn't deallocated.
View 1 Replies
View Related
Oct 29, 2014
// dynamic memory for 2D char array
char **board = (char**) calloc(column, sizeof(char*));
for(int i=0; i<column; i++)
board[i] = (char*) calloc(row, sizeof(char));
//for char 255 row and colum
char temp[255];
inputFile.getline(temp,255);
[Code]...
so i want to change into vector class like Vector<board>row;
View 8 Replies
View Related
Nov 14, 2013
I'm writing a class that has two constructors. However, I can't get them to work quite right. One constructor has a parameter of an int (with a default value of 0) and the other has a parameter of a C-style string.
First of all, are these function prototypes correct for the constructors?
MyInt(int n = 0);// first constructor, int param, default value 0
MyInt(const char * c);// second constructor, c-style string param
Both constructors work fine in some cases but don't work in all cases. Here are some potential calls to these functions that are supposed to work:
// These two work fine
MyInt x(12345),
y("9876543210123456789"),
// The array assignment doesn't work when the value is negative
// I'm not allowing negative numbers, but I want to create the object and assign the array to 0
r1(-1000),
[Code] .....
Here's the private data from the class (from the header file):
private:
int currentSize,
maxSize;
char* digits;// Pointer to an array of digits
// Increase the size of digits array by 5
void Grow ();
View 2 Replies
View Related
Nov 29, 2014
I have a class called Book and I am trying to create a dynamic pointer based array of the class. When I try to run the program I keep getting the error: pointer being freed was not allocated at the line of code that says "delete [] A;". I am using Xcode to run the program.
Book *changeArraySize(Book *A, int &size, double factor) {
int i;
int newsize = size*factor;
Book *A2 = new Book[newsize];
[Code] ....
View 7 Replies
View Related
Oct 9, 2014
Let's say we have a custom Vector class and I need to know which of the following is considered to be more efficient and why of course.
Vector Vector::operator+(const Vector &b) const {
return Vector(x+b.x,y+b.y);
}
Vector Vector::operator+(const Vector &b) const {
Vector tmp(x+b.x,y+b.y);
return tmp;
}
View 1 Replies
View Related
Oct 27, 2014
When we want to use custom classes for the STL set data structure, how to do it?
I look around and see people passing comparators, overloading operator(), and also overloading operator< in the class itself.
Which one's the idiomatic c++ way?
View 1 Replies
View Related
Oct 3, 2014
I have made a custom class matrices class which allows me to add, multiply, subtract (etc.) matrices. I have the following method for multiplication but I get an error that says
'invalid use of 'this' outside of a non-static member function'
How can I refer to the current instance without getting this error.
void operator *(Matrices& matrix2) {
this.multiplyMatrix(matrix2);
}
View 2 Replies
View Related
Nov 6, 2013
Right now I have code
#include <iostream>
using namespace std;
class Rectangle {
private:
double width;
double length;
[Code] .....
it gives error ...
View 1 Replies
View Related
Mar 15, 2015
I have a class like this:
#include <string>
using namespace std;
//-----------------------------------------------
class Prenumeratorius {
private:
string pavarde;
string adresas;
string leidinioKodas;
[code]....
bunch of variables, constructor, setter and getter.And I have this class:
#include <vector>
class Leidinys {
private:
string kodas;
string pavadinimas;
double vienetoKaina;
[code]....
in to my "Leidinys.h" header file, I get a build error, I tried to remove same includes in both files, that didn't worked.
View 3 Replies
View Related
Jan 4, 2013
I'm writing a program in which I have to use a matrix to represent a file in code. because it's a file, the size of the matrix is undefined, and therefore the matrix has to be dynamic. I found out that my compiler doesn't like dynamic multidimensional arrays, so I was thinking of this matrix as a dynamic (monodimensional) array of other dynamic (monodimensional) arrays. My program (and thus this example) uses unsigned chars.
unsigned char variable=something;
unsigned char**matrix=new unsigned char*[lenghtOfMainArray];
for(int rowNumber=0;rowNumber<lenghtOfArray;rowNumber++)
{
[Code].....
View 2 Replies
View Related
May 22, 2014
I have a matrix class who use the stl vector to manage its
memory(UInt is typedef of size_t):
class Mtrx {
protected:
[Code]....
However, the two different classes are only different with each other on the allocator. I am thinking, that a elegant way should be merging them together.
View 1 Replies
View Related
Mar 30, 2014
To my understanding, a Container should have an Allocator member data, which is used internally.
Question: what happens, for example, when we swap() two containers that use different allocators?
Must the allocators be swapped as well?
Must all elements of the container be reallocated when the allocator is changed?
View 7 Replies
View Related
Sep 4, 2013
I just wondering if there's a way in C++ to get classes dynamically which have the same base class? I mean, instead of creating switch statement to create multiple classes, I would like to use a single line to create any of these classes.
//Instead of:
switch(class_type) {
case type_1:
SubClass_1 obj = new SubClass_1();
[code].....
View 4 Replies
View Related
Mar 31, 2014
Consider a singly-linked list container.
Being constructed with an alloc<T>, it will rebind it as alloc<node<T>> for internal use.
Question: is allocator_type the same as alloc<T> or as alloc<node<T>>?
View 2 Replies
View Related
Mar 27, 2014
I am coding a singly-linked list container. Of course, internally it uses Node<T>.
Question: what is the correct way to use the allocator given by the user? I ask, because I've read this on the rival C++ Reference:
std::list<T, A> allocates nodes of some internal type Node<T>, using the allocator std::allocator_traits<A>::rebind_alloc<Node<T>>, which is implemented in terms of A::rebind<Node<T>>::other if A is an std::allocator
[URL]...
The above doesn't seem right to do, because then what should pointer and const_pointer be?
using pointer = std::allocator_traits<Alloc>::pointer;
using const_pointer = std::allocator_traits<Alloc>::const_pointer;
// but we're using Alloc<Node<T>> not Alloc<T>
// so maybe do this?
using pointer = value_type *;
using const_pointer = const value_type *;
View 6 Replies
View Related
Apr 17, 2014
I'm having problems with this code:
#include <iostream>
using namespace std;
class Foo {
public:
Foo( int n );// Constructor
~Foo();// Destructor
int *ptr;
int N;
[Code] ....
I'm using Visual C++ 2008 version. The problem arises at the end, after the sentence 'system("pause")' is reached, which makes me think that the problem happens when calling the destructor. The destructor is called twice, the first time it's called is in the function print. The problem seems to be that the destructor can only be called once.
I know I can avoid this situation by defining the function print like this:
void print ( const Foo &f )
...
but I would like to know if there is some way I can do this keeping the definition that I've provided.
View 2 Replies
View Related
Apr 25, 2014
Class with Pointers and Dynamic Arrays
View 2 Replies
View Related
Mar 27, 2014
I have become overwhelmed an frustrated at these current operator overloads!
Currently I still can not get my +,* or istream operators working at all!
#include <iostream>
#include <cstring>
#include "myint.h"
[Code]....
View 1 Replies
View Related
Feb 6, 2012
I want to allocate dynamically classes from a table. There is a different number of class derived from the same father class. Actually I do:
Code:
typedef struct sSysFBList {
TCHAR name[64];
int dim;
SystemFB *fbExec;
}systemFBCalls;
[Code] ....
and so on. NOTE: SystemFB is the virtual father class, all the other are derived from it.
I would like to know if there is the possibility to define the type of the class in the systemFBCalls struct to avoid all these if - else if - else if
Code:
typedef struct sSysFBList {
TCHAR name[64];
int dim;
????? name_of_class;
SystemFB *fbExec;
[Code] .....
View 4 Replies
View Related
Oct 15, 2013
How I can implement it.
Tickable.h
#include <list>
#ifdef TICKABLE_EXPORTS //Automatically defined by MSVS
#define DLL __declspec(dllexport)
#else
#define DLL __declspec(dllimport)
#pragma comment(lib, "Tickable.lib")
#endif
class DLL Tickable{
[Code] ....
error LNK2001:
unresolved external symbol "private: static class std::list<class Tickable*,SKIPPED BITS> Tickable::subs" HUGE_SYMBOL_LIST
PATHTickable.obj
I know with such a tiny and insignificant class the dll export seems pointless but this class is actually intended to be a .lib ONLY. But it is derived from by .dll style classes, and through inheritance this error is the exact same as what appears in the derived class, I just imagine that the cut down version would be easier to work with.
Is it possible to hold either a static variable in a dll which is of a dynamic type, OR would it be possible to reference an external variable which is static throughout the instances and this variable can be chucked away in a namespace of mine somewhere?
I suppose my only other option (if this is possible) would be to define a maximum instance number and create a standard array of pointers but this could both waste so much memory when not in use and cause problems if I need more memory.
View 5 Replies
View Related
Mar 13, 2013
But it can the other way around
Code:
static_Array= dynamic_Array;
dynamic_Array = static_Array;
The second statement works and i'm able to print out both arrays with equal values but with the first
[code] static_Array = dynamic_Array;I get incompatible types in assignment of 'int*' to 'int [7]' is the error I get [/code]
View 2 Replies
View Related