Re: memory trampling

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Zeppe

    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::TestO bjs(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
  • James Kanze

    #2
    Re: memory trampling

    On Oct 21, 5:31 pm, Zeppe
    <ze...@remove.a ll.this.long.co mment.yahoo.itw rote:
    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:james.kan ze@gmail.com
    Conseils en informatique orientée objet/
    Beratung in objektorientier ter Datenverarbeitu ng
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

    Comment

    Working...