www.digitalmars.com         C & C++   DMDScript  

c++ - Reseting the state of all global variables in a program

reply "jim p" <x y.com> writes:
I want to re-initialise all global variables defined in a program.
Is there any method for doing this ??

Reasons are, I have some third party routines which I have adapted to be
called from my program.
However, when I call these routines for a second time, the results are
incorrect because the global variables are not initialised.
I don't want to tamper with the routines too much.
There's lots of global variables...

Any ideas ???
Apr 16 2003
parent reply "Walter" <walter digitalmars.com> writes:
"jim p" <x y.com> wrote in message news:b7k7ts$1sua$1 digitaldaemon.com...
 I want to re-initialise all global variables defined in a program.
 Is there any method for doing this ??

 Reasons are, I have some third party routines which I have adapted to be
 called from my program.
 However, when I call these routines for a second time, the results are
 incorrect because the global variables are not initialised.
 I don't want to tamper with the routines too much.
 There's lots of global variables...

 Any ideas ???

Put a variable at the beginning and the end of the global data segment. That'll give you two pointers bracketing it, upon which you can save/restore the contents. Watch out for the BSS segment. You'll need to verify it's right by checking the .map file. Note that doing these kind of kludges could get you fired from most companies <g>.
Apr 16 2003
parent reply "jim p" <x y.com> writes:
Cheers Walter.

Excuse my ignorance, but how do I save/restore the contents and what is the
BSS segment ??


"Walter" <walter digitalmars.com> wrote in message
news:b7kah0$29b0$1 digitaldaemon.com...
 "jim p" <x y.com> wrote in message news:b7k7ts$1sua$1 digitaldaemon.com...
 I want to re-initialise all global variables defined in a program.
 Is there any method for doing this ??

 Reasons are, I have some third party routines which I have adapted to be
 called from my program.
 However, when I call these routines for a second time, the results are
 incorrect because the global variables are not initialised.
 I don't want to tamper with the routines too much.
 There's lots of global variables...

 Any ideas ???

Put a variable at the beginning and the end of the global data segment. That'll give you two pointers bracketing it, upon which you can

 the contents. Watch out for the BSS segment. You'll need to verify it's
 right by checking the .map file.

 Note that doing these kind of kludges could get you fired from most
 companies <g>.

Apr 16 2003
parent reply "Walter" <walter digitalmars.com> writes:
"jim p" <x y.com> wrote in message news:b7kb4j$2a77$1 digitaldaemon.com...
 Cheers Walter.

 Excuse my ignorance, but how do I save/restore the contents

char *pstart = &start; char *pend = &end; char *backup = (char*) malloc(pend - pstart); memcpy(backup, pstart, pend - pstart); to restore: memcpy(pstart, backup, pend - pstart);
 and what is the
 BSS segment ??

BSS is where all the 0 initialized data goes. Check out the .map file!
Apr 16 2003
parent reply "jim p" <x y.com> writes:
ok, its all in place.
I'm still not sure about the BSS segment.
I see it in the map file, but I'm not sure what to look for, with regards to
it being OK

James




"Walter" <walter digitalmars.com> wrote in message
news:b7kf9c$2df6$1 digitaldaemon.com...
 "jim p" <x y.com> wrote in message news:b7kb4j$2a77$1 digitaldaemon.com...
 Cheers Walter.

 Excuse my ignorance, but how do I save/restore the contents

char *pstart = &start; char *pend = &end; char *backup = (char*) malloc(pend - pstart); memcpy(backup, pstart, pend - pstart); to restore: memcpy(pstart, backup, pend - pstart);
 and what is the
 BSS segment ??

BSS is where all the 0 initialized data goes. Check out the .map file!

Apr 16 2003
parent reply "Walter" <walter digitalmars.com> writes:
Data in the BSS segment does not occupy space in the exe file, it's just
allocated by the loader and set to 0.

"jim p" <x y.com> wrote in message news:b7kos7$2khr$1 digitaldaemon.com...
 ok, its all in place.
 I'm still not sure about the BSS segment.
 I see it in the map file, but I'm not sure what to look for, with regards

 it being OK

 James




 "Walter" <walter digitalmars.com> wrote in message
 news:b7kf9c$2df6$1 digitaldaemon.com...
 "jim p" <x y.com> wrote in message


 Cheers Walter.

 Excuse my ignorance, but how do I save/restore the contents

char *pstart = &start; char *pend = &end; char *backup = (char*) malloc(pend - pstart); memcpy(backup, pstart, pend - pstart); to restore: memcpy(pstart, backup, pend - pstart);
 and what is the
 BSS segment ??

BSS is where all the 0 initialized data goes. Check out the .map file!


Apr 17 2003
parent reply "jim p" <x y.com> writes:
ok, but how do I check its 'right' ?


"Walter" <walter digitalmars.com> wrote in message
news:b7llhm$5v3$1 digitaldaemon.com...
 Data in the BSS segment does not occupy space in the exe file, it's just
 allocated by the loader and set to 0.

 "jim p" <x y.com> wrote in message news:b7kos7$2khr$1 digitaldaemon.com...
 ok, its all in place.
 I'm still not sure about the BSS segment.
 I see it in the map file, but I'm not sure what to look for, with


 to
 it being OK

 James




 "Walter" <walter digitalmars.com> wrote in message
 news:b7kf9c$2df6$1 digitaldaemon.com...
 "jim p" <x y.com> wrote in message


 Cheers Walter.

 Excuse my ignorance, but how do I save/restore the contents

char *pstart = &start; char *pend = &end; char *backup = (char*) malloc(pend - pstart); memcpy(backup, pstart, pend - pstart); to restore: memcpy(pstart, backup, pend - pstart);
 and what is the
 BSS segment ??

BSS is where all the 0 initialized data goes. Check out the .map file!



Apr 17 2003
parent "Walter" <walter digitalmars.com> writes:
Check that you are saving and restoring the data in it.

"jim p" <x y.com> wrote in message news:b7lret$9ts$1 digitaldaemon.com...
 ok, but how do I check its 'right' ?


 "Walter" <walter digitalmars.com> wrote in message
 news:b7llhm$5v3$1 digitaldaemon.com...
 Data in the BSS segment does not occupy space in the exe file, it's just
 allocated by the loader and set to 0.

 "jim p" <x y.com> wrote in message


 ok, its all in place.
 I'm still not sure about the BSS segment.
 I see it in the map file, but I'm not sure what to look for, with


 to
 it being OK

 James




 "Walter" <walter digitalmars.com> wrote in message
 news:b7kf9c$2df6$1 digitaldaemon.com...
 "jim p" <x y.com> wrote in message


 Cheers Walter.

 Excuse my ignorance, but how do I save/restore the contents

char *pstart = &start; char *pend = &end; char *backup = (char*) malloc(pend - pstart); memcpy(backup, pstart, pend - pstart); to restore: memcpy(pstart, backup, pend - pstart);
 and what is the
 BSS segment ??

BSS is where all the 0 initialized data goes. Check out the .map








Apr 17 2003