473,386 Members | 1,699 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,386 software developers and data experts.

Using objects without ever creating an instance - recommendable?

Hi everyone!

I am debugging a big piece of code on the search for memory leaks,
using g++ under suse 9.3.
Since I'm trying to eliminate ALL memory leaks, I now stumbled upon a
class foo that is not ever instantiated (thus no constructor or
destructor ever called), but instead, all of its member variables are
defined as static in the according .cpp file, and it's methods are
invoked by calling foo::bar(...) to invoke method "bar(...)".

foo.h:
----------------
class foo {
public:
foo() {};
~foo() {};
static void bar (const char *);

private:
static char * myString;
static int myInt;
}

foo.cpp:
-----------------
/* definition of static members */
char *foo::myString;
int foo::myInt;

void foo::bar (const char *someString) {
myString = (char *) someString;
myInt = 1;
}
------------------

main.cpp:
------------
#include "foo.h"

int main() {
foo::bar ("Hello World!");
}
--------------
>From my understanding of C, the line
myString = (char *) someString;
stores a copy of the pointer to the parameter given to foo::bar in
myString, which can cause undefined behaviour, because the parameter
string is a (const char *) and loses validity after the function exits.

Question no. 1: Is that correct?
Question no. 2: Is the usage of classes without an instance excusable
under ANY circumstances? I totally disapprove of this style, but maybe
it's just my lack of understanding of how objects work?

What I see is that there is no defined point to free the memory pointed
to
by any member variables of class foo - the job normally done by a
destructor.

Question no. 3:
If - in order to solve this - I create an instance myFoo of class foo,
and remove the
declarations of static member variables in the foo.cpp, can I be sure
that
I will get an error at compile time, if I forget to change any access
to the
variables from foo::myString to myFoo->myString?
That last question seems obvious to answer, but I want a 2nd opinion :)

Regards,

Lars Uffmann

Aug 1 '06 #1
3 1822
la**********@rwth-aachen.de wrote:
I am debugging a big piece of code on the search for memory leaks,
using g++ under suse 9.3.
Since I'm trying to eliminate ALL memory leaks, I now stumbled upon a
class foo that is not ever instantiated (thus no constructor or
destructor ever called), but instead, all of its member variables are
defined as static in the according .cpp file, and it's methods are
invoked by calling foo::bar(...) to invoke method "bar(...)".

foo.h:
----------------
class foo {
public:
foo() {};
~foo() {};
static void bar (const char *);

private:
static char * myString;
Do the contents of this "string" ever change? No, I don't mean the
value of the pointer, I mean, do you ever change individual chars in
this "string"? If not, it should be declared a pointer to const char:

static char const* myString;
static int myInt;
}

foo.cpp:
-----------------
/* definition of static members */
char *foo::myString;
And here too

char const* foo::myString;
int foo::myInt;

void foo::bar (const char *someString) {
myString = (char *) someString;
If 'myString' is a pointer to const char, there will be no need to
cast anything.
myInt = 1;
}
------------------

main.cpp:
------------
#include "foo.h"

int main() {
foo::bar ("Hello World!");
}
--------------
>From my understanding of C, the line
myString = (char *) someString;
stores a copy of the pointer to the parameter
No. It "stores a copy of the pointer". Not "to the parameter".
given to foo::bar in
myString, which can cause undefined behaviour, because the parameter
string is a (const char *) and loses validity after the function
exits.
No, it does not. The value does not lose validity.
Question no. 1: Is that correct?
No.
Question no. 2: Is the usage of classes without an instance excusable
under ANY circumstances?
Sure.
I totally disapprove of this style, but maybe
it's just my lack of understanding of how objects work?
Static data members are singletons. If the program design calls for
a singleton, use it.
What I see is that there is no defined point to free the memory
pointed to
by any member variables of class foo - the job normally done by a
destructor.
There is no need - objects with static duration are created at the start
of the program and disposed of at the end of the program. If the type
of a static object has a constructor, it will be called, same for the
destructor. In your example, the static objects are of the built-in
types, no constructor or destructor exists.
Question no. 3:
If - in order to solve this - I create an instance myFoo of class foo,
and remove the
declarations of static member variables in the foo.cpp, can I be sure
that
I will get an error at compile time, if I forget to change any access
to the
variables from foo::myString to myFoo->myString?
WHY? What for? Does the program work? If it ain't broke, don't fix it.
That last question seems obvious to answer, but I want a 2nd opinion
:)
Happy to provide one.

