473,666 Members | 2,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

construction of static foo objects .. more

Referencing source snippet below, the actual contruction of the foo
objects is done in a class. In that regard, I chose methods,
class1_construc t and class2_construc t for demonstration purposes. That
aside, I encountered source akin to what's shown below today and I was
almost convinced the source is wrought with trouble. On second thought
it appears legal. The static ptr_foo object and it's use in assignment
to other foo objects (ptr_1 etc ) at first troubled me but it's fine as
is. Correct? Of course the real issue surrounded the fact that the
ptr_1, ptr_2, ptr_3 and ptr_4 objects ar used in a thread. As a result
when the user exited the GUI application the vector of foo objects ( fv
vec ) was being called prior to destruction of the ptr_1, ptr_2 , ptr_3
and ptr_4 objects. That led to nasty error messages.

My initial recommendation (futher below )was to rid the 'statics', in
part because I thought there was a static initilization/destruction
fiasco going on. Come to think of it that's not the case. Correct?
Having said I think the ideal thing to do here involves the use of a
reference counted smart pointer. Correct?

# include <vector>
# include <sstream>
# include <string>

std::string
to_string(int i)
{
std::stringstre am s;
s << i;
return s.str() ;
}

struct foo {
std::string str ;
};

typedef std::vector < foo* foo_vec ;
foo_vec fv;
void class1_construc t()
{
for ( int idx ( 0 ); idx < 4; ++idx )
{
foo *ptr_foo = new foo () ;
ptr_foo->str = to_string ( idx ) ;
fv.push_back ( ptr_foo ) ;
}
}
void class1_delete()
{
for ( foo_vec::size_t ype idx ( 0 );
idx < fv.size();
++idx )
{
std::cout << "delete " << std::endl;
delete fv [ idx ] ;
}
}

static foo *ptr_foo;
static foo *ptr_1;
static foo *ptr_2;
static foo *ptr_3;
static foo *ptr_4;

int main()
{
class1_construc t() ;
for ( foo_vec::size_t ype idx ( 0 );
idx < fv.size();
++idx )
{
ptr_foo = fv [ idx ];
if ( ptr_foo->str == "1" )
{
ptr_1 = ptr_foo ;
}
if ( ptr_foo->str == "2" )
{
ptr_2 = ptr_foo ;
}
if ( ptr_foo->str == "3" )
{
ptr_3 = ptr_foo ;
}
if ( ptr_foo->str == "4" )
{
ptr_4 = ptr_foo ;
}
}
class1_delete() ;

}
Initial recommendation

foo *ptr_1;
foo *ptr_2;
foo *ptr_3;
foo *ptr_4;

int main()
{
class1_construc t() ;
for ( foo_vec::size_t ype idx ( 0 );
idx < fv.size();
++idx )
{
foo *ptr_foo = fv [ idx ];
if ( ptr_foo->str == "1" )
{
ptr_1 = ptr_foo ;
}
if ( ptr_foo->str == "2" )
{
ptr_2 = ptr_foo ;
}
if ( ptr_foo->str == "3" )
{
ptr_3 = ptr_foo ;
}
if ( ptr_foo->str == "4" )
{
ptr_4 = ptr_foo ;
}
}
class1_delete() ;

}

Sep 28 '06 #1
4 1795

ma740988 wrote:
Referencing source snippet below, the actual contruction of the foo
objects is done in a class. In that regard, I chose methods,
class1_construc t and class2_construc t for demonstration purposes. That
aside, I encountered source akin to what's shown below today and I was
almost convinced the source is wrought with trouble. On second thought
it appears legal. The static ptr_foo object and it's use in assignment
to other foo objects (ptr_1 etc ) at first troubled me but it's fine as
is. Correct? Of course the real issue surrounded the fact that the
ptr_1, ptr_2, ptr_3 and ptr_4 objects ar used in a thread. As a result
when the user exited the GUI application the vector of foo objects ( fv
vec ) was being called prior to destruction of the ptr_1, ptr_2 , ptr_3
and ptr_4 objects. That led to nasty error messages.

My initial recommendation (futher below )was to rid the 'statics', in
part because I thought there was a static initilization/destruction
fiasco going on. Come to think of it that's not the case. Correct?
Having said I think the ideal thing to do here involves the use of a
reference counted smart pointer. Correct?
At global scope, the "static" keyword affects linkage, not lifetime, so
a static initialization/destruction fiasco would not be affected. The
only thing that changes when you remove the "static" keyword from
ptr_1, ptr_2, ptr_3, and ptr_4 is that now they may be referenced by
another translation unit (one that declares them with something like
"extern foo *ptr_1;").

In a quick examination, I don't see anything wrong with the code you
posted, with or without the statics. Does it cause some error on your
system?

--
Alan Johnson

