Like Tree15Likes

oparator new

This is a discussion on oparator new within the C++ Programming forums, part of the General Programming Boards category; Why do I get always 4 for strlen(buf) ? Code: int main() { int i = 1; char * buf ...

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

    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;
    }
    Compiler MSVC++ 2010 with Code::Blocks.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    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.
    King Mir and Ducky like this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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


    Ps. I support UKIP also. ;-)
    Salem likes this.
    Compiler MSVC++ 2010 with Code::Blocks.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    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;
    Ducky likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    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
    764
    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?
    Compiler MSVC++ 2010 with Code::Blocks.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Yes, if you put that code in a loop.
    Ducky likes this.
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > 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.
    Ducky likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    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.
    Salem and Ducky like this.
    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
    764
    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;
    }
    Compiler MSVC++ 2010 with Code::Blocks.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    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
    Ducky likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Why do you need to use new in the first place?
    laserlight likes this.
    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
    764
    Well then what else should I use? I don't know what size to allocate in advance.
    Compiler MSVC++ 2010 with Code::Blocks.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,456
    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.
    Ducky likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    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!
    Ducky likes this.
    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.

Page 1 of 2 12 LastLast
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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21