Init before anything else

P

Paul

Hello,

Consider this case:

Init(params) does some initialization and there is a function
Connect(params). The system starts and is idle until some one calls
Connect, as soon as first Connection is received and only on first
connection request Init() has to be called before serving Connection
request.
if(bInitialized) checks needs to be avoided as Connect() is invoked
several times. Init & Connect prototypes are different.

I have a solution, changing function pointers, any others??? Please
suggest...

thanks
-Paul.
 
J

Jack Klein

Hello,

Consider this case:

Init(params) does some initialization and there is a function
Connect(params). The system starts and is idle until some one calls
Connect, as soon as first Connection is received and only on first
connection request Init() has to be called before serving Connection
request.
if(bInitialized) checks needs to be avoided as Connect() is invoked
several times. Init & Connect prototypes are different.

Why does the fact that Connect() is invoked several times mean that
"if(bInitialized) checks needs(sic) to be avoided"? It's the best way
to handle the situation.

return_type Connect(arguments)
{
static int bInitialized;

if (!bInitialized)
{
Init();
}

/* the rest of Connect() */
}
I have a solution, changing function pointers, any others??? Please
suggest...

thanks

What makes you think that function pointers are a better solution than
that? What reason do you have for not using a solution like the
above?
 
M

Mac

Why does the fact that Connect() is invoked several times mean that
"if(bInitialized) checks needs(sic) to be avoided"? It's the best way
to handle the situation.

return_type Connect(arguments)
{
static int bInitialized;

if (!bInitialized)
{
Init(); bInitialized = 1;
}

/* the rest of Connect() */
}
[snip]

Or you could let Init set bInitilized to 1. Personally I would prefer to
see bInitialized altered immediately after it is tested, as I've
shown above.

--Mac
 
P

Paul

Mac said:
Why does the fact that Connect() is invoked several times mean that
"if(bInitialized) checks needs(sic) to be avoided"? It's the best way
to handle the situation.

return_type Connect(arguments)
{
static int bInitialized;

if (!bInitialized)
{
Init(); bInitialized = 1;
}

/* the rest of Connect() */
}
[snip]

Or you could let Init set bInitilized to 1. Personally I would prefer to
see bInitialized altered immediately after it is tested, as I've
shown above.

--Mac

I feel if(!bInitialized) check is redundant, as bInitialize = 1 for
ever after initialization. Function pointer solution is mentioned just
to avoid discussion on it, I don't say its a good solution considering
function call overhead.

I came across this code, which is in C++ (c communicty please excuse)
but its here to demonstrate the concept, I wonder if we can do
something like this in C.

return_type Init(args)
{
//some code
}
class A
{
A()//C'tor
{
Init(args);
}
};
return_type Connect(args)
{
static A a;
//some code
}

Hey C guys, please don't fire me for posting this C++ code here,
concept is great isn't it.

Cheers,
-Paul
 
C

Capstar

Paul said:
Mac said:
On 19 Aug 2004 19:50:52 -0700, (e-mail address removed) (Paul) wrote in
comp.lang.c:


Hello,

Consider this case:

Init(params) does some initialization and there is a function
Connect(params). The system starts and is idle until some one calls
Connect, as soon as first Connection is received and only on first
connection request Init() has to be called before serving Connection
request.
if(bInitialized) checks needs to be avoided as Connect() is invoked
several times. Init & Connect prototypes are different.

Why does the fact that Connect() is invoked several times mean that
"if(bInitialized) checks needs(sic) to be avoided"? It's the best way
to handle the situation.

return_type Connect(arguments)
{
static int bInitialized;

if (!bInitialized)
{
Init();

bInitialized = 1;
}

/* the rest of Connect() */
}

[snip]

Or you could let Init set bInitilized to 1. Personally I would prefer to
see bInitialized altered immediately after it is tested, as I've
shown above.

--Mac


I feel if(!bInitialized) check is redundant, as bInitialize = 1 for
ever after initialization. Function pointer solution is mentioned just
to avoid discussion on it, I don't say its a good solution considering
function call overhead.

I came across this code, which is in C++ (c communicty please excuse)
but its here to demonstrate the concept, I wonder if we can do
something like this in C.

return_type Init(args)
{
//some code
}
class A
{
A()//C'tor
{
Init(args);
}
};
return_type Connect(args)
{
static A a;
//some code
}

Hey C guys, please don't fire me for posting this C++ code here,
concept is great isn't it.

Then try something like this:

return_type Connect(arguments)
{
static int bInitialized = Init(); /*assuming Init() returns int*/

/* the rest of Connect() */
}

Mark
 
P

Paul

Capstar said:
Paul said:
Mac said:
On Thu, 19 Aug 2004 22:16:35 -0500, Jack Klein wrote:


On 19 Aug 2004 19:50:52 -0700, (e-mail address removed) (Paul) wrote in
comp.lang.c:


Hello,

Consider this case:

Init(params) does some initialization and there is a function
Connect(params). The system starts and is idle until some one calls
Connect, as soon as first Connection is received and only on first
connection request Init() has to be called before serving Connection
request.
if(bInitialized) checks needs to be avoided as Connect() is invoked
several times. Init & Connect prototypes are different.

Why does the fact that Connect() is invoked several times mean that
"if(bInitialized) checks needs(sic) to be avoided"? It's the best way
to handle the situation.

return_type Connect(arguments)
{
static int bInitialized;

if (!bInitialized)
{
Init();

bInitialized = 1;

}

/* the rest of Connect() */
}


[snip]

Or you could let Init set bInitilized to 1. Personally I would prefer to
see bInitialized altered immediately after it is tested, as I've
shown above.

--Mac


I feel if(!bInitialized) check is redundant, as bInitialize = 1 for
ever after initialization. Function pointer solution is mentioned just
to avoid discussion on it, I don't say its a good solution considering
function call overhead.

I came across this code, which is in C++ (c communicty please excuse)
but its here to demonstrate the concept, I wonder if we can do
something like this in C.

return_type Init(args)
{
//some code
}
class A
{
A()//C'tor
{
Init(args);
}
};
return_type Connect(args)
{
static A a;
//some code
}

Hey C guys, please don't fire me for posting this C++ code here,
concept is great isn't it.

Then try something like this:

return_type Connect(arguments)
{
static int bInitialized = Init(); /*assuming Init() returns int*/

/* the rest of Connect() */
}

Mark

this doesn't work, since initilizers needs to be constants in C.
Or do we accept C++ wins over C here, then why the hell we are still
programming in C?
 
C

Capstar

Paul said:
Capstar said:
Paul said:
On Thu, 19 Aug 2004 22:16:35 -0500, Jack Klein wrote:



On 19 Aug 2004 19:50:52 -0700, (e-mail address removed) (Paul) wrote in
comp.lang.c:



Hello,

Consider this case:

Init(params) does some initialization and there is a function
Connect(params). The system starts and is idle until some one calls
Connect, as soon as first Connection is received and only on first
connection request Init() has to be called before serving Connection
request.
if(bInitialized) checks needs to be avoided as Connect() is invoked
several times. Init & Connect prototypes are different.

Why does the fact that Connect() is invoked several times mean that
"if(bInitialized) checks needs(sic) to be avoided"? It's the best way
to handle the situation.

return_type Connect(arguments)
{
static int bInitialized;

if (!bInitialized)
{
Init();

bInitialized = 1;


}

/* the rest of Connect() */
}


[snip]

Or you could let Init set bInitilized to 1. Personally I would prefer to
see bInitialized altered immediately after it is tested, as I've
shown above.

--Mac


I feel if(!bInitialized) check is redundant, as bInitialize = 1 for
ever after initialization. Function pointer solution is mentioned just
to avoid discussion on it, I don't say its a good solution considering
function call overhead.

I came across this code, which is in C++ (c communicty please excuse)
but its here to demonstrate the concept, I wonder if we can do
something like this in C.

return_type Init(args)
{
//some code
}
class A
{
A()//C'tor
{
Init(args);
}
};
return_type Connect(args)
{
static A a;
//some code
}

Hey C guys, please don't fire me for posting this C++ code here,
concept is great isn't it.

Then try something like this:

return_type Connect(arguments)
{
static int bInitialized = Init(); /*assuming Init() returns int*/

/* the rest of Connect() */
}

Mark


this doesn't work, since initilizers needs to be constants in C.
Or do we accept C++ wins over C here, then why the hell we are still
programming in C?

Hmmm, good point. I know I should have tried it before posting. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top