BTW, what book on C++ are you currently reading?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 1 '06 #2
Victor Bazarov schrieb:
la**********@rwth-aachen.de wrote:
private:
static char * myString;
Do the contents of this "string" ever change? No, I don't mean the
value of the pointer, I mean, do you ever change individual chars in
this "string"? If not, it should be declared a pointer to const char:
Don't change, but it's pretty irrelevant because I was simplifying for
the example. The real code here uses lots of pointers to changing data
as member variables of class "foo".
From my understanding of C, the line
myString = (char *) someString;
stores a copy of the pointer to the parameter
No. It "stores a copy of the pointer". Not "to the parameter".
Still unclear. Given someString points to memory address X, then
after the assignment of myString, myString also points to memory
address X, right? That's what I meant.

myString, which can cause undefined behaviour, because the parameter
string is a (const char *) and loses validity after the function
exits.
No, it does not. The value does not lose validity.
Oh okay - I didn't know that. So if you store a pointer to a
const char passed to a function, the memory reserved for the
string during the function call stays untouched?
Question no. 2: Is the usage of classes without an instance excusable
under ANY circumstances?
Sure.
Mkay...
Static data members are singletons. If the program design calls for
a singleton, use it.
Thanks for the keyword...
http://en.wikipedia.org/wiki/Singleton_pattern
cleared up a few of my questions about how it is designed in this code
here. According to wiki, the constructor should be made either private
or protected, is NO constructor achieving the same effect? This class
has none.
For debugging purposes, I made it a normal class now and am using only
1 instance of it.
There is no need - objects with static duration are created at the start
of the program and disposed of at the end of the program. If the type
of a static object has a constructor, it will be called, same for the
destructor. In your example, the static objects are of the built-in
types, no constructor or destructor exists.
Hmm... so that means when the program exits - using exit (0); -
mtrace would NOT detect a memory leak unless there is some bug in the
code?

WHY? What for? Does the program work? If it ain't broke, don't fix it.
Nope - program has some huge memory leaks - over 200 megabytes of log
file from mtrace(). I am trying to fix them one by one, so I'm changing
everything that mtrace identifies as memory leak when I do a chain of
return; - at any point in the program to exit to the main routine and
then exit the
application.

The memory leaks make the code behave unpredictable and the behaviour
changes even on introduction of cerr-statements.
BTW, what book on C++ are you currently reading?
None, I just used the "foo" term because I saw it in lots of examples
and one name is as good as any *g*

