initialization

Library initialization -- pthread_once in Win32 implementation

Hello. I am trying to make a fully thread-safe initialization function for my library and I couldn't easily find an alternative to pthread_once, which should solve the problem very easily. I've come to this code: void libInit (void) { #ifdef WIN32 static volatile int initialized = 0; static HANDLE mtx; if (!initialized) ...

Initializing.. which one is more efficient?

I have the following question. Which one of these is better that should be followed and why? string strMyString = "SampleString"; or string strMyString("SampleString"); Thanks in advance. ...

Receiving a Java Method without using getDeclaredMethod

Hi, I wish to initialize an array of java methods in the child class, as a class field like so void callme() {System.out.println("hi!");} Method[] actions = new Method[] {&callme,&callme}; and call all methods in this array at parent class like so: for (meth:actions) {meth.invoke();} However currently I cannot find a way to implici...

Direct array initialization with a constant value

Whenever you allocate a new array in C# with new T[length] the array entries are set to the default of T. That is null for the case that T is a reference type or the result of the default constructor of T, if T is a value type. In my case i want to initialize an Int32 array with the value -1: var myArray = new int[100]; for (int i=0...

C# equivalent for C code needed

Can anybody tell me the C# equivalent for this C code? static const value_string message_id[] = { {0x0000, "Foo"}, {0x0001, "Bar"}, {0x0002, "Fubar"}, ... ... ... } ...

What's the difference between dict() and {}?

So let's say I wanna make a dictionary. We'll call it d. But there are multiple ways to initialize a dictionary in Python! For example, I could do this: d = {'hash': 'bang', 'slash': 'dot'} Or I could do this: d = dict(hash='bang', slash='dot') Or this, curiously: d = dict({'hash': 'bang', 'slash': 'dot'}) Or this: d = dict([['...

How to import initial data to database with Hibernate?

When deploying applications, I often use Hibernate’s capacity to create database schema in order to simplify the deployment. This is easily achievable by configuring hibernate.hbm2ddl.auto property. However, on occasion I also need to insert some initial data to database, for example root user. Is there a way I could achieve this via h...

Initialize final variable before constructor in Java

Is there a solution to use a final variable in a Java constructor? The problem, if I init. a final var. like: private final String name = "a name"; then I cannot use it in the constructor, while java first runs the constructor an then the fields... Is there a solution to get in contact with the final var. in the constructor? ...

Design Question: How to deal with initialization?

Lets assume we have a class which will be widely used throughout the (c#) code, for example a Log class. Say the Log writes entries into XML files in a specific directory. Now one attempt to force the user to initialize the class with the required information would be to make the default (parameterless) constructor private and provide on...

C#: List add object initializer

Lets say I have this class: class MyList<T> { } What must I do to that class, to make the following possible: var list = new MyList<int> {1, 2, 3, 4}; ...

Why should I initialize member variables in the constructor?

Something I've been taught when I first started with Object Oriented programming languages. When declaring a field in a class, don't initialize it yet, do that in the constructor. In C#: public class Test { private List<String> l; public Test() { l = new List<String>(); } } But when someone recently asked me why to do t...

How can I reduce Perl CGI script start-up time?

I'm developing some CGI scripts and I'm trying to find a solution to decrease the "starting time" produced when I import a lot of modules with "use". ...

Universal approach for storing INI-type settings and/or DB files on various Windows machines

OK, true confession first: Maybe it's just me, but sometimes "best practices for program settings" on Windows machines feels like it's changed more than Microsoft's data access strategies. I'm still running XP, and somewhere along the way just kind "glazed over" about where MS wanted me to store all my app's data, etc. I controlled all...

C++ template static pointer-to-member initialization

I have a template class which has a static pointer-to-member, like this: template<class T, T* T::*nextptr> class Queue { T* head; T* tail; static T* T::*pnext; }; My question is how to write the initializer of the static pointer-to-member. I tried the obvious case: template<class T, T* T::*nextptr> T* Queue<T, nextptr>::*...

int issue in g++/mysql/redhat

Hi - I haven't written C in quite some time and am writing an app using the MySQL C API, compiling in g++ on redhat. So i start outputting some fields with printfs... using the oracle api, with PRO*C, which i used to use (on suse, years ago), i could select an int and output it as: int some_int; printf("%i",some_int); I tried to d...

Why C# is always winning over VB.NET?

I wrote a program that allow two classes to "fight". For whatever reason C# always wins. What's wrong with VB.NET ? static void Main(string[] args) { Player a = new A(); Player b = new B(); if (a.Power > b.Power) Console.WriteLine("C# won"); else if (a.Power < b.Power) Cons...

Initializing JS components at the end of HTML or on "onload"?

For a while I had been running JavaScript component initialization by waiting for the "onload" event to fire and executing a main() of sorts. It seemed cleaner and you could be sure the the ID state of your DOM was in order. But after some time of putting that through its paces I found that the component's initialization was choked off...

How to work out 1D array If I can't predict it's length?

Duplicate Array of Unknown Length in C# How do I initialize string[] without a need of initializing the length? I want it to be dynamic array, so when I add something, Length increases and no Exception raises? Should I just use some kind of List? ...

Initializing PODs in C# - best practice

As far as I know[*], C# defaults the plain-old-data-types (int, float, bool, etc) to sensible values. int a, float b, and bool c take on the values 0, 0.0f, and false, respectively. Assuming this is so: Should I explicitly give my variables values of 0, 0.0f, and false for clarity, or should I leave the values implicit, thus reducing ...

Initialization and Assignment

I have some 'legacy' code (which I can't change, but need to add on to) that looks something like this: template<typename T> T Foo(T& target) { //Assign to 'target', but never read from it before that. //Also, 'target' is going to be a POD-type. target = T(); return target; } int main() { float value = Foo(value);...