Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

global variables in C++

Tags:

c++

arrays

So I have something like this

#define HASHSIZE 1010081

static struct nlist *hashtab[HASHSIZE];

Now I want to be able to change the HASHSIZE of my hashtab, because I want to test different primes numbers and see which would give me less collisions. But Arrays do not take variable sizes so HASHSIZE has to be a constant. Is there a way to go about this?

like image 776
SuperString Avatar asked Nov 28 '22 04:11

SuperString


1 Answers

Why don't you use std::vector instead of using arrays in C++?

Eg:

  std::vector<nlist *> hashtab; 
  hashtab.resize(<some_value>); 

But anyways you can do this if you are using g++ because g++ supports Variable Length Arrays(VLAs) as an extension.

Eg:

  int HASHSIZE=<some_value>
  static struct nlist *hashtab[HASHSIZE];
like image 84
Prasoon Saurav Avatar answered Nov 30 '22 17:11

Prasoon Saurav