Am working on my diploma thesis and in order to implement my code into
this big project, it seems I need to debug the whole project :(

Regards,

Lars

Aug 1 '06 #3
la**********@rwth-aachen.de wrote:
Victor Bazarov schrieb:
>la**********@rwth-aachen.de wrote:
>>private:
static char * myString;
Do the contents of this "string" ever change? No, I don't mean the
value of the pointer, I mean, do you ever change individual chars in
this "string"? If not, it should be declared a pointer to const
char:

Don't change, but it's pretty irrelevant because I was simplifying for
the example. The real code here uses lots of pointers to changing data
as member variables of class "foo".
>>>From my understanding of C, the line
myString = (char *) someString;
stores a copy of the pointer to the parameter
No. It "stores a copy of the pointer". Not "to the parameter".

Still unclear. Given someString points to memory address X, then
after the assignment of myString, myString also points to memory
address X, right? That's what I meant.
Then you're correct. The copy of the pointer value is created.
>>myString, which can cause undefined behaviour, because the parameter
string is a (const char *) and loses validity after the function
exits.
No, it does not. The value does not lose validity.

Oh okay - I didn't know that. So if you store a pointer to a
const char passed to a function, the memory reserved for the
string during the function call stays untouched?
Well, it's so with a literal. Generally speaking (to complicate things)
it is possible that the memory will go away. Inside the function we
don't know how the address passed to the function was obtained. But
it definitely does not lose validity (in your case) until the end of
the function, and if it's the address of the literal, it stays OK.
>>Question no. 2: Is the usage of classes without an instance
excusable under ANY circumstances?
Sure.
Mkay...
>Static data members are singletons. If the program design calls for
a singleton, use it.

Thanks for the keyword...
http://en.wikipedia.org/wiki/Singleton_pattern
cleared up a few of my questions about how it is designed in this code
here. According to wiki, the constructor should be made either private
or protected, is NO constructor achieving the same effect? This class
has none.
But this class is not a singleton. Each of the static data members is.
And then, again, don't look at the pattern (yet). If you need some kind
of a C string and it has its designated purpose, and there is no need to
have many of them, then a static data member of some class will do.
For debugging purposes, I made it a normal class now and am using only
1 instance of it.
That's up to you, of course. I was using the word 'singleton' to mean
that there is only one of something, not to refer to the pattern.
>There is no need - objects with static duration are created at the
start of the program and disposed of at the end of the program. If
the type of a static object has a constructor, it will be called,
same for the destructor. In your example, the static objects are of
the built-in types, no constructor or destructor exists.

Hmm... so that means when the program exits - using exit (0); -
mtrace would NOT detect a memory leak unless there is some bug in the
code?
I don't know what you're talking about.
>WHY? What for? Does the program work? If it ain't broke, don't
fix it.
Nope - program has some huge memory leaks - over 200 megabytes of log
file from mtrace(). I am trying to fix them one by one, so I'm
changing everything that mtrace identifies as memory leak when I do a
chain of return; - at any point in the program to exit to the main
routine and then exit the
application.
That's a good approach. I didn't see any memory leaks in the code you
posted, though.
The memory leaks make the code behave unpredictable and the behaviour
changes even on introduction of cerr-statements.
That can be also due to malformed code produced by the optimizing
compiler.
>BTW, what book on C++ are you currently reading?
None, I just used the "foo" term because I saw it in lots of examples
and one name is as good as any *g*

Am working on my diploma thesis and in order to implement my code into
this big project, it seems I need to debug the whole project :(
Well, good luck with it! Ask more questions as you need. We're here to
help.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 2 '06 #4

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

Similar topics

28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
3
by: simon lee | last post by:
Dear all, I have a html code like <a href="product.html" target=new>product</a> Although the file "product.html" can be opened at the new page, but with scrollbar, menu, location, and so...
26
by: codymanix | last post by:
Last night I had several thought about RAII and want to discuss a bit. Why doesn't CSharp support destructors in structs? Wouldn't that make RAII possible like in C++? When the struct goes out of...
6
by: marktm | last post by:
Hi- I am trying to use setInterval to call a method within an object and have not had any luck. I get "Object doesn't support this property or method" when I execute the following code. What I...
14
by: D. Alvarado | last post by:
Hello, I am trying to open a window containing an image and I would like the image to be flush against the window -- i.e. have no padding or border. Can I make this happen with a single call to a...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
19
by: Kamilche | last post by:
I have looked at many object-oriented programming frameworks out there for C. Though the ideas presented are intriguing, and I've used some of them in my own work, they all suffered some drawback...
1
by: Screenbert | last post by:
After finding nothing anywhere in google I am posting this so everyone can benefit by it. The formating is not pretty since I copied it from my word document, but you should benefit by it. ...
0
by: screenbert | last post by:
Managing DHCP Servers using C# They said it was impossible. It couldn't be done. But you can in fact manage DHCP servers using C#. This includes creating and deleting Scopes, SuperScopes,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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
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...

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.