Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9181265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:14:49+00:00 2026-06-17T18:14:49+00:00

I’m being faced with an unexplicable change in a variable value. Now I’m not

  • 0

I’m being faced with an unexplicable change in a variable value. Now I’m not very experienced in C, most of the code I wrote with one hand on the keyboard and the other tracking a page in K&R, so please be gentle.

I have a C project, in Visual Studio 2010, a Lua binding for the pupnp library. Here’s some relevant code;

file: luaUPnPdefinitions.h (excerpt)

#ifndef LuaUPnPdefinitions_h
#define LuaUPnPdefinitions_h
...
// tracker for library being started or not
volatile static int UPnPStarted;
...
#endif  /* LuaUPnPdefinitions_h */

file: LuaUPnP.h (complete file)

#ifndef LuaUPnP_h
#define LuaUPnP_h

#include "upnp.h"
#include "upnptools.h"
#include "uuid.h"
#include <lua.h>
#include <lauxlib.h>
#include "luaIXML.h"
#include "darksidesync_aux.h"
#include "luaUPnPdefinitions.h"
#include "luaUPnPsupport.h"
#include "luaUPnPcallback.h"

#endif  /* LuaUPnP_h */

file: LuaUPnP.c (excerpt)

#include "luaUPnP.h"  // only include in this file
...
static int L_UpnpSendAdvertisement(lua_State *L)
{
    int result = UpnpSendAdvertisement(checkdevice(L, 1), luaL_checkint(L,2));
    if (result != UPNP_E_SUCCESS) return pushUPnPerror(L, result, NULL);
    lua_pushinteger(L, 1);
    return 1;
}
...

file: LuaUPnPsupport.h (excerpt)

#ifndef LuaUPnPsupport_h
#define LuaUPnPsupport_h

//#include <ixml.h>
#include <lua.h>
#include <lauxlib.h>
#include "luaIXML.h"
#include "upnptools.h"
#include "luaUPnPdefinitions.h"
...
UpnpDevice_Handle checkdevice(lua_State *L, int idx);
...
#endif  /* LuaUPnPsupport_h */

file: LuaUPnPsupport.c (excerpt)

#include "luaUPnPsupport.h"  // only include in this file
...
UpnpDevice_Handle checkdevice(lua_State *L, int idx)
{
    pLuaDevice dev;
    luaL_checkudata(L, idx, LPNP_DEVICE_MT);
    if (! UPnPStarted) luaL_error(L, UpnpGetErrorMessage(UPNP_E_FINISH));
    dev = (pLuaDevice)lua_touserdata(L, idx);
    return dev->device;
}

Now for the problem;
the UPnPstarted static variable basically tracks whether the pupnp library background processes have been started.
Now when debugging at the L_UpnpSendAdvertisement function at some point (reproducable), then UPnPstarted == 1, but when I hit this line;

int result = UpnpSendAdvertisement(checkdevice(L, 1), luaL_checkint(L,2));

and step into it, the debugger jumps to the checkdevice function (in file LuaUPnPsupport.c), and the value of UPnPstarted immediately changes to UPnPstarted == 0.

I’m lost. It’s a static variable, so should be shared, why does it change value, just by stepping into that other function?
In the debugger, the watch window, the values are lit up red, indicating that they just changed. Initially I thought that the files where included in the wrong order, and the UPnPstarted variable was duplicated (or had 2 instances), but when adding a watch to the watch window; &UPnPstarted to track the memory location of the variable, I don’t see a change when I step into the function, so it seems to me it refers to the same memory location.

I just don’t get it. Any ideas on what is happening?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:14:51+00:00Added an answer on June 17, 2026 at 6:14 pm

    Marking the variable as static does not mean there will be a single instance shared across translation units (essentially a .c source file plus all the headers it pulls in). Each translation until will have its own definition of UPnPStarted as each .c file includes the header file where UPnPStarted is defined.

    This means when the function UpnpSendAdvertisement() is invoked for the first time it is accessing its own version of UPnPStarted, which has not been modified and will have its initial, unchanged, value of 0.

    To share the same variable across translation units use extern at declaration and provide exactly one definition:

    /* In the header file. */
    extern volatile int UPnPStarted;
    

    and then in one, and only one, .c file:

    volatile int UPnPStarted;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need a function that will clean a strings' special characters. I do NOT
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I'm making a simple page using Google Maps API 3. My first. One marker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.