why i get same values inside hash table? - c

I created hash table to insert my values . But when I insert more than one value I got same values inside all fields . my code is here :
create a structure for both user and hashtable
struct UserNode
{
char *username;
char *password;
};
struct HashTable
{
int size;
struct UserNode *table;
};
// initialize the hash table as NULL
struct HashTable* initializeTable(int size)
{
struct HashTable *htable;
int i = 0;
if (size < MIN_TABLE_SIZE)
{
printf("Table Size Too Small\n");
return NULL;
}
htable = malloc(sizeof(struct HashTable));
if (htable == NULL)
{
printf("Out of Space\n");
return NULL;
}
htable->size = size;
htable->table = malloc(size * sizeof(struct UserNode));
if (htable->table == NULL)
{
printf("Table Size Too Small\n");
return NULL;
}
for (i = 0; i < htable->size; i++)
{
htable->table[i].username= malloc(20 * sizeof(char));
htable->table[i].password = malloc(20 * sizeof(char));
htable->table[i].username=NULL;
htable->table[i].password=NULL;
}
printf("Hsh table sucessfully created\n");
return htable;
}
insert each user name to hash table
int Insert(char *key,char *password,struct HashTable *htable)
{
int pos = 0;
pos = Find(key,htable);
printf("the value of key : %d\n",pos);
if ((htable)->table[pos].username == NULL)
{
(htable)->table[pos].username,key ;
(htable)->table[pos].password = password;
}
else
{
printf("Duplicate element ..\n");
}
return 0;
}
This function to display the hash table
void Retrieve(struct HashTable *htable)
{
int i=0;
for (i = 0; i < htable->size; i++)
{
if (htable->table[i].username == NULL)
printf("Position: %d \tusername : NULL\tpassword: NULL\n",i + 1);
else
printf("Position: %d \t username: %s\tpassword: %s\n",i + 1,htable->table[i].username,htable->table[i].password);
}
}
and I am calling these function from main function as :
............main codes............
...............................
case 1:
printf("Enter size of the Hash Table:\n");
scanf("%d",&size);
htable = initializeTable(size);
break;
case 2:
if (i > htable->size)
{
printf("Table is Full, Rehash the table\n");
continue;
}
printf("Enter the username:\n");
scanf("%s",&username);
printf("Ebter the password:\n");
scanf("%s",&password);
Insert(username,password,htable);
i++;
break;
case 3:
printf("Display\n");
Retrieve(htable);
break;
But when I insert more username and password inside structures via insert function , I got same values in both fields . that is the new one is overwrite the previous one and display both value as new username . why ? an problem on my code ?

Two things:
You have the code:
htable->table[i].username= malloc(20 * sizeof(char));
htable->table[i].password = malloc(20 * sizeof(char));
htable->table[i].username=NULL;
htable->table[i].password=NULL;
First you allocate memory, then you immediately overwrite the pointers with NULL making you loose the memory you just allocated, leading to a leak and possible undefined behavior (and probable crashes) if you use the pointers without checking for NULL.
If you are going to allocate fixed sizes for the username and password members of the UserNode structure, why not make them arrays? Like
struct UserNode
{
char username[20];
char password[20];
};
This will, incidentally, also solve the first problem since you no longer need to allocate memory. It will also lessen memory fragmentation.

