473,395 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

will two static libraries share the same heap or not?

21
Hi All,

I made a copy of a static library from an open-source code and changed its name to something different. In my driver I linked to both of these libraries. My question is will the global variables declared in these libraries step over each other or they will occupy two different heaps?

I have already test and they seems to be using the same heap since my global variables keeps getting the same data. To be honest with you, my understanding that since I am using two different libraries each library will occupy different heap or stack, is this true? I would appreciate if some one can explain this to me

Some people might ask why in the world I am doing this. It is because I am too lazy to dig into this code and try to change its structure to fit my need. Believe me I tried to do that and after working on it for two weeks I found out that I need to make too many changes. And there is always new version of it. So it would be nightmare to do so everytime there is new version. So I am trying to find an easy solution out of this and it seems there is none.

Please any kind of hints or help will be highly appreciated.
Jaz
Oct 5 '07 #1
8 2382
weaknessforcats
9,208 Expert Mod 8TB
Gobal variables share the same scope regardless of whether or not they are defined in a library. When they have the same name your build dies with duplicate definitions.

All of the variables reside in a portion of memory rerserved for global variables. That is, they are not on the stack nor are they on the heap.

To be on the stack, the variable must be defined inside a function. To be on the heap, the variable must be allocated from inside a function.

The global can be defined as static but this just restricts the problem to the one file that defines the global. A static global variable can use used only in the file that defines it.

One solution is to not use any global variables in your code that are not in a namespace. If you code this way:
Expand|Select|Wrap|Line Numbers
  1. namespace jazi
  2. {
  3.      int thedata;
  4. }
  5.  
this variable is identified as jazi::thedata whereas a variable outside the name space is just thedata. At least that would separate your stuff from the library.

I am assuming you are coding in C++ because if this is C, you are dead. With C, you have to a) make all of your global variables static and pack all your code into one source file, or b) rename all of your global variables so they are different from the library's and hope no one reuses one of your names.

Global variables are among the worst code practices known to man.
Oct 5 '07 #2
Jazi
21
Gobal variables share the same scope regardless of whether or not they are defined in a library. When they have the same name your build dies with duplicate definitions.

All of the variables reside in a portion of memory rerserved for global variables. That is, they are not on the stack nor are they on the heap.

To be on the stack, the variable must be defined inside a function. To be on the heap, the variable must be allocated from inside a function.

The global can be defined as static but this just restricts the problem to the one file that defines the global. A static global variable can use used only in the file that defines it.

One solution is to not use any global variables in your code that are not in a namespace. If you code this way:
Expand|Select|Wrap|Line Numbers
  1. namespace jazi
  2. {
  3.      int thedata;
  4. }
  5.  
this variable is identified as jazi::thedata whereas a variable outside the name space is just thedata. At least that would separate your stuff from the library.

I am assuming you are coding in C++ because if this is C, you are dead. With C, you have to a) make all of your global variables static and pack all your code into one source file, or b) rename all of your global variables so they are different from the library's and hope no one reuses one of your names.

Global variables are among the worst code practices known to man.

I really appreciate your feedback and it makes a lot of sense. However, here is my situation.

I am using an open source code. I really do not want to make changes to the code at all. If possible then I will rather do the minimum. So the namespace thing you mentioned above makes a lot of sense and I thought about it but it will require a lot of changes into the current version. Here is what I have and here where the issue reside.

There are two headers in this package; globs.h and exts.h. The globs.h contains the declaration and initialization of this variable as
//globs.h
int thedata=0;

This header is included by ONE source code that have a function which upload the data from the files into these variables.

The second header exts.h is a mirror of the globs.h except the word "extern" added to each global variables in globs.h

//exts.h
extern int thedata;

This file is included by all the other functions (source files).
Now, I thought if I get rid of the "extern" word from the exts.h then this will work "stupid assumption" it fails so I thought of getting rid of the whole exts.h and include the globs.h instead but I got the duplication definitions issue as you mentioned above.

Is there a quick fix to this problem besides the one you mentioned above.

Best regards,
Oct 5 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
I say again, bite the bullet and use a namespace to isolate you from that open source.

