Thread: Constructor wrong??

  1. #1
    Registered User
    Join Date
    May 2010
    Location
    Italy
    Posts
    5

    Constructor wrong??

    Hello everybody from Italy!!! Initially in my 'Union - Find' that I have implemented I've defined these 4 structures, respectively: 'A - a', 'B - b', 'C - c', 'D - d', but I want to do it directly with the Constructor, but my program, when I debug it, visualize the 'iostream' header file library and then ends with the usual warning : "segmentation fault!".

    Here is my Constructor :

    Code:
    public :  
      Lista* nodoA, *nodoB, *nodoC, *nodoD;
      Union() { nodoA->name = 'A'; nodoA->nxt = NULL; nodoB->name = 'B'; nodoB->nxt = NULL; nodoC->name = 'C'; nodoC->nxt = NULL; nodoD->name = 'D'; nodoD->nxt = NULL; }  /* Constructor. */
      ~Union() { delete nodoA; delete nodoB; delete nodoC; delete nodoD; }  /* Destructor. */
    Where do I wrong? :'( Is it legal?? I hope in your help!

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    You declare nodoA, nodoB, nodoC, and nodoD but never point them to valid memory. You then proceed to deference all of them when you do:
    Code:
    nodoA->name = 'A'; ...

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Example:

    Code:
    struct Nodo
    {
        char name;
        Lista* nxt;
    
        Nodo(char name, Lista* nxt = 0) : name(name), nxt(nxt) {}
    };
    
    
    Lista *nodoA;
    nodoA = new Nodo('A');
    goto( comeFrom() );

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Except in this case, I think dynamic memory is completely unnecessary. Plus you don't include delete in your code. Plus you really should be using smart pointers in your code, which you aren't. Plus you should be conveying C++0x, and not the old C and its 0s. It's nullptr, and nothing else. Worst-case scenario NULL. But then you really should upgrade your compiler.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by Elysia View Post
    Except in this case, I think dynamic memory is completely unnecessary. Plus you don't include delete in your code. Plus you really should be using smart pointers in your code, which you aren't. Plus you should be conveying C++0x, and not the old C and its 0s. It's nullptr, and nothing else. Worst-case scenario NULL. But then you really should upgrade your compiler.

    Ok, i'll give you the C++0x concepts, but my intention was to illustrate that you can't access members through -> without pointing to *something* first.
    goto( comeFrom() );

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh boy. Did we read the link I posted earlier? Or maybe you just felt like adding something to it? Nothing wrong with that, but you should perhaps make a hint that you did read it since you are echoing exactly what has been written before...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by Elysia View Post
    Oh boy. Did we read the link I posted earlier? Or maybe you just felt like adding something to it? Nothing wrong with that, but you should perhaps make a hint that you did read it since you are echoing exactly what has been written before...
    Lol! I sure didn't read it -it's not a concept I have difficulty with.

    I think it's more my understanding that people posting to these forums have difficulty with the *internet* in general, especially utilizing search engines.

    Also, because the OP used pointer syntax without pointing to anything, its doubtful he/she will understand that the first section entitled "Using pointers without allocating them" even applies to what they're doing.
    goto( comeFrom() );

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by StainedBlue View Post
    I think it's more my understanding that people posting to these forums have difficulty with the *internet* in general, especially utilizing search engines.
    A lot of times, yes. Then you simply bang their heads until they get it.

    Quote Originally Posted by StainedBlue View Post
    Also, because the OP used pointer syntax without pointing to anything, its doubtful he/she will understand that the first section entitled "Using pointers without allocating them" even applies to what they're doing.
    Then they have to ask about it. "What am I supposed to read and how does it apply to my situation"? If they don't, then we'll have to teach them.
    This is something I take for granted from everyone. If they ignore it, I will just keep repeating that they should read it or refuse to help.
    Everyone has to learn to fend a little for themselves, you know?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Oh, BTW, I was totally unaware of 'nullptr'. Thank you for the update. See my new thread.
    goto( comeFrom() );

  11. #11
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168

    Where do YOU get your C++0x feature listing?

    Where do YOU get your C++0x feature listing?
    goto( comeFrom() );

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Wikipedia, for example.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    @Elysia

    Sorry, I guess you didn't get my joke. Oh well, I'm used to it...
    goto( comeFrom() );

Popular pages Recent additions subscribe to a feed