473,386 Members | 1,867 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.

Re: memory trampling

joshuajnoble wrote:
I threw together a quick test here, I'm thinking my problem is in what
I'm doing with the static pointer (perhaps). Here's my Singleton:
I'm not sure, I can't see major problems in this piece of code.
>

#ifndef SINGLETON
#define SINGLETON

class SingletonImpl
{
public:
static SingletonImpl* getInst();
int val1;
char* val2[5];

protected:
static SingletonImpl* inst;
SingletonImpl();

};

#endif

SingletonImpl::SingletonImpl(){
val1 = 1;
val2 = "abcdefghijklmn";
this is maybe val2[0] = "..."; otherwise, it won't compile. However, it
will give you warning anyway regarding the conversion from const char*
(the string literal) to char*.
}

SingletonImpl* SingletonImpl::getInst(){
if(inst == 0) {
inst = new SingletonImpl();
}
return inst;
}
Surely inst is initialised somewhere to NULL, as in

SingletonImpl* SingletonImpl::inst = NULL;

or similar. Otherwise it won't link. If you don't initialise it to zero,
it will compile and crash.

TestObjs::TestObjs(char* ch){
c = ch;
SingletonImpl::getInst()->val2[0] = c;
}

class TestObjs {
public:
TestObjs(char* ch);
char* c;
};
And then my main:

int main (int argc, char * const argv[]) {
char* c = "ghu";
TestObjs t = TestObjs(c);
char* c3 = "3333333";
TestObjs t2 = TestObjs(c3);
return 0;
}
again, only the deprecated casts from string literal to char* seem "wrong".
And this blows up...I realize this is a pretty rudimentary question
and not really on par with the normal level of discussion here so
thanks in advance for any help and your patience :)
I don't think so, really. Araprt from the fact that the level here gets
very low at times, every question is worth discussing. Check the
SingletonImpl::inst initialisation, and if it is all right, in my
opinion it should not crash, so you may want to post the complete code
that in your pc generates the error.

Best wishes,

Zeppe
Oct 21 '08 #1
1 3431
On Oct 21, 5:31*pm, Zeppe
<ze...@remove.all.this.long.comment.yahoo.itwrot e:
joshuajnoble wrote:
I threw together a quick test here, I'm thinking my problem
is in what I'm doing with the static pointer (perhaps).
Here's my Singleton:
I'm not sure, I can't see major problems in this piece of code.
[...]
* *val2 = "abcdefghijklmn";
this is maybe val2[0] = "..."; otherwise, it won't compile.
I'd count that as a major problem, since it means that he's not
posting the actual code which causes the problem.
However, it will give you warning anyway regarding the
conversion from const char* (the string literal) to char*.
The standard doesn't require warnings:-). As a quality of
implementation issue, of course, a good compiler should give the
warning. Provided you've invoked it as a good C++
compiler---every compiler I know needs a half a dozen options to
be truly a C++ compiler.

[...]
Surely inst is initialised somewhere to NULL, as in
SingletonImpl* SingletonImpl::inst = NULL;
or similar. Otherwise it won't link.
You don't actually need the initialization, since objects with
static lifetime are zero initialized anyway. But without the
definition, it's undefined behavior---with the compilers I
usually use, it won't link either, but the standard doesn't
require an error of any sort, and it's quite possible that a
compiler could implicitly supply the definition, but skip the
zero initialization for it. That would still be conforming (but
not really what I would call a very good implementation).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 21 '08 #2

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

Similar topics

0
by: Andreas Suurkuusk | last post by:
Hi, I just noticed your post in the "C# memory problem: no end for our problem?" thread. In the post you implied that I do not how the garbage collector works and that I mislead people. Since...
4
by: Frank Esser | last post by:
I am using SQL 8 Personal edition with sp2 applied. I set the max server memory to 32MB and leave the min server memory at 0. When my application starts hitting the database hard the memory usage...
32
by: John | last post by:
Hi all: When I run my code, I find that the memory that the code uses keeps increasing. I have a PC with 2G RAM running Debian linux. The code consumes 1.5G memory by the time it finishes...
4
by: Franklin Lee | last post by:
Hi All, I use new to allocate some memory,even I doesn't use delete to release them. When my Application exit, OS will release them. Am I right? If I'm right, how about Thread especally on...
9
by: Mike P | last post by:
I know everything about reference counting and making sure you don't have large objects lying around. I have also profiled my app with multiple tools. I know about the fact GC collects memory but...
16
by: Rich S | last post by:
Or just loosing it? I have a small c++ app which generates millions combinations of bitset class objects and adds them to a STL map. (on a side thanks to all who responded to my earlier...
22
by: xixi | last post by:
hi, we are using db2 udb v8.1 for windows, i have changed the buffer pool size to accommadate better performance, say size 200000, if i have multiple connection to the same database from...
5
by: SStory | last post by:
Hi all, I really needed to get the icons associated with each file that I want to show in a listview. I used the follow modified code sniplets found on the internet. I have left in...
5
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS...
1
by: Jean-Paul Calderone | last post by:
On Tue, 22 Apr 2008 14:54:37 -0700 (PDT), yzghan@gmail.com wrote: The test doesn't demonstrate any leaks. It does demonstrate that memory usage can remain at or near peak memory usage even after...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.