In Insert(),
if ((htable)->table[pos].username == NULL)
{
(htable)->table[pos].username,key ;
(htable)->table[pos].password = password;
The (htable)->table[pos].username,key; line just did nothing. Also, strings should be copied through strcpy() or strncpy(), not through pointer assignment.
So it should be:
if (htable->table[pos].username == NULL)
{
htable->table[pos].username = malloc(strlen(key) + 1);
strcpy(htable->table[pos].username, key);
htable->table[pos].password = malloc(strlen(password) + 1);
strcpy(htable->table[pos].password, password);
}

Related

Why does the returned array differ from the array that uses the return value in my program?

I have a program where I want to add a new element into an array of structs.
In my add function it seems to be working, but when I try to use it in the main function, it seems to differ from what's being originally returned.
I have my main.c like this:
int main(void) {
Recept * receptek;
int valasztas;
int *hossz = 0; //A receptek száma.
receptek = betolt("receptek.txt", &hossz);
if (receptek != NULL) {
menu();
printf("Valasztott opcio: ");
while(scanf("%d", &valasztas) == 1) {
if(valasztas == 1) {
Recept * uj = recept_felvesz(receptek, hossz);
printf("%dTH VALUE HERE: %s\n", (int)hossz, uj[(int)hossz].nev);
if(uj != NULL) {
printf("Recept sikeresen felveve.\n\n");
hossz = (int)hossz + 1;
} else {
printf("Nem sikerult a receptet felvenni.\n\n");
}
} else if(valasztas == 2) {
kiir(receptek, (int)hossz);
} else if(valasztas == 6) {
break;
} else {
system("cls");
menu();
printf("Ervenytelen opcio.\n\n");
}
printf("Valasztott opcio: ");
}
} else {
printf("Nem sikerult betolteni a tarolofajlt!\n");
}
return 0;
}
And I store my add function (recept_felvesz) in a different .h file.
Recept* recept_felvesz(Recept* receptek, int* hossz) {
printf("Hossz 1: %d", (int)hossz);
setbuf(stdin, NULL);
char bekert_nev[30];
char bekert_ot[300];
printf("Add meg a recept nevet: ");
if(fgets(bekert_nev, 30, stdin))
bekert_nev[strcspn(bekert_nev, "\n")] = '\0'; //A \n karaktert kicseréli \0-ra
printf("Add meg a recept osszetevoit: ");
if(fgets(bekert_ot, 300, stdin))
bekert_ot[strcspn(bekert_ot, "\n")] = '\0'; //A \n karaktert kicseréli \0-ra.
system("cls");
menu();
Recept uj = {(int)hossz, bekert_nev, bekert_ot};
receptek = (Recept*) realloc(receptek, ((int)hossz + 1) * sizeof(Recept));
receptek[(int)hossz] = uj;
if (receptek == NULL) return NULL;
FILE *file = fopen("receptek.txt", "a");
if (file == NULL) return NULL;
fprintf(file, "\n%s;%s;", bekert_nev, bekert_ot);
fclose(file);
printf("\n%dTH VALUE HERE: %s\n", (int)hossz, receptek[(int)hossz].nev);
return receptek;
}
Here is the buggy output at the moment:
15TH VALUE HERE: 32423
15TH VALUE HERE: ☺
Recept sikeresen felveve.
Why does the actual return value (32423) differ from what's being used in the main function(☺)?
Edit: structure Recept looks like this:
typedef struct Recept {
int azonosito;
char *nev;
char *osszetevok;
} Recept;
In this code memory of local variables bekert_nev and bekert_ot are used outside of the scope where they are defined (function recept_felvesz). They are allocated on stack so after exit from function recept_felvesz their memory can be used by anyone else as it is considered unused. For example printf can store its internal variables in this memory block.
One way to fix this issue is to allocate bekert_nev and bekert_ot in global memory using malloc function:
char* bekert_nev = malloc(30);
char* bekert_ot = malloc(300);
Remember to free this memory with free function when you will destroy your main receptek array:
Recept* rec = receptek;
for( int i = 0; i < hossz; ++i) {
free( rec->nev );
free( rec->osszetevok );
rec++;
}
Alternatively, you can put this arrays inside of your Recept struct and write directly into them
typedef struct Recept {
int azonosito;
char nev[30];
char osszetevok[300];
} Recept;

Issue with hashtable in c

So I have an assignment to create a program in c that reads a couple of sentences(a 140mb file), and based on the 2nd input, which is a number, I need to return the Nth most common word. My idea was to build a hash table with linear probing, every time I get a new element I hash it accordingly based its position and based on djb2, else if there is a collision I rehash. After that, I apply Quicksort based on the occurrence and then I finally access by index.
I am having issues finishing up a hash table with linear probing in c. I am pretty sure I have finished it but every time I run I am getting a heap buffer overflow on lldb. I tried to spot the issue but I still cannot figure it out.
Am I getting out of memory on stack? The file is relatively small to consume so much memory.
I used address sanitiser and I got a heap-buffer-overflow on inserting.
I don't think I am touching the memory outside the allocate region but I am not 100% sure.
Any idea what has gone wrong? This is the table.c implementation and below that you can see the form of the struct.
Here is a more detailed message from address sanitiser:
thread #1: tid = 0x148b44, 0x0000000100166b20 libclang_rt.asan_osx_dynamic.dylib`__asan::AsanDie(), queue = 'com.apple.main-thread', stop reason = Heap buffer overflow
{
"access_size": 1,
"access_type": 1,
"address": 105690555220216,
"description": "heap-buffer-overflow",
"instrumentation_class": "AddressSanitizer",
"pc": 4294981434,
"stop_type": "fatal_error"
}
table.c :
#include "table.h"
#include "entities.h"
static inline entry_t* entryInit(const char* const value){
unsigned int len = strlen(value);
entry_t* entry = malloc(sizeof(entry));
entry->value = malloc(sizeof(char*) * len);
strncpy(entry->value, value, strlen(value));
entry->exists = 1;
entry->occurence = 1;
return entry;
}
table_t* tableInit(const unsigned int size){
table_t* table = malloc(sizeof(table_t));
table->entries = malloc(size*sizeof(entry_t));
table->seed = getPrime();
table->size = size;
table->usedEntries = 0U;
return table;
}
//okay, there is definitely an issue here
table_t* tableResize(table_t* table, const unsigned int newSize){
//most likely wont happen but if there is an overflow then we have a problem
if(table->size > newSize) return NULL;
//create a temp array of the realloced array, then do changes there
entry_t* temp = calloc(newSize,sizeof(entry_t));
table->size = newSize;
//temp pointer to an entry
entry_t *tptr = NULL;
unsigned int pos = 0;
unsigned int index = 0;
while(pos != table->size){
tptr = &table->entries[pos];
if(tptr->exists == 1){
index = hashString(table->seed, tptr->value, table->size, pos);
temp[index] = *entryInit(tptr->value);
temp[index].occurence = tptr->occurence;
break;
}
else pos++;
}
table->entries = temp;
//TODO: change table destroy to free the previous array from the table
free(temp);
return table;
}
//insert works fine, it is efficient enough to add something in the table
unsigned int tableInsert(table_t* table,const char* const value){
//decide when to resize, might create a large enough array to bloat the memory?
if(table->usedEntries >(unsigned int)(2*(table->size/3))) table = tableResize(table, table->size*2);
entry_t* entry = NULL;
unsigned int index;
auto int position = 0;
while(position != table->size){
//calculate the hash of our string as a function of the current position on the table
index = hashString(table->seed,value,table->size, position);
entry = &table->entries[index];
if(entry->exists == 0){
*entry = *entryInit(value);
table->usedEntries++;
return index;
} else if (entry->exists == 1 && strcmp(entry->value, value) == 0){
entry->occurence++;
return index;
} else{
position++;
}
}
}
//there might be an issue here
static inline void tableDestroy(const table_t* const table){
entry_t* entry = NULL;
for (auto int i = 0; i < table->size; ++i){
entry =&table->entries[i];
//printf("Value: %s Occurence: %d Exists: %d \n",entry->value, entry->occurence, entry->exists );
if(&table->entries[i] !=NULL)free(&table->entries[i]);
}
free(table);
}
entities.h :
#pragma once
typedef struct __attribute__((packed)) __entry {
char *value;
unsigned int exists : 1;
unsigned int occurence;
} entry_t;
typedef struct __table {
int size;
int usedEntries;
entry_t *entries;
unsigned int seed;
} table_t;
here is how I read from a file and process the text:
void readFromFile(const char* const fileName, table_t* table){
FILE *fp = fopen(fileName, "r");
if(!fp) fprintf(stderr,"error reading file. \n");
char word[15];//long enough to hold the biggest word in the text?
int position = 0;
char ch;
while((ch = fgetc(fp))!= EOF){
//discard all the ascii chars that are not letters
if(!(ch >= 65 && ch <= 90) && !(ch >= 97 && ch <= 122)){
word[position]= '\0';
if(word[0] == NULL)continue;
tableInsert(table, word);
position = 0;
continue;
}
else word[position++] = ch;
}
}
Any suggestions what is wrong with my code?
I believe resize might have an issue and I am not properly deleting yet because I have had a lot of problems with the memory management.
Thanks in advance!

how do i fix this malloc error occurred while hashing

trying to implement a dictionary using a hash table after a lot of debugging getting stuck with this malloc error
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/* to store a data (consisting of key and value) in hash table array */
struct item
{
char key[100];
char value[1000];
};
/* each hash table item has a flag (status) and data (consisting of key and value) */
struct hashtable_item
{
int flag;
/*
* flag = 0 : data does not exist
* flag = 1 : data exists at given array location
* flag = 2 : data was present at least once
*/
struct item *data;
};
struct hashtable_item *array;
int size = 10007;
int max = 10007;
/* this function returns corresponding index of the given key */
int hashcode(char* key,int TS)
{
return (key[0]+27*key[1]+729*key[2])%TS;
}
/* this function initializes the hash table array */
void init_array()
{
int i;
for (i = 0; i < max; i++)
{
array[i].flag = 0;
array[i].data = NULL;
}
}
/* this function inserts an element in the hash table */
void insert(char* key, char* value,int TS)
{
int index = hashcode(key,TS);
int i = index;
int h = 1;
struct item *new_item = (struct item*) malloc(sizeof(struct item));
strcpy(new_item->key , key);
strcpy(new_item->value , value);
/* probing through the array until an empty space is found */
while (array[i].flag == 1)
{
if (array[i].data->key == key)
{
/* case when already present key matches the given key */
printf("\n This key is already present in hash table, hence updating it's value \n");
strcpy(array[i].data->value , value);
return;
}
i = (i + (h * h)) % max;
h++;
if (i == index)
{
printf("\n Hash table is full, cannot add more elements \n");
return;
}
}
array[i].flag = 1;
array[i].data = new_item;
printf("\n Key (%s) has been inserted\n", key);
size++;
}
/* to remove an element form the hash table array */
void remove_element(char* key,int TS)
{
int index = hashcode(key,TS);
int i = index;
int h = 1;
/* probing through the hash table until we reach at location where there had not been an element even once */
while (array[i].flag != 0)
{
if (array[i].flag == 1 && array[i].data->key == key)
{
/* case where data exists at the location and its key matches to the given key */
array[i].flag = 2;
array[i].data = NULL;
size--;
printf("\n Key (%s) has been removed \n", key);
return;
}
i = (i + (h * h)) % max;
h++;
if (i == index)
{
break;
}
}
printf("\n Key does not exist \n");
}
/* to display the contents of hash table */
void display()
{
int i;
for(i = 0; i < max; i++)
{
if (array[i].flag != 1)
{
printf("\n Array[%d] has no elements \n", i);
}
else
{
printf("\n Array[%d] has elements \n %s (key) and %s (value) \n", i, array[i].data->key, array[i].data->value);
}
}
}
int size_of_hashtable()
{
return size;
}
main function
void main()
{
int choice,n, c;
char key[100];
char value[1000];
int TS=10007;
array = (struct hashtable_item*) malloc(max * sizeof(struct hashtable_item*));
init_array();
do {
printf("Implementation of Hash Table in C with Quadratic Probing.\n\n");
printf("MENU-: \n1.Inserting item in the Hash table"
"\n2.Removing item from the Hash table"
"\n3.Check the size of Hash table"
"\n4.Display Hash table"
"\n\n Please enter your choice-:");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Inserting element in Hash table \n");
printf("Enter key:");
fgets(key,100, stdin);
printf("Enter value:");
fgets(value,1000, stdin);
insert(key, value,TS);
break;
case 2:
printf("Deleting in Hash table \n Enter the key to delete-:");
fgets(key,100, stdin);
remove_element(key,TS);
break;
case 3:
n = size_of_hashtable();
printf("Size of Hash table is-:%d\n", n);
break;
case 4:
display();
break;
default:
printf("Wrong Input\n");
}
printf("\n Do you want to continue-:(press 1 for yes)\t");
scanf("%d", &c);
}while(c == 1);
}
Implementation of Hash Table in C with Quadratic Probing.
this is how the output looks like
MENU-:
1.Inserting item in the Hash table
2.Removing item from the Hash table
3. Check the size of the Hash table
4.Display Hash table
Please enter your choice-:1
Inserting element in Hash table
Enter key:Enter value:john helllo we aer
a.out: malloc.c:2374: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end `enter code here`& pagemask) == 0)' failed.
Aborted (core dumped)
Usually sysmalloc errors are thrown when you corrupt some memory, like writing or reading from unallocated space and etc.
The first thing I noticed is that the allocation of array is not quite correct. I figured out that you need an array of struct hashtable_item-s but you are allocating space for an array of pointers.
sizeof(struct hashtable_item*) will always be 8 in x64 systems because it's the size of a pointer. you should probably use sizeof(struct hashtable_item) it will give you the size of actual struct object.
what you need is something like this:
array = (struct hashtable_item*) malloc(max * sizeof(struct hashtable_item));
But on the other hand as your array is static I'd recommend that you allocated it in stack instead of heap:
struct hashtable_item[10007];
Also if you could provide information about at which line is your insert() failing it would be appreciated.

