strange problem with optimisation

R

Rahul Iyer

G'day

I have encountered quite a strange problem with a new function i've added
to an existing software.

Pseudo code of actions.

-- begins --

func_foo (){

/* already in software */

while (packet) {
/* call to my function */
func_bar();
}
}

/* my function */
void func_bar () {
/*some static variables*/

/*and declaring an instance of a structure (defined previously containing
3 double) */
roundTripTime rtt;

/*rtt being initialised*/
init_rtt(&rtt);

/* function that does the processing, internally calls many other
functions */
func_baz(&rtt);

#ifdef DEBUG
printf_rtt(&rtt);
#endif

/*Not a part of the code.. .*/
/*sleep(1)i*/
fprintf (fp, "%f,%f,%f\n",rtt.t_half,rtt.t_otherhalf,rtt.t_total);
return;
}

-- ends --

Now ideally the last f-print should print to file the value stored in
rtt.elements. And each time new values should be evaluated and printed
to file.

-- the problem --
What actually happens is, each time only initialised values of rtt.elements
is printed to screen, not the computed or derived values. Strangely though,
if I introduce a 'sleep' or 'getchar' command before or just after the
'fprintf', appropriate values (computed values) are printed out.

I get the impression, the compiler (gcc-2.95) is playing tricks here in
trying to optimise code. I am not using any -O flags.
Has anyone ecountered a similar issue?
Any input greatly appreciated.

thanks
-r
PS: . no threads used in this software
. compiler gcc-2.95
. if the question isnt appropriate for this newsgroup, please let me
know.
 
M

Mac

G'day

I have encountered quite a strange problem with a new function i've
added to an existing software.

Pseudo code of actions.

-- begins --

func_foo (){

/* already in software */

while (packet) {
/* call to my function */
func_bar();
}
}

/* my function */
void func_bar () {
/*some static variables*/

/*and declaring an instance of a structure (defined previously
containing
3 double) */
roundTripTime rtt;

/*rtt being initialised*/
init_rtt(&rtt);

/* function that does the processing, internally calls many other
functions */
func_baz(&rtt);

#ifdef DEBUG
printf_rtt(&rtt);
#endif

/*Not a part of the code.. .*/
/*sleep(1)i*/
fprintf (fp, "%f,%f,%f\n",rtt.t_half,rtt.t_otherhalf,rtt.t_total);
return;
}

-- ends --

Now ideally the last f-print should print to file the value stored in
rtt.elements. And each time new values should be evaluated and printed
to file.

-- the problem --
What actually happens is, each time only initialised values of
rtt.elements is printed to screen, not the computed or derived values.
Strangely though, if I introduce a 'sleep' or 'getchar' command before
or just after the 'fprintf', appropriate values (computed values) are
printed out.

I get the impression, the compiler (gcc-2.95) is playing tricks here in
trying to optimise code. I am not using any -O flags. Has anyone
ecountered a similar issue? Any input greatly appreciated.

thanks
-r
PS: . no threads used in this software
. compiler gcc-2.95
. if the question isnt appropriate for this newsgroup, please let me
know.

Compiler specific questions are off-topic here.

here is a URL for the FAQ list:
http://docs.mandragor.org/files/Programming_languages/C/clc_faq_en/C-faq/top.html

Also see the "Welcome to comp.lang.c!" post which periodically appears
in this newsgroup.

If your standard library does have a bug, I imagine you are not the first
person to discover it. You may want to search on a platform specific
mailing list or something.

And you can try to find a platform specific group, too.

Or, if you think the bug just might be in your code (it probably is) then
you can try to whittle the code down to the smallest example which shows
the problem and post it here, in its entirety so that people can cut and
paste it onto their own machines and run it through their favorite
compilers or source checkers or whatever.

If the code is totally platform specific, you probably shouldn't post it
here no matter how small you make it.

--Mac
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top