I had this problem once. I had a global variable named GROUP. In my case it was the group number of the employee in the empoyee database. I had a lot of code invested in this. At one point someone thought that using Microsoft sockets was a good idea. That's when everything stopped compiling. Turns out the nitwit at M$ was using GROUP to represent a group of sockets.

Three months later I had all my stuff in a namespace and big scar that reminds me to never, ever, use a global variable.

This is the reason that namespaces were put into C++.

Ideally, you would handle all this global stuff using a Singleton object but a namespace is enough of an improvement to warrant using one. The Singleton won't appeal until global variables are outlawed in your development area.
Oct 5 '07 #4
Jazi
21
I say again, bite the bullet and use a namespace to isolate you from that open source.

I had this problem once. I had a global variable named GROUP. In my case it was the group number of the employee in the empoyee database. I had a lot of code invested in this. At one point someone thought that using Microsoft sockets was a good idea. That's when everything stopped compiling. Turns out the nitwit at M$ was using GROUP to represent a group of sockets.

Three months later I had all my stuff in a namespace and big scar that reminds me to never, ever, use a global variable.

This is the reason that namespaces were put into C++.

Ideally, you would handle all this global stuff using a Singleton object but a namespace is enough of an improvement to warrant using one. The Singleton won't appeal until global variables are outlawed in your development area.
You mentioned before "All of the variables reside in a portion of memory rerserved for global variables. That is, they are not on the stack nor are they on the heap." Does this portion of memory have a specific name? Also, can you recommend me where to go and find more about this. My coworker does not seems to believe this is accurate.
Regards,
Jaz
Oct 8 '07 #5
Banfa
9,065 Expert Mod 8TB
You mentioned before "All of the variables reside in a portion of memory rerserved for global variables. That is, they are not on the stack nor are they on the heap." Does this portion of memory have a specific name? Also, can you recommend me where to go and find more about this. My coworker does not seems to believe this is accurate.
It would commonly be called the "data section" and contains initialised global data, uninitialised data resides in the BSS section and these 2 sections along with the heap make up the data segment of the program, as explained by wikipedia.

You should note that this is a typical memory configuration for a platform, not the configuration that absolutely every platform uses. You should check your platform/compiler documentation to see exactly what segments/sections exist for your platform.


The terms segement and section are used rather ambiguously and interchangable when refering to areas of memory used by a program which sometimes reside inside other areas.
Oct 8 '07 #6
Jazi
21
Thank you Sir for your clear and prompt responses.

Here is what I did and I am not sure this is exactly what you meant when you said "use a namespace to isolate you from the opne source".

As I mentioned before all the global variables used in this open source are found in two header files:
//globs.h
...
int thedata=0;
...

The second header exts.h is a mirror of the globs.h except the word "extern" added to each global variables in globs.h

//exts.h
extern int thedata;
...

What i did was to use namespace in the globs.h as follow
namespace myspace{
int thedata=0;
...
}
Then removed the exts.h and include the globs.h everywhere. I got the duplication definition errors.

Then I went back to your email and it looks like you meant I should build a wrapper around the open-source without making any changes to it. To tell you the truth I already did build a class wrapper around it but to explore certain functions from the package not the global variables. This is how I come to know about these global variables.

Frankly, I am not really clear on how to use the namespace to isolate me from the open-source in this case. Would you mind giving me a quick example no how to do that?

Thank you,
Jaz
Oct 8 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
Frankly, I am not really clear on how to use the namespace to isolate me from the open-source in this case. Would you mind giving me a quick example no how to do that?
That's a fair question.

I will make some assunptions:
1) there is open source code that I cannot control
2) this open source uses global variables
3) I need to use those global variables also
4) I do not want any globals I use confused with the open source globals.

Step 1:
Do not make any changes whatsoever to the open source code.

Step 2:
Create a namespace for my globals.

Let's call it the Jazi namespace. In one of your source files:

Expand|Select|Wrap|Line Numbers
  1. namespace Jazi
  2. {
  3.     int SameNameAsOpenSourceGlobal;
  4. }
  5.  
Step 3:
Create a header to access both yours and the open source global:
Expand|Select|Wrap|Line Numbers
  1. extern int ::SameNameAsOpenSourceGlobal;  //the open source global
  2. extern int Jazi::SameNameAsOpenSourceGlobal; //your variable of the same name
  3.  
This line:
Expand|Select|Wrap|Line Numbers
  1. extern int ::SameNameAsOpenSourceGlobal;  //the open source global
  2.  
couly be replaced by the #include of the open source header file.

Step 4:

In your code just use the correct name.

This also works for functions.

Remember: with a namespace when you define a variable it is created in that namespace. If you do this in a header file, you will have a variable created every time you inlcude the header and this will cause duplicate definitions.

Header files are not to contain actual variables but only declarations about those variables.
Oct 8 '07 #8
Jazi
21
That's a fair question.

I will make some assunptions:
1) there is open source code that I cannot control
2) this open source uses global variables
3) I need to use those global variables also
4) I do not want any globals I use confused with the open source globals.

Step 1:
Do not make any changes whatsoever to the open source code.

Step 2:
Create a namespace for my globals.

Let's call it the Jazi namespace. In one of your source files:

Expand|Select|Wrap|Line Numbers
  1. namespace Jazi
  2. {
  3.     int SameNameAsOpenSourceGlobal;
  4. }
  5.  
Step 3:
Create a header to access both yours and the open source global:
Expand|Select|Wrap|Line Numbers
  1. extern int ::SameNameAsOpenSourceGlobal;  //the open source global
  2. extern int Jazi::SameNameAsOpenSourceGlobal; //your variable of the same name
  3.  
This line:
Expand|Select|Wrap|Line Numbers
  1. extern int ::SameNameAsOpenSourceGlobal;  //the open source global
  2.  
couly be replaced by the #include of the open source header file.

Step 4:

In your code just use the correct name.

This also works for functions.

Remember: with a namespace when you define a variable it is created in that namespace. If you do this in a header file, you will have a variable created every time you inlcude the header and this will cause duplicate definitions.

Header files are not to contain actual variables but only declarations about those variables.
Assumptions 1 and 2 are correct, assumptions 3 and 4 are not. The problem is not I want to use the same global variables somewhere in my code. The issue is that I have two sets of data used by this package. Each data set gets uploaded from files using especial function and get stored at these global variables. The program runs fine serially:
Meaning:
Load data set1 into global variables
do whatever
Then, load data set2 "the global variables get the new values"
do whatever.
Then keep going if I need to use data set1 again ...

What I have been trying to do is to write a class wrapper that will have two instances one for dealing with data set1 and the other for data set2. The process should be alive until I kill without having to reloading from files. But this does not work because of the global variables .

I hope this clear now. I did not try to say this when I first posted this thread because it would have confused people.

It looks like having two libraries with two different global names is the solution for me at this point. What do you think?

I appreciate your help.
Oct 8 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: sks_cpp | last post by:
When do static members allocate memory? (at load time) How about static variables within a method? Where is the memory for static variables - is it allocated on the heap or does it have a...
7
by: John | last post by:
Hi: I want all objects of a class share the same set of data, which will be input from a file when the code start to run. In my design, I use static data member. The definition of the class is...
9
by: thomson | last post by:
Hi all, Would you please explain me where will be the heap stored if it is declared inside the Class, As class is a reference type, so it gets stored on the heap, but struct is a value...
12
by: Bonj | last post by:
can you embed a resource into a static libary? I'm using a custom binary resource specified as a .bin file in the .rc file of the static library project I've got. I use FindResource, LoadResource,...
15
by: dn | last post by:
I'm starting an n-tier application with an ASP.NET 2.0 presentation layer, a business layer, a data access layer, and a SQL Server 2005 database, and I have a question. In the business and data...
53
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global...
4
by: Dave | last post by:
I have a global.asax file with Application_Start defined and create some static data there and in another module used in the asp.net application and I realize that static data is shared amongst...
2
by: Nagrik | last post by:
Dear Group, The book of Bjarne Stroustrup in chapter 5.4.4 says the following "The word static is one of the most overused words in C and C++. For static data members it has both of the...
8
by: junky_fellow | last post by:
Guys, Consider the following snippet of code: int main(VOID) { static ushort fractionalValue={ 0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 250, 333, 666, 750, 0, 0 };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.