Creating a dynamically expandable array of memory in C

In my below code I am trying to create a dynamically expandable array of memory.
#include <stdio.h>
#include <stdlib.h>
#define BLOCKSIZE 5
int hash_table_length = 0;
int *currentblock = NULL;
int size_left;
int *hash_table = NULL;
int *start = NULL;
int *create_hash_table() {
int *tmp;
if (currentblock == NULL || size_left == 0) {
if (currentblock == NULL) {
currentblock = (int *) malloc( BLOCKSIZE * sizeof(int));
start = currentblock;
size_left = BLOCKSIZE;
} else {
currentblock = (int *) malloc( BLOCKSIZE * sizeof(int));
size_left = BLOCKSIZE;
}
}
tmp = currentblock++;
size_left -= 1;
return tmp;
}
void build() {
int hash;
int i = 0;
for (i = 0; i < 20; i++) {
hash = i + 3;
if (hash_table_length == 0) {
hash_table = create_hash_table();
hash_table_length++;
} else {
hash_table = create_hash_table();
hash_table_length++;
}
hash_table = &hash;
printf("hash value is %d\n", *hash_table);
}
}
int main() {
build();
// How do I reach the start of the hash table again?
// the below start does not give me the first value
printf("Hash table first value is %d\n", *start);
return 0;
}
My problem here is I wish to traverse through the values stored in the hash_table. I am unable to reach to the first element/address of the hash_table. I wish to print out all the values stored in my hash table. How can this be done?
In your code the hash values never get stored inside the hash table(inside currentblock). Inside the create_hash_table() function you allocate memory for a new block but never store values inside this block. Thus if you try dereferencing any of these int* locations you might get a garbage value(which may be a 0).
This is what is precisely happening inside your main() function when you dereference the start pointer. It is infact pointing to the start of the hash table and as that location is uninitialized it gives an output of 0.
To actually store values inside the hash table change the following inside build():
hash_table = &hash;
to:
*hash_table = hash; // Store value of 'hash' inside the memory location pointed to by hash table(which happens to be 'current_block' inside build())
Now if you try running the code, it will output 3.
Coming to the second part of question as to how you'll traverse the entire hash table: It cannot be done using this code. This is because there is no linkage between your malloc'd blocks of integers. The malloc() call can assign any block of free memory from the heap. Thus in the current form you have disconnected blocks of locations which cannot be traversed.
Instead of malloc you can use realloc to increase the size of your current block. realloc allocates memory for the larger block and copies your previous data to this new block. This will essentially allow you to traverse the entire hash table using start.
Here is how you might do that:
#include <stdio.h>
#include <stdlib.h>
#define BLOCKSIZE 5
int hash_table_length = 0;
int *currentblock = NULL;
int size_left;
int *hash_table = NULL;
int *start = NULL;
int *create_hash_table() {
int *tmp;
if (currentblock == NULL || size_left == 0) {
if (currentblock == NULL) {
currentblock = (int *) malloc(BLOCKSIZE * sizeof(int));
start = currentblock;
size_left = BLOCKSIZE;
} else {
/* Call realloc() to allocate new memory block of size (hash_table_length+BLOCKSIZE) and copy previous data*/
currentblock = ((int *) realloc(start,(hash_table_length + BLOCKSIZE) * sizeof(int))) + hash_table_length;
size_left = BLOCKSIZE;
}
}
tmp = currentblock++;
size_left -= 1;
return tmp;
}
void build() {
int hash;
int i = 0;
for (i = 0; i < 20; i++) {
hash = i + 3;
if (hash_table_length == 0) {
hash_table = create_hash_table();
hash_table_length++;
} else {
hash_table = create_hash_table();
hash_table_length++;
}
/* Store value of hash inside the hash_table */
*hash_table = hash;
printf("hash value is %d\n", *hash_table);
}
}
int main() {
int i;
build();
printf("Hash table first value is %d\n", *start);
/* Traverse the hash table */
for(i = 0; i < hash_table_length; ++i)
printf("hash_table[%d] = %d\n",i,*start++);
return 0;
}

