char table / pointer memory allocation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mehafi@gmail.com

    char table / pointer memory allocation

    Hi,

    Why this program work?

    #include<iostre am.h>
    class test
    {
    public:
    char *ptr;
    void setPtr(char* p){
    ptr = p;
    }
    void print(){
    cout << ptr << endl;
    }
    };

    main()
    {
    test t;
    t.setPtr("abc") ;
    t.print();
    system("pause") ;
    }

    Is argument of function setPtr - "abc" - a temporary char table, made
    on stack, which is deleted when setPtr function ends? If it is, the
    pointer - ptr - point then to the memory addres which was deleted, so
    print method should print random characters or may crash the program.
    When I made char table using new:

    main()
    {
    char* tab = new char[4];
    strcpy(tab, "abc");
    test t;
    t.setPtr(tab);
    delete[] tab;
    t.print();
    system("pause") ;
    }

    print() method prints random characters.
    But why the first program doesn't crash / prints random chars ?

    thanks in advance
    best regards
    mehafi
  • sk_usenet

    #2
    Re: char table / pointer memory allocation


    <mehafi@gmail.c omwrote in message Hi,
    >
    Why this program work?
    >
    #include<iostre am.h>
    Non standard header. Get any recent book on C++ that would explain why this
    is not portable.
    class test
    {
    public:
    char *ptr;
    void setPtr(char* p){
    ptr = p;
    }
    void print(){
    cout << ptr << endl;
    }
    };
    >
    main()
    You need an explicit "int main()".
    {
    test t;
    t.setPtr("abc") ;
    t.print();
    system("pause") ;
    }
    >
    Is argument of function setPtr - "abc" - a temporary char table, made
    on stack, which is deleted when setPtr function ends? If it is, the
    "abc" is not on stack, it's a string literal and has static storage
    duration.
    The storage for these objects shall last for the duration of the program
    pointer - ptr - point then to the memory addres which was deleted, so
    print method should print random characters or may crash the program.
    When I made char table using new:
    What's a char table?
    main()
    {
    char* tab = new char[4];
    strcpy(tab, "abc");
    test t;
    t.setPtr(tab);
    delete[] tab;
    t.print();
    system("pause") ;
    }
    >
    print() method prints random characters.
    Because of undefined behavior.
    But why the first program doesn't crash / prints random chars ?
    Read above.
    --



    Comment

    • mehafi@gmail.com

      #3
      Re: char table / pointer memory allocation

      thx.

      Comment

      • Paavo Helde

        #4
        Re: char table / pointer memory allocation

        mehafi@gmail.co m wrote in news:b1ee3325-348a-41ec-88bd-d3aea0a2c784
        @f36g2000hsa.go oglegroups.com:
        Hi,
        >
        Why this program work?
        >
        #include<iostre am.h>
        class test
        {
        public:
        char *ptr;
        void setPtr(char* p){
        ptr = p;
        }
        void print(){
        cout << ptr << endl;
        }
        };
        >
        main()
        {
        test t;
        t.setPtr("abc") ;
        t.print();
        system("pause") ;
        }
        >
        Is argument of function setPtr - "abc" - a temporary char table, made
        on stack, which is deleted when setPtr function ends? If it is, the
        "abc" is a string literal which has static duration (compiled into the
        executable code and not going anywhere from there). Thus it will be alive
        during the whole program.

        Besides, I recommend to get familiar with std::string and forget the
        string lifetime issues forever.

        Regards
        Paavo




        Comment

        • James Kanze

          #5
          Re: char table / pointer memory allocation

          On May 7, 9:47 pm, Paavo Helde <nob...@ebi.eew rote:
          meh...@gmail.co m wrote in news:b1ee3325-348a-41ec-88bd-d3aea0a2c784
          @f36g2000hsa.go oglegroups.com:
          Besides, I recommend to get familiar with std::string and
          forget the string lifetime issues forever.
          I can't let that pass. The only times I use C style strings is
          when lifetime is an issue; a static char[] with a constant
          initializer is static initialized, and effectively has an
          infinite lifetime. A static std::string can easily be accessed
          before it is constructed, or after it is destructed.

          --
          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

          • Paavo Helde

            #6
            Re: char table / pointer memory allocation

            James Kanze <james.kanze@gm ail.comwrote in news:65660d3f-3e1f-4924-93e7-
            8e2fe084edc0@j2 2g2000hsf.googl egroups.com:

            [...]
            >
            Nobody suggested using char[] for mutable objects.
            >
            And nobody has objected using string literals in the code.

            Paavo

            Comment

            Working...