Sep 28 '06 #2

Alan Johnson wrote:
At global scope, the "static" keyword affects linkage, not lifetime, so
a static initialization/destruction fiasco would not be affected. The
only thing that changes when you remove the "static" keyword from
ptr_1, ptr_2, ptr_3, and ptr_4 is that now they may be referenced by
another translation unit (one that declares them with something like
"extern foo *ptr_1;").
Perhaps I'm mistaken (time to brush up again) but I thought the ones
with the static keyword could also be referenced in another translation
unit, except now I don't need extern?
In a quick examination, I don't see anything wrong with the code you
posted, with or without the statics. Does it cause some error on your
system?
Well there's a piece that I may or may not have mentioned, which
involved the desctruction of the vector on exit. ptr_1, ptr_2, ptr_3
and ptr_4 was used in a thread and from the looks of it the vector of
objects was destroyed prior to deleting the thread. As a result I was
seeing alot of 'undefined behavior ( crash) ' errors.

Sep 28 '06 #3
This is a bit of a tangent, but is it typical and/or correct
to refer to a function like class1_construc t() as a constructor?
To me it's not a constructor but a global function, not part of any
class, operating on a free-floating object.

Steve
Sep 28 '06 #4
ma740988 <ma******@gmail .comwrote:
>Alan Johnson wrote:
>At global scope, the "static" keyword affects linkage, not
lifetime, so a static initialization/destruction fiasco would not
be affected. The only thing that changes when you remove the
"static" keyword from ptr_1, ptr_2, ptr_3, and ptr_4 is that
now they may be referenced by another translation unit (one
that declares them with something like "extern foo *ptr_1;").
>Perhaps I'm mistaken (time to brush up again) but I thought the ones
with the static keyword could also be referenced in another translation
unit, except now I don't need extern?
What Alan said -- static at global scope is a holdover from C,
deprecated in C++, which makes the name invisible to other translation
units. If you have "static int foo" in one translation unit and
"extern int foo" in a second translation unit, they will never
reference the same variable. (And you will need a plain "int foo"
somewhere for the extern to resolve.)

Officially, you should now use a nameless namespace to make a name
visible in only one translation unit, instead of global "static".

Steve
Sep 28 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1672
by: Jerivix Entadi | last post by:
I'm attempting to create an application to work with a fluid database of people. I'm doing this in a command line, text-based manner. I need to know two things: 1) How do I save data, perhaps a group of objects and their memebers or a few lines of text? and 2) Is there any way to automate object construction? I'm thinking that I can create a vector or something similar to handle the number of people, is there a way for me to write a...
14
1980
by: trying_to_learn | last post by:
i am on the chapter on copy construction in C++ in the code (see below), the author says if u pass the object by value as in HowMany h2 = f(h); ....then a bitwise object is created w/o calling the constructor of the class. However he says when we leave scope of function HowMany f(HowMany x) then the destructor is called. why this inconsistency?. Accdng to me even the destructor should *not* be called. i can understand that bit wise copy...
7
1471
by: Dave | last post by:
Hello all, In the code below, I use a pointer to an object under construction. Is the usage below legal? I have come across similar code at work. It compiles, but I'm not sure it's really legal... Thanks, Dave struct D;
15
1836
by: Jakob Bieling | last post by:
Hi, I am aware of the fact, that the order of construction of global objects is unspecified. But I am in a situation, where I need to guarantee, that one object is created before others (not all others, just all objects of a specified type). I came up with the following solution, but I am not sure, if it does solve my problem: class first {
9
2653
by: Laban | last post by:
Hi, I find myself using static methods more than I probably should, so I am looking for some advice on a better approach. For example, I am writing an app that involves quite a bit of database operations on purchase orders and inventory. I have created a PurchaseOrder class and Inventory class to encapsulate operations like creating POs, finding items, etc. These two classes are used extensively from different parts of the app.
9
1758
by: Raphael Jolivet | last post by:
Hello there, I have a bunch of objects that I store in a text file (to store preferences), after having serialized them with JSON. Let's say I have my object : obj1 = { a : 1
7
2403
by: Raider | last post by:
Are global objects always constructed in order of they declaration? I use one global in the constructor of another one and I want to be sure it works the same with any compiler. The code looks like this: #include <iostream> class Class1 { public:
14
6008
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared static inside functions (i.e. local static objects) 5. objects declared at file scope.
4
3356
by: Steffen Bobek | last post by:
Extension methods are made for use with instances. I'd like to "misuse" them as static methods, too. Let me tell you my ambition: I use an extension method to serialize objects somehow like this: MyObject obj = new MyObject(); obj.ToXmlFile("some directory\\some file.xml"); The method "ToXmlFile" is defined elsewhere far apart from MyObject to work for all objects:
0
8869
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1775
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.