is it true to do this to malloc array with unknown size

I want to make an array with unknown size , is it true to make it like this ? :
int *array,var,i=0;
FILE *fp;
fopen=("/home/inputFile.txt","r");
fscanf(fp,"%d",&var);
while(fp!=NULL)
{
if(var>0)
{
array=malloc(sizeof(int));
array[i++]=var
}
fscanf(fp,"%d",&var);
}
This is absurdly false, full of memory leaks and undefined behaviors.
However, it's not that far from one of the right ways, the linked list way:
struct linked_int
{
int value;
struct linked_int* pNext;
};
struct linked_int *pHead=NULL;
struct linked_int **ppTail = &pHead;
int* array = NULL;
int cpt=0;
/*Read file, building linked list*/
FILE *fp = fopen("/home/inputFile.txt","r");
if(fp != NULL)
{
int var;
while(fscanf(fp,"%d",&var)==1)
{
if(var>0)
{
struct linked_int *pNew = malloc(sizeof(struct linked_int));
pNew->value = var;
pNew->pNext = NULL;
/*Append at the tail of the list*/
*ppTail = pNew;
ppTail = &(pNew->pNext);
cpt++;
}
}
fclose(fp);
}
/*Copy from the linked list to an array*/
array = malloc(sizeof(int) * cpt);
if(array != NULL)
{
int i;
struct linked_int const *pCur = pHead;
for(i=0 ; i<cpt ; i++)
{
arr[i] = pCur->value;
pCur = pCur->pNext;
}
}
/*Free the linked list*/
while(pHead != NULL)
{
struct linked_int *pDelete = pHead;
pHead = pHead->pNext;
free(pDelete);
}
ppTail = &pHead;
Other ways:
Another right way is the realloc way, which consists in re-allocating the array with an ever expanding size (usually with a geometric growth, i.e. multiplying the array size by a number such as 1.5 every time). A wrong way to do so is to add 1 to the array size every time.
It goes something like this:
int arrayCapacity=0, numberOfItems=0;
int* array = NULL;
int var;
while(fscanf(fp, "%d", &var)==1)
{
if(numberOfItems >= arrayCapacity)
{
/*Need to resize array before inserting*/
const int MIN_CAPACITY = 4;
const double GROWTH_RATE = 1.5;
int newCapacity = arrayCapacity<MIN_CAPACITY ? MIN_CAPACITY : (int)(arrayCapacity*GROWTH_RATE);
int* tmp = realloc(array, newCapacity*sizeof(int));
if(tmp==NULL)
{
/*FAIL: can't make the array bigger!*/
}
else
{
/*Successfully resized the array.*/
array = tmp;
arrayCapacity = newCapacity;
}
}
if(numberOfItems >= arrayCapacity)
{
puts("Cannot add, array is full and can't be enlarged.");
break;
}
else
{
array[numberOfItems] = var;
numberOfItems++;
}
}
/*Now we have our array with all integers in it*/
The obvious result is that in this code, there can be unused space in the array. This isn't a problem.
sizeof(int) will return you 4 (and note that few compilers/settings may say you 2 or 8 in response). So your code is equivalent to allocating a 4 bytes long array.
If you want an array with unknown size, it could be worth to take a loot at STL containers like std::vector (because it will manage allocations and resizes behind the scene). If you plan to stick with "plain C" scope, you may be interested with TSTL2CL library: http://sourceforge.net/projects/tstl2cl
The basic thing is, ARRAY is STATIC not DYNAMIC.

Resources