GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-May-2004, 03:24
7eleven 7eleven is offline
New Member
 
Join Date: May 2004
Location: Australia
Posts: 7
7eleven is on a distinguished road

Strange problem when I try to print a value?


Hello,

first method
---------------------------------------------------------------
CPP / C++ / C Code:
static int              sender_buffer[4];
static int 		frame_counter 		= 1;

static void transmit(MSG *msg, FRAMEKIND kind, int msglen, int seqno)
{
	
    	sender_buffer[frame_counter] = f;
	frame_counter++;
.........
..........
..........
.........
......
physical_method(blah, balh, blah);

}

/* second method */
static void physical_ready(CnetEvent ev, CnetTimer timer, CnetData data)
{
 printf("Frame Counter %d\n", frame_counter);
}
-----------------

The program will call transmit method first, then pyhsical_ready method. The problem is when I try to display the frame_counter, is always 1. I have increment the frame_counter on ther first method. instead frame_counter is global variable.

I do not know why it always print zero on pyhsical_ready method, but it's find when i try to print from other method?

Any Ideas Guys what wrong with that...

Thank's
Last edited by dsmith : 13-May-2004 at 07:05. Reason: Please enclose your c code in [c] & [/c]
  #2  
Old 13-May-2004, 07:28
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hello 7eleven.

I don't exactly follow your code, but I do have a question. Why have you defined a global as static? Have you tried not defining this as static. Global variables are implicitly static and adding the word static may be doing something else. This is from MSDN and I don't exactly understand what it means by internal linkage, but the rest is pretty straight forward

Quote:
Originally Posted by MSDN
Objects and variables defined outside all blocks have static lifetime and external linkage by default. A global object or variable that is explicitly declared as static has internal linkage.

I don't know if this helps, but if you aren't positive that the global variable should be a "static int", try it with just "int" and see if that helps.

Cheers,
d
  #3  
Old 15-May-2004, 10:45
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
When I see programs using global variables, I ask, "Why?" Are there lots of routines that access frame_counter, and you just didn't want to use it as an argument, or what? Are you sure that no other functions have modified frame_counter?

Here's an example that successfully does what I think you are attempting.
Note that, for debug purposes, I put printf() statements in certain places that allow me to see what's happening to the variable in question. You can do the same.

CPP / C++ / C Code:
#include <stdio.h>

static int  frame_counter = 1;

static void first_function(void);
static void second_function(void);

int main()
{ 


  printf("\n---Debug: going into first_function():     frame_counter = %d\n\n",
          frame_counter);

  first_function();

  printf("\n---Debug: returned from first_function():  frame_counter = %d\n\n",
          frame_counter);

  return 0;
}

static void first_function()
{
  frame_counter++;

  printf("\n --Debug: going into second_function():    frame_counter = %d\n\n",
          frame_counter);

  second_function();

  printf("\n --Debug: returned from second_function(): frame_counter = %d\n\n",
          frame_counter);
}

static void second_function()
{
 printf("Frame Counter = %d\n", frame_counter);
}


Regards,

Dave
 
 

Recent GIDBlogToyota - 2008 August Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you print a web page? rhino1616 Web Design Forum 10 30-Jul-2004 23:25
Another FX 5600 problem (but with details that might shed light on this) BobDaDuck Computer Hardware Forum 2 16-Apr-2004 07:53
unwanted scrollbar problem kelly001 Web Design Forum 3 24-Oct-2003 10:44
Guestbook error BobbyDouglas Web Design Forum 1 16-Oct-2003 22:17
select problem zuzupus MySQL / PHP Forum 0 15-Aug-2003 07:25

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:13.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.