LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   How to initialize a static array of a class in a static member function (http://www.linuxquestions.org/questions/programming-9/how-to-initialize-a-static-array-of-a-class-in-a-static-member-function-621342/)

lali.p 02-15-2008 07:09 AM

How to initialize a static array of a class in a static member function
 
Hi
What i want to do is initialize a static array that i have in class and initialize it using a static memmber function of that class.

Here's what i want:[HTML]
// This is .h file
class Test
{
private:
const static int size=10;
static int a[size];// Array Declaration
static void initArray();
public:
Test();
}

// This is .cpp file
#include"Test.h"
int Test::a[size];// Array Definition
Test::Test()
{
initArray();// To initialize the array members
}
void Test::initArray()
{
for(int i=0;i<size;i++)
{
a[i]=i;
}
}
[/HTML]
However the above code doesnt compile and i want to know WHY

I know that if i want to initialize the array i can do the following in .cpp file
int Test::a[size]={1,2,3,4,5,6,7,8,9,10};
However this does not solve my purpose. As if i change the value of size later i would have to make a change in this initialization as well.

Moreover i want to know why the hell C++ doen't allow this to happen :p

Ohh ta0kira the geek where are you <just kidding>

Please help :-(

Nylex 02-15-2008 07:14 AM

It compiles fine for me. The only thing I can see that you're missing is the semi-colon at the end of the class definition, i.e.

Code:

class Test
{
  ...
};


lali.p 02-15-2008 07:51 AM

Hi I Am really Sorry It works even for me but What i want is that instead of int i want pthread_mutex_t i.e

THE FOLLOWING IS THE CODE HAVING PROBLEMS I AM SORRY FOR THE ABOVE CORRECT CODE
// .h file
[HTML]
#include<pthread.h>
class Test
{
private:
static const int size=10;
static pthread_mutex_t a[size] //i.e i want an array of mutex locks
static void initArray();
public:
Test();
};
// This is the .cpp file i.e implementation file

#include"Test.h"
pthread_mutex_t Test::a[size]//This is the definition of the array;
void Test::initArray()
{
for(int i=0;i<size;i++)
{
a[i]=PTHREAD_MUTEX_INITIALIZER;// If i comment this line code works fine and this is the line compiler says as error
}
}
Test::Test()
{
initArray();
}
[/HTML]

Now this code really gives me stupid errors. Very simple errors.

I know that there is something wrong with my usage of pthread_mutex_t or PTHREAD_MUTEX_INITIALIZER.


However the code works fine if i do the initialization as ;
pthread_mutex_t Test::a[size]={PTHREAD_MUTEX_INITIALIZE,...10 times}

However i dont want this and want that my array of mutex locks shd be initialized in my static function initArray so that when i change size i dont have to edit the initialization again
.


Please guide me i have been trying this for past 3 hours


Also i dont know much about threaded programming and i guess thats what is causing the problem.

Please help this newbie.

I am sorry for my earlier blunder


Regards
lali.cppx

Nylex 02-15-2008 07:57 AM

Posting the compiler errors would help..

cicorino 02-15-2008 08:08 AM

it's just the initializer to give problems
 
Hy,
I also had problems using the PTHREAD_MUTEX_INITIALIZER
inside the code, so I always use the pthread_mutex_init().
In your case you just need to write:

pthread_mutex_init( &(a[ i]), NULL);

lali.p 02-15-2008 08:22 AM

Hi thanks for that "REAL TIME" reply.


This is a great site man. Never ben disappointed. NEVER !!!!


Anyway, i read on net about using PTHREAD_MUTEX_INITIALIZER when the lock has been declared as static.
And also i overheard some 1 about using only PTHREAD_MUTEX_INITIALIZER.

May be i am wrong.


So some 1 please provide me with details and any nice material on pthreads and how to use them in C++ would be appreciated i.e how to use the pthread library i. i want some book or any online material. I have googled but want your suggestions as well.

Regards
lali.cpp

ta0kira 02-15-2008 11:21 AM

PTHREAD_MUTEX_INITIALIZER is a macro for something like {{0,0,0,0,0{0}}} which is a C-struct initializer and can't be used except at the point of definition.

It's very bad to have the constructor of every instance call the init function without some sort of control. That function either needs a static flag that says when it's been inited, or (see next):
Code:

void Test::initArray()
{
    static bool init_done = false;

    if (!init_done)
    {
    for(int i=0;i<size;i++)
    pthread_mutex_init(&a[i], NULL);

    init_done = true;
    }
}

Or:
Code:

void Test::initArray()
{
    static bool init_done = false;

    if (!init_done)
    {
    pthread_mutex_t temp_mutex = PTHREAD_MUTEX_INITIALIZER;

    for(int i=0;i<size;i++)
    memcpy(&a[i], &temp_mutex, sizeof(temp_mutex));

    init_done = true;
    }
}

Lastly, if you really need these inited regardless of if there's a class instance out there you can abuse the system like this:
Code:

int Test::initArray()
{
    static bool init_done = false;

    if (!init_done)
    {
    for(int i=0;i<size;i++)
    pthread_mutex_init(&a[i], NULL);

    init_done = true;
    }

    return 0;
}

static const int mutex_init_function_abuse = Test::initArray();

That will only be effective if no other static object will require access to the mutexes before the beginning of main.
ta0kira

ta0kira 02-15-2008 11:47 AM

Quote:

Originally Posted by lali.p (Post 3058038)
So some 1 please provide me with details and any nice material on pthreads and how to use them in C++ would be appreciated i.e how to use the pthread library i. i want some book or any online material. I have googled but want your suggestions as well.

If you look at info libc there should be a pthread reference in there. It shows up very nicely in Konqueror under info:libc.

The main thing to remember is that it's C++-style to use as little static data as possible, but most Unix C requires a lot of static data, as well as initializer functions that need to be explicitly called somehow by main. This is the only safe way to make sure everything is initialized properly, especially when you have 10+ source files.

If you're interested in creating a "thread class" then take a look at this thread:
Trouble when try to implement a timer thread using C++

You always need to keep in mind that threads are global, and therefore exist outside of all other threads. This means that as functions (and even if/for/while blocks) begin and end and their stack objects come and go, outside threads aren't aware of that. That's why most of the outside data actually accessed by a thread needs to be static, so that other threads' stack states don't cause the thread in question to access data that's no longer there.
ta0kira

lali.p 02-15-2008 11:18 PM

It's very bad to have the constructor of every instance call the init function without some sort of control. That function either needs a static flag that says when it's been inited, or (see next):
Code:

void Test::initArray()
{
static bool init_done = false;

if (!init_done)
{
for(int i=0;i<size;i++)
pthread_mutex_init(&a[i], NULL);

init_done = true;
}
}


Thanks ta0kira. Actually it was my mistake. What i am doing is that this CTOR is being called from getinstance static function of a singleton class. So this ctor is going to be called only once.

Thanks for for indepth insight.

Moreover, i would like some suggestiosn from you.

What i have been always curious is to see how STL is being implemented behind the scenes.
However i have googled a lot but not seen a single implementation i.e behind the scenes source code to say implement map or set or say vector.


And if STL just specifies the interface and every1 can just implement their own implementation don't you think that it would be inefficient.

Like for some compilers STL provided would be efficient and for some it won't be efficient

I may be totally wrong in whatever i have mentioned above. So please provide details etc.

Also i have seen sgi site but even there implementation is not there.
Who is the one who makes sure that STL is efficient.

I just love STL. And in future even i want to have STL like generic classes. So was a bit curious.

PLease provide details

Thanks & Regards

lali.cpp

ta0kira 02-16-2008 09:27 AM

Quote:

Originally Posted by lali.p (Post 3058861)
Thanks ta0kira. Actually it was my mistake. What i am doing is that this CTOR is being called from getinstance static function of a singleton class. So this ctor is going to be called only once.

Why not make the array non-static then? That will also allow you to use it in another source for the same project later on if you want to.
Quote:

Originally Posted by lali.p (Post 3058861)
And if STL just specifies the interface and every1 can just implement their own implementation don't you think that it would be inefficient.

Templates aren't actually classes/functions. They are literally templates for creating real classes and functions. When you declare or define an object using a template then that template is "instantiated." Nothing really happens then except for creation of code associated with the constructor, destructor, and template functions they might use. Each time you use a template function or call a function on a template-derived object, that function is instantiated, and therefore code is created for it. Unused functions literally aren't compiled if they aren't used (but they can cause parsing errors.) Definitions of virtual functions should be considered "used" because the vtable of the class will need them.

In order to be able to instantiate a template, its code must be out in the open. That means that the source code for the STL is in the STL headers you actually include. Unlike C headers that have libc behind them, all of the templates in the STL define their functions in the same place they declare them. Take a look at /usr/include/c++/*. Most of those files will include something from a subdirectory which is where most of the code is defined.

Templates are normally defined only in headers with no source files. This is because templates are generally inlined, so you don't have to worry about multiple definition linking errors. Also, some sources might use functions that others don't and therefore will need to force instantiation.
ta0kira


All times are GMT -5. The time now is 03:58 AM.