Jump to content
  • Advertisement
Sign in to follow this  
Rattrap

static const member array, C++ question

This topic is 4832 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been working on create a small cryptography library. A lot of the various algorithms use lookup tables. I was thinking about implementing these tables using static const arrays
class CRijndael
{
    //  Leaving out constructors and other methods
    protected:
        static const unsigned char SBox[256];
        static const unsigned char ReverseSBox[256];
};


My problem is that I want to make a library of these kinds of algorithms, which means most of them wouldn't likely be used in an application. From what I understand, since these arrays are declared static, they will be initialized with the application, whether they are used or not. In the case of the CRijndael example, that is about 2k of memory that would be allocated that really doesn't need to be, not to mention any other similar classes that also have these static lookup tables. I'm basically looking for opinions here. Would it make more sense to use a dyanmic array or a non static array and populate it in a constructor? I'm starting to think it would, especially since it isn't likely that there would be multiple instances of each class.

Share this post


Link to post
Share on other sites
Advertisement
I wouldn't worry about it. A decent linker will detect and remove unused code, at least for whole compilation units.
And even if they're used one rare code path by the executable the loader will only page in the parts you're actually using.

The only reasons I can think of for avoiding static arrays would be to save space on disk or avoid having to manage a separate application for precalculating them.
Quote:
Original post by Rattrap
In the case of the CRijndael example, that is about 2k of memory that would be allocated that really doesn't need to be
Looks more like 512 bytes too me.

Share this post


Link to post
Share on other sites
A note:
static const unsigned char SBox[256];

chars are unsigned by default, so you do not need to tell it explicitly.

As for your problem, there is a solution (if I understood it right): instead of declaring as static the arrays, you can create a new structure like the following:


struct A
{
char *SBox;
char *ReverseSBox;
};




Then, in you class:

class CRijndael
{
// Leaving out constructors and other methods
protected:
static A tables;
};




When you need the look-up tables, you initialize them. Since all objects share 'tables', you have only one instance of the tables, and you can create themonly if you need them.
Of, course, you have to change this example to manage that the A object is correctly initializated and destructed when noone need them. You should be able to add a variable (like a reference counter) to the A structure. You increase this counter every time an object CRijndael is created and you decrease it when an object is deleted. When it reaches 0, your tables could be freed (you can free them before, if you don't need them).
Hope this helps.

Share this post


Link to post
Share on other sites
Quote:
Original post by cignox1
chars are unsigned by default, so you do not need to tell it explicitly.
Nope, they can be either actually. It's one of those unique properties of chars.
There's no way to know except specifying (un)signed explicitly.

Share this post


Link to post
Share on other sites
Quote:

Nope, they can be either actually. There's no way to know except specifying (un)signed manually.


Well, then sorry (I always thought that they were unsigned due to the standard)...

Share this post


Link to post
Share on other sites
Quote:
Original post by doynax
Quote:
Original post by cignox1
chars are unsigned by default, so you do not need to tell it explicitly.
Nope, they can be either actually. It's one of those unique properties of chars.
There's no way to know except specifying (un)signed explicitly.

There is a way to check, actually:


#include <limits>
#include <iostream>

int main()
{
// Results in true if char is signed (useable at compile-time)
bool const char_is_signed = ::std::numeric_limits< char >::is_signed;
}





Also, keep in mind that whether a char is signed or unsigned, it is still a completely different type from signed char and unsigned char and it cannot be used interchangeably with either (particularly noticeable in terms of template instantiation and passing by reference).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!