pointer to function solves bug ?

T

Tomer Levinboim

Hi, got a Question regarding some bug I encountered.
the following is some abstractization of it :

I have the following function declared in say a.h and defined in a.c,

void setit(int x)
{
internal_index = x;
}

when I invoke it from b.c directly:

setit(7);

the assignment doesn't work (have no clue why, this is the bug)
But, when I invoke it from b.c as follows:

void (*direct_command_type_1)(int);
direct_command_type_1 = setit;
direct_command_type_1(5),

it does work.

the variable internal_index, is declared extern in c.h, and actually
defined in c.c which b.c and a.h include...

everything links and compiles alright (this is from a large project
I`m currently working on, so there might be some other things in
here...)

anyone got a clue or some helpful comments ?
Thanks
Tomer Levinboim
 
M

Mark A. Odell

(e-mail address removed) (Tomer Levinboim) wrote in

Hi, got a Question regarding some bug I encountered.
the following is some abstractization of it :

I have the following function declared in say a.h and defined in a.c,

void setit(int x)
{
internal_index = x;
}

when I invoke it from b.c directly:

Where the &^*& is internal_index defined and with what scope?
 
T

Thomas Stegen

Tomer said:
the variable internal_index, is declared extern in c.h, and actually
defined in c.c which b.c and a.h include...

If you are including the c file that is probably your problem.
You'll end up with multiple definitions of internal index...
 
S

Stephen L.

Tomer said:
Hi, got a Question regarding some bug I encountered.
the following is some abstractization of it :

I have the following function declared in say a.h and defined in a.c,

void setit(int x)
{
internal_index = x;
}

when I invoke it from b.c directly:

setit(7);

the assignment doesn't work (have no clue why, this is the bug)
But, when I invoke it from b.c as follows:

void (*direct_command_type_1)(int);
direct_command_type_1 = setit;
direct_command_type_1(5),

it does work.

the variable internal_index, is declared extern in c.h, and actually
defined in c.c which b.c and a.h include...

everything links and compiles alright (this is from a large project
I`m currently working on, so there might be some other things in
here...)

anyone got a clue or some helpful comments ?
Thanks
Tomer Levinboim

My _quess_ is that somehow your definition/build
process ends up with two distinct variables that
_look_ like the same variable in the source code.

If you could build your program with both the
"direct" and "thru-the-function-pointer" call
of your function, you should be able to see if
that's the case. Try adding -

void setit(int x)
{
internal_index = x;

fprintf(stderr, "x = %d, %p\n", x, &internal_index);
}

and see if both invocations produce the same
address on `stderr'. You might be able to see
this in a debugger as well...

HTH,


Stephen
 
M

Mark A. Odell

void setit(int x)
{
internal_index = x;

fprintf(stderr, "x = %d, %p\n", x, &internal_index);
fprintf(stderr, "x = %d, %p\n", x, internal_index,
&internal_index);
 
K

Keith Thompson

Mark A. Odell said:
fprintf(stderr, "x = %d, %p\n", x, internal_index,
&internal_index);

I think you mean:

fprintf(stderr, "x = %d, internal_index = %d, &internal_index = %p\n",
x, internal_index, (void*)&internal_index);
 
M

Mark A. Odell

I think you mean:

fprintf(stderr, "x = %d, internal_index = %d, &internal_index =
%p\n",
x, internal_index, (void*)&internal_index);

Indeed, sir, I did since %p expects a void *.
 
T

Tomer Levinboim

Keith Thompson said:
And fprintf with the format you specified expects 2 additional
arguments, not 3.

Thanks for the answers. I didn't mean to post it twice, and I`m not
sure why it happen, but I apologize for that.

in anycase, as I see it now, after few more tries, the problem is not
with my code since, it actually works on two other computers, on the
same IDE (VS.NET) with the same C compiler (Microsoft (R) 32-bit C/C++
Optimizing Compiler Version 13.10.3077 for 80x86).
it still does not work on my own computer under Debug, however, when
I change to Release, it does work.
so it seems there are some wierd issues with VS.NET. I did try to
uninstall (twice. first time using only the Control Panel\Add&Remove
programs, and second with some additional manual work).
and I do have the .Net 1.1 Framework...

any suggestions ? anyone saw something like this ?
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top