# Dump program variables

**URL:** <https://community.unix.com/t/dump-program-variables/141793>\
**Category:** UNIX for Advanced & Expert Users\
**Created:** [March 18, 2002, 1:57am UTC](https://community.unix.com/t/dump-program-variables/141793 "2002-03-18T01:57:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rejise](https://community.unix.com/letter_avatar/rejise/32/5_5575768a8748004e209b776fc1b2916d.png) [@rejise](https://community.unix.com/u/rejise)\
**Post date:** [March 18, 2002, 1:57am UTC](https://community.unix.com/t/dump-program-variables/141793/1 "2002-03-18T01:57:28Z")

</div>

Hi,

Wish if could provide some clues.  
How do I dump all the C program variables(global) into say a file with their names and the values. So that when I restart the application again I could use this same file for reinitializing.Is this possible?

Thanks,  
Reji

---

<div class="post-metadata">

**Author:** ![AtleRamsli](https://community.unix.com/letter_avatar/atleramsli/32/5_5575768a8748004e209b776fc1b2916d.png) [@AtleRamsli](https://community.unix.com/u/AtleRamsli)\
**Post date:** [March 19, 2002, 12:37pm UTC](https://community.unix.com/t/dump-program-variables/141793/2 "2002-03-19T12:37:39Z")

</div>

To do this the way you state it is only possible if you use the symbol table, and you do not want to use a debugging tool as part of your program 🙂

You should put all your globals into a struct, and then read and write that struct.

lets say you have a name, and some int, and a float:

```nohighlight
int GL_i;
char GL_name[256];
float GL_f;

```

Change this to:

```nohighlight
struct {
	int i;
	char name[256];
	float f;

} GL;

```

As a rule, if you have lots of globals spread out like in the first example, your program is badly structured.  
A global is a potential bug.  
For every global, contemplate making it static.  
For the structure above, contemplate storing it in a dynamically allocated buffer - you will win big: You can have multiple instances, and your program (or library) will become safer, for a library this may provide reentrancy (each instance has its own data - or you will have to lock it!)

Atle
