473,386 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

pointer to function solves bug ?

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
Nov 14 '05 #1
8 1224
tr********@hotmail.com (Tomer Levinboim) wrote in
news:4e**************************@posting.google.c om:
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?

--
- Mark ->
--
Nov 14 '05 #2
Tomer Levinboim wrote:
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...

--
Thomas.

Nov 14 '05 #3
Tomer Levinboim wrote:

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
Nov 14 '05 #4
"Stephen L." <sd*********@cast-com.net> wrote in
news:40***************@cast-com.net:
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); }


--
- Mark ->
--
Nov 14 '05 #5
"Mark A. Odell" <od*******@hotmail.com> writes:
"Stephen L." <sd*********@cast-com.net> wrote in
news:40***************@cast-com.net:
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);
}


I think you mean:

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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6
Keith Thompson <ks***@mib.org> wrote in
news:ln************@nuthaus.mib.org:
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);


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

--
- Mark ->
--
Nov 14 '05 #7
"Mark A. Odell" <od*******@hotmail.com> writes:
Keith Thompson <ks***@mib.org> wrote in
news:ln************@nuthaus.mib.org:
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);


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


And fprintf with the format you specified expects 2 additional
arguments, not 3.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8
Keith Thompson <ks***@mib.org> wrote in message news:<ln************@nuthaus.mib.org>...
"Mark A. Odell" <od*******@hotmail.com> writes:
Keith Thompson <ks***@mib.org> wrote in
news:ln************@nuthaus.mib.org:
> 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);


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


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 ?
Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: firegun9 | last post by:
Hello everyone, here is my program: /////////////////////////////// #include <iostream> using namespace std; void multi(double* arrayPtr, int len){ for(int i=0; i<len; i++)...
11
by: Enquiries, Hopkins Research | last post by:
Hi all I have a conundrum that is puzzling me. I have a large codebase in C that I am converting to C++ as fast as possible (i.e. slowly because I keep learning new idioms and stumbling with...
4
by: Saurabh Aggrawal | last post by:
Hi, I have made a dll and it is working fine in the debug build (as expected) but when I run it in the release build it is working strangely. For example, the dll saves the 10 properties on the...
4
by: ranjmis | last post by:
Hi all, I have come across a piece of code wherein a function returns a function pointer as it seems to me but not very clear from the prototype. As shown below - although return type is void...
8
by: Martin Jørgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
18
by: svata | last post by:
Hello to all, as a result from my previous post I'm busy with splitting code into functions. The one problem ( out of many ) I encounter is how to properly use/code a function which returns...
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
6
by: =?iso-8859-1?q?Erik_Wikstr=F6m?= | last post by:
Is there some way to get a function-pointer to the operators of the builtin types? In the following example I have a generic function which applies an operator on two values, however it does not...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.