LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-15-2008, 07:09 AM   #1
lali.p
Member
 
Registered: Jan 2007
Distribution: Slackware 11.0
Posts: 141

Rep: Reputation: 16
Talking 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

Ohh ta0kira the geek where are you <just kidding>

Please help :-(
 
Old 02-15-2008, 07:14 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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
{
  ...
};
 
Old 02-15-2008, 07:51 AM   #3
lali.p
Member
 
Registered: Jan 2007
Distribution: Slackware 11.0
Posts: 141

Original Poster
Rep: Reputation: 16
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
 
Old 02-15-2008, 07:57 AM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Posting the compiler errors would help..
 
Old 02-15-2008, 08:08 AM   #5
cicorino
Member
 
Registered: May 2006
Location: Italy
Distribution: Slackware, Slackware64
Posts: 31

Rep: Reputation: 16
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);
 
Old 02-15-2008, 08:22 AM   #6
lali.p
Member
 
Registered: Jan 2007
Distribution: Slackware 11.0
Posts: 141

Original Poster
Rep: Reputation: 16
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
 
Old 02-15-2008, 11:21 AM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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

Last edited by ta0kira; 02-15-2008 at 11:28 AM.
 
Old 02-15-2008, 11:47 AM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by lali.p View Post
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
 
Old 02-15-2008, 11:18 PM   #9
lali.p
Member
 
Registered: Jan 2007
Distribution: Slackware 11.0
Posts: 141

Original Poster
Rep: Reputation: 16
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
 
Old 02-16-2008, 09:27 AM   #10
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by lali.p View Post
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 View Post
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ question, how to define a member array, and it's size, outside of the class dec.. Winter Knight Programming 2 01-23-2007 07:28 AM
passing a class member function to pthread_create. dmail Programming 1 07-29-2006 11:15 AM
C++: difference between static method variable and static class variable? Thinking Programming 3 01-16-2006 10:08 AM
static member and non-static member allomeen Programming 9 01-06-2006 05:31 PM
Array declaration in class or main function??? redhatrosh Programming 4 03-15-2005 02:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration