initialization

How to initialize a struct in ANSI C?

Hi guys! I'm not a specialist for ANSI C (or regular C at all), so I stumbled about this stupid thing: I want to initialize a struct element, splitted in declaration and initialization. This is what I got: typedef struct MY_TYPE { boolean flag; short int value; double stuff; } MY_TYPE; void function(void) { MY_TYPE a; ... ...

Finding C++ static initialization order problems

We've run into some problems with the static initialization order fiasco, and I'm looking for ways to comb through a whole lot of code to find possible occurrences. Any suggestions on how to do this efficiently? Edit: I'm getting some good answers on how to SOLVE the static initialization order problem, but that's not really my questio...

Resetting a winform's elements to initialized state (C#/.Net)

I'm building a winform in C# with various elements in a panel that start out either invisible, disabled, or set to null (labels, combo boxes, grids, etc.). As the user goes through and makes choices, these elements are populated, selected, etc. The idea is to upload files, read them, and process entries to a database. Once the processi...

hibernate: LazyInitializationException: could not initialize proxy

Hi all, Here's one that has me perplexed. I'm trying to implement a basic Hibernate DAO structure, but am having a problem. Here's the essential code: int startingCount = sfdao.count(); sfdao.create( sf ); SecurityFiling sf2 = sfdao.read( sf.getId() ); sfdao.delete( sf ); int endingCount = sfdao.count(); assertTrue( startin...

Why must const members be intialized in the constructor initializer rather than in its body?

Why must class members declared as const be initialized in the constructor initializer list rather than in the constructor body? What is the difference between the two? ...

Uses for anonymous namespaces in header files

Someone asserted on SO today that you should never use anonymous namespaces in header files. Normally this is correct, but I seem to remember once someone told me that one of the standard libraries uses anonymous namespaces in header files to perform some sort of initialization. Am I remembering correctly? Can someone fill in the deta...

How to pass method result as parameter to base class constructor in C++?

I've trying to achieve something like this: class Base { public: Base(string S) { ... }; } class Derived: Base { public: int foo; string bar() { return stringof(foo); // actually, something more complex }; Derived(int f) : foo(f), Base(bar()) { }; } Now, this doesn't work as I want, because bar() is ca...

Delphi: App initialization - best practices / approach

I run into this regularly, and am just looking for best practice/approach. I have a database / datamodule-containing app, and want to fire up the database/datasets on startup w/o having "active at runtime" set to true at design time (database location varies). Also run a web "check for updates" routine when the app starts up. Given ...

pthread_mutex_t init error

I am using xcode 2.4.1 on tiger. When i do below everything is ok. when i do pthread_mutex_t mute; ImageMan() { dibSize=0; mute = PTHREAD_MUTEX_INITIALIZER; } I get these two errors error: expected primary-expression before '{' token error: expected `;' before '{' token I dont know why. However if i do pthread_mutex_t mute = PT...

splash screen w/ load assembly status

Hi, I'm trying to create a splash screen that shows assemblies (all referenced library) loading status. I use AppDomain.AssemblyLoad AssemblyLoadEventHandler delegate to catch what assembly is being loaded but the problem is the event is not triggered when the program initializes. I tried register the event handler in application startu...

Difference between declaring variables before or in loop?

Hi, I have always wondered if, in general, declaring a throw-away variable before a loop, as opposed to repeatedly inside the loop, makes any (performance) difference? A (quite pointless example) in Java: a) declaration before loop: double intermediateResult; for(int i=0;i<1000;i++){ intermediateResult = i; System.out.println...

Initialize multidimensional array

Checkbox[,] checkArray = new Checkbox[2, 3]{{checkbox24,checkboxPref1,null}, {checkbox23,checkboxPref2,null}}; // I am getting error . How do I initialize it? ...

Why can I assign an existing reference to a literal value in C++?

Consider the following: int ival = 1.01; int &rval = 1.01; // error: non-const reference to a const value. int &rval = ival; rval = 1.01; The first assignment of &rval to a literal value fails as expected. If I comment out that line the code compiles and runs. I understand why the initialization fails, but I'm confused why the assi...

Which event in the app's startup sequence is appropriate to trigger loading a config file in AIR/Flex?

I am working on a small AIR desktop application and I have some configuration infos that I want to store in a little file that's loaded at some point when the application starts and will be used to set public properties on the root application object. This should work just as if I had public variables declared in an <mx:Script> block at ...

How to initialize a List<T> to a given size (as opposed to capacity)?

.NET offers a generic list container who's performance is almost identical (see Performance of Arrays vs. Lists question). However they are quite different in initialization. Arrays are very easy to initialize with a default value, and by definition they already have certain size: string[] Ar = new string[10]; which allows one to sa...

What is the best way to initialize an array to a fixed-length array? (C++/CLI)

In managed C++/CLI, I could do this either as (1): array<System::Byte>^ css_keycode = {0x51, 0x67, 0x67, 0xc5, 0xe0, 0x00}; or (2): array<System::Byte>^ css_keycode; css_keycode = gcnew array<System::Byte>(6) {0x51, 0x67, 0x67, 0xc5, 0xe0, 0x00}; But I apparently can't do (3): array<System::Byte>^ css_keycode; css_keycode = {0x5...

How to Initialise a static Map in Java

How would you initialise a static Map in Java? Method one: Static initialiser Method two: instance initialiser (anonymous subclass) or some other method? What are the pros and cons of each? Here is an example illustrating two methods: import java.util.HashMap; import java.util.Map; public class Test { private static final Map<In...

Do STL maps initialize primitive types on insert?

I have a std::map like this: map<wstring,int> Scores; It stores names of players and scores. When someone gets a score I would simply do: Scores[wstrPlayerName]++; When there is no element in the map with the key wstrPlayerName it will create one, but does it initialize to zero or null before the increment or is it left undefined? ...

Having trouble initializing an SDL_Surface

I'm trying to set up something in SDL [in C++] where I can draw a one pixel big rectangle. I've got everything in my code working except my second SDL_Surface called rectangle. I'm having trouble initializing it. Here's the line where I try to initialize it: rectangle = SDL_Surface(SDL_DOUBLEBUF | SDL_HWACCEL | ...

How to quickly initialise an associative table in Lua ?

In Lua, you can create a table the following way : local t = { 1, 2, 3, 4, 5 } However, I want to create an associative table, I have to do it the following way : local t = {} t['foo'] = 1 t['bar'] = 2 The following gives an error : local t = { 'foo' = 1, 'bar' = 2 } Is there a way to do it similarly to my first code snippet ? ...