Thread: oparator new

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    930

    oparator new

    Why do I get always 4 for strlen(buf) ?

    Code:
    int  main()
    {
       int i = 1;
       char * buf ;
    
       while(i<11)
       {
            buf = new char[i++];
            strcat(buf, "a");
            cout << strlen(buf) <<"\n";
       }
    
       delete [] buf;
    
       return  0;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Try strcpy instead of strcat.

    The memory you get is uninitialised, and the first thing strcat does is go looking for a \0.
    Your memory is filled with junk, so this is a crap-shoot as to what really happens.

    Also, you're leaking all the memory except for the last iteration.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Thanks, so I have to delete it every time before I want to reallocate it?


    Ps. I support UKIP also. ;-)
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You're not reallocating anything at the moment.

    You need to start with something like
    char *s = new char[1];
    *s = '\0';


    Then each loop iteration is
    char *t = new char[i+1];
    strcpy(t,s);
    strcat(t,"a");
    delete [ ] s;
    s = t;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Remember the rule: every new needs a corresponding delete. There is no many-to-one relationship here. Everytime you new, you must delete. If I see two new and one delete, then there's a bug. If there is one new and two delete, then there is a bug.
    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
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Thanks Elysia, that's what I wanted to know.
    So in Salem's example we should "delete [] t" also at the end of every loop?
    Using Windows 10 with Code Blocks and MingW.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, if you put that code in a loop.
    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 the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > So in Salem's example we should "delete [] t" also at the end of every loop?
    No you wouldn't.

    Code:
    new
    for ( .... ) {
        new
        ....
        delete
    }
    delete
    Follow the memory, not the variable names.
    If you delete t inside the loop, then you lose everything.

    s = t; is the stepping stone from one block of memory to the next. If you remove this one from under your feet, you're just going to get wet.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You should be aware that nobody does this sort of thing in the real world. To continually allocate something that is just one byte bigger and copy the existing contents across each time is madness. Have a read of this: Schlemiel the Painter's algorithm - Wikipedia, the free encyclopedia

    In the real world, you either use a nice container that grows geometrically, or you perform one pass up front to work out the final size required, and then only allocate once.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Ok, but that was just an example to understand what's going on.
    In my real program I have a constantly changing size that I need to allocate.
    In this case should I delete it in every loop or only when the loop has finished?

    Code:
    // global
    char * buf;
    int  main()
    {
       int newsize = 0;    
    
       while(i<11)
       {
            GetNewsize(newsize);
            buf = new char[newsize];
            function(buf);
            delete [] buf;
       }
     
    
       return  0;
    }
    Using Windows 10 with Code Blocks and MingW.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes, that should be fine - well, no memory leaks anyway.

    Though later on, you might want to introduce a 'maxsize' as well, so you can re-use the buffer if newsize <= maxsize
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why do you need to use new in the first place?
    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
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Well then what else should I use? I don't know what size to allocate in advance.
    Using Windows 10 with Code Blocks and MingW.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ducky
    Well then what else should I use? I don't know what size to allocate in advance.
    Use a std::string, std::vector<char>, etc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Ducky View Post
    Well then what else should I use? I don't know what size to allocate in advance.
    Depends on what you are trying to do.
    Are you trying to call C API functions? If so, don't. Use C++ API.
    Are you doing something else?
    Details!
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused by '&' oparator
    By Ducky in forum Windows Programming
    Replies: 3
    Last Post: 08-20-2008, 09:24 AM