CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 30 of 30
  1. #16
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: Dynamic Memory Allocation Which is best?

    Quote Originally Posted by Dave1024
    Reason behind the search for various methods.

    char *kk;
    kk = new char[1000];
    //Array Use (Copy a .txt file content to a .doc file)
    delete []kk;
    kk = NULL;

    When i use this program runs success.Output is OK But compile in VS2005 with warning level 4 I got following message

    Leaking memory 'kk' due to an exception. Consider using a local catch block to clean up memory:

    Which method will avoid this problem.....
    Code:
    try
    {
       char* kk = 0;
       kk = new  char[1000];
    
       // do stuff with your buffer
       
       delete []kk;
       kk = NULL;
    }
    catch()
    {
       // prevents program from crashing if bad_alloc is thrown.
    }
    Yes, the catch is empty but it prevents the program from crashing due to unhandled bad_alloc exception. That compiler warning doesn't stop your program from executing the sunny day scenario. It's just telling you that if an exception were to occur, there is no way to catch it.

    Worry about writing a program correctly first. Worry about whether it is fast enough later. In this case, I really don't know why we are talking about speed. This is not a complex problem where speed should be an issue. I tend to prefer vectors over arrays because my code looks nicer and the vector provides a convenient object oriented interface which improves the overall quality of the software. I have never had a problem with a vector causing my software to be so slow that it wouldn't work. I'm not saying that it couldn't happen. If it did, you could analyze the program and refactor it. Worrying about which will be faster, before you even write the program and profile it is a complete waste of time in this case.
    Last edited by kempofighter; July 30th, 2008 at 05:09 PM.

  2. #17
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    Re: Dynamic Memory Allocation Which is best?

    Quote Originally Posted by Bornish
    Having to allocate large quantities of objects with very short lifetime. How to most effectively obtain that? Reuse allocations!!!
    Sure, but rarely does this entail writing a pool allocator. For example, if you need a dynamic array within a critical loop, you do not allocate and deallocate the array on each iteration... you simply declare a vector outside of the loop and resize it on each iteration, significantly reducing the number of allocations made. There is also a tendency among developers with a background in Java (et al) to unnecessarily allocate on the heap objects that could have been placed on the stack. Judging by the OP's previous statements about the stack, there's a good chance he's overusing the heap and underusing the stack.

    Anyway, to repeat what's already been said, there's no need to be concerned about the speed of a particular allocation if there's no noticeable impact on your program's overall performance. I advise strongly against making any premature optimizations, especially considering statements like the following:

    Quote Originally Posted by Dave1024
    char *kk;
    kk = new char[1000];
    //Array Use (Copy a .txt file content to a .doc file)
    delete []kk;
    kk = NULL;
    If this function were time-critical, you should be BY FAR more concerned about file operations than heap allocations.
    Last edited by Hermit; July 22nd, 2008 at 07:54 AM. Reason: redirected a misdirected statement
    - Alon

  3. #18
    Join Date
    Aug 2004
    Location
    Bucharest, Romania... sometimes
    Posts
    1,039

    Re: Dynamic Memory Allocation Which is best?

    Quote Originally Posted by Hermit
    Sure, but rarely does this entail writing a pool allocator. For example, if you need a dynamic array within a critical loop, you do not allocate and deallocate the array on each iteration... you simply declare a vector outside of the loop and resize it on each iteration, significantly reducing the number of allocations made. There is also a tendency among developers with a background in Java (et al) to unnecessarily allocate on the heap objects that could have been placed on the stack. Judging by your previous statements about the stack, there's a good chance you're overusing the heap and underusing the stack.

    Anyway, to repeat what's already been said, there's no need to be concerned about the speed of a particular allocation if there's no noticeable impact on your program's overall performance. I advise strongly against making any premature optimizations, especially considering statements like the following:



    If this function were time-critical, you should be BY FAR more concerned about file operations than heap allocations.
    @Hermit: please do not quote my post and then comment others' posts. The sample loop using placement new was meant to show what happens when a process is using a large number of objects of the same type, but with short lifetime. It usually happens in multi-threaded environments, so most of the application will be "the critical loop" examplified.
    @All: personally I hate to work with developers who are not concerned about speed and optimizations; there're no such things as premature optimizations, since real optimizations come from design, which should occur way before having a skeleton implementation; those who first write the code, test and then optimize... at the refactoring stage... well, may be blamed for the ongoing decrease in value of SW devs on the jobs market.
    Regards,
    Bogdan Apostol
    ESRI Developer Network

    Compilers demystified - Function pointers in Visual Basic 6.0
    Enables the use of function pointers in VB6 and shows how to embed native code in a VB application.

    Customize your R2H
    The unofficial board dedicated to ASUS R2H UMPC owners.

  4. #19
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Dynamic Memory Allocation Which is best?

    Using efficient algorithms is very different from squeezing an extra nano-second out of a for-loop.

    My only problem with the vector is that it initialises its POD to zero, which make loading an image file into memory unnecessarily inefficient. For such cases I have a very simple buffer class which uses new [] and delete [] internally.
    My hobby projects:
    www.rclsoftware.org.uk

  5. #20
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    Re: Dynamic Memory Allocation Which is best?

    Quote Originally Posted by Bornish
    @Hermit: please do not quote my post and then comment others' posts.
    Big apologies, didn't look carefully enough at the names on each post.
    - Alon

  6. #21
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Thumbs down Re: Dynamic Memory Allocation Which is best?

    Quote Originally Posted by Bornish
    @All: personally I hate to work with developers who are not concerned about speed and optimizations; there're no such things as premature optimizations, since real optimizations come from design, which should occur way before having a skeleton implementation; those who first write the code, test and then optimize... at the refactoring stage... well, may be blamed for the ongoing decrease in value of SW devs on the jobs market.
    Regards,
    Since you are complaining about other people's posts, I'd like to add my 2 cents. I think this is a fairly inflammatory statement, which I totally disagree with. The fastest possible program is not always the best possible program. There is no need to be purposefully wasteful when designing a program but it's difficult to guess what type of performance problems you'll have until you execute it and use a profiler to anaylze performance. You assume that because some are interested in developing interfaces and funciontal implementations first and worrying about speed/optimization after the initial program is running that they are going to produce a poor quality product. That is an inflammatory statement. Then you go on a tirade about a decrease in product and software developer value. That is simply irresponsible in my opinion. I can't believe that this thread would go in this direction over a very simple example (allocating memory for a character array). I can understand talking about this over something more important but in this case we don't even know what the OP's program is required to do. All we've seen is a few lines of code allocating memory for one single char buffer.

  7. #22
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Dynamic Memory Allocation Which is best?

    It's certainly true that, where possible, design choices affecting big-O runtime should be made early and/or encapsulated for easy replacement later. In that sense, optimization is a factor from the very start.

    But design choices which don't affect the big-O runtime at all, or affect a non-dominant term, should not be stressed about overmuch until they've been found to be a bottleneck. Little point in cutting the runtime of an O(n^2) loop in half if you've got an O(n^4) loop lurking elsewhere.....

  8. #23
    Join Date
    Jul 2008
    Posts
    11

    Re: Dynamic Memory Allocation Which is best?

    Quote Originally Posted by Dave1024
    I am using following ways to Allocate & Deallocate memory dynamically
    1. Using malloc() and free ()
    2. Using calloc() and free()
    3 using new and delete operator
    4 using stl <vector>

    Ok all are different styles. But i looking the better performence.During the use of stl allocation application is very slow. Multiple times calling of new and delete will reduce the application efficiency.

    On the shadow of these points how i choose a way which give optimum use of CPU. Which is the best on first 3 ways ?
    In terms of performance, calloc() will initialize allocated memory to 0's so it will be slower than malloc() which initializes nothing.

    new(), delete(), and stl<vector> are just adding layers of extra code calls for the C++/STL environment which will also call the same native commands as malloc()/calloc().

    But malloc()/free() is for C and new()/delete() is for C++. You should really avoid mix and matching allocation constructs in C++. Not following this principle can open you up for possible ugly security/memory corruption problems in your code. In my opinion, if you want performance, don't write it in C++. Write straight C code. There is a reason why OS's like Solaris and Linux are written primarily in C.

  9. #24
    Join Date
    Aug 2004
    Location
    Bucharest, Romania... sometimes
    Posts
    1,039

    Re: Dynamic Memory Allocation Which is best?

    Quote Originally Posted by kempofighter
    Since you are complaining about other people's posts, I'd like to add my 2 cents. I think this is a fairly inflammatory statement, which I totally disagree with. The fastest possible program is not always the best possible program. There is no need to be purposefully wasteful when designing a program but it's difficult to guess what type of performance problems you'll have until you execute it and use a profiler to anaylze performance. You assume that because some are interested in developing interfaces and funciontal implementations first and worrying about speed/optimization after the initial program is running that they are going to produce a poor quality product. That is an inflammatory statement. Then you go on a tirade about a decrease in product and software developer value. That is simply irresponsible in my opinion. I can't believe that this thread would go in this direction over a very simple example (allocating memory for a character array). I can understand talking about this over something more important but in this case we don't even know what the OP's program is required to do. All we've seen is a few lines of code allocating memory for one single char buffer.
    @kempofighter:Thanks for your reply; I like to see different opinions. I don't understand why did you feel offended by mine, though. I've started my statement with "personally..." so it is my write to have an opinion and to like or hate working with someone, especially when provided the reason for it. Inflammatory? Don't think was more then replying to a question about performance with a strong advise against making any premature optimizations! I can't agree with that since I've started programming on paper (>18 yrs ago), with no "performance tools" to help identify bottlenecks, and at the national olympics, the first eliminatory test was based on how performant (speed and memory used) was our implementation, since most participants were having perfect output results. You see, they needed only a handful of people for the final practical test, since were not enough computers (small / stupid country ).
    I'd like to clarify something: I wasn't really taking about fastest implementation... more of a more responsiveness one. Also, you may notice in this thread and others, that are quite many developers quite concerned about optimizations and the current trend in SW production. I'm not saying everyone should agree with these observations.
    To reverse the "highjacking" of this thread, I think I understood Dave's original post: better performance with dynamic allocated memory using malloc, calloc, new & delete, or vector class? I gave him my answer: placement new after malloc! Why? Because is more important how you use the heap manager, not how you call it!

    PS: we could continue this friendly discussion in pvt if you'd like, so we no longer highjack this thread; just send me a msg.

    Best regards,
    Bogdan Apostol
    ESRI Developer Network

    Compilers demystified - Function pointers in Visual Basic 6.0
    Enables the use of function pointers in VB6 and shows how to embed native code in a VB application.

    Customize your R2H
    The unofficial board dedicated to ASUS R2H UMPC owners.

  10. #25
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Dynamic Memory Allocation Which is best?

    Quote Originally Posted by why2jjj
    In my opinion, if you want performance, don't write it in C++. Write straight C code.
    This I don't understand whatsoever. Whatever you can do in 'C' can be done in C++. Nothing stops you from doing 'C' code in a C++ program.

    Regards,

    Paul McKenzie

  11. #26
    Join Date
    Aug 2004
    Location
    Bucharest, Romania... sometimes
    Posts
    1,039

    Re: Dynamic Memory Allocation Which is best?

    Quote Originally Posted by Paul McKenzie
    This I don't understand whatsoever. Whatever you can do in 'C' can be done in C++. Nothing stops you from doing 'C' code in a C++ program.
    I totally agree with you Paul!
    Bogdan Apostol
    ESRI Developer Network

    Compilers demystified - Function pointers in Visual Basic 6.0
    Enables the use of function pointers in VB6 and shows how to embed native code in a VB application.

    Customize your R2H
    The unofficial board dedicated to ASUS R2H UMPC owners.

  12. #27
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Dynamic Memory Allocation Which is best?

    Quote Originally Posted by Bornish
    I think I understood Dave's original post: better performance with dynamic allocated memory using malloc, calloc, new & delete, or vector class? I gave him my answer: placement new after malloc! Why? Because is more important how you use the heap manager, not how you call it!
    Did you consider the possibility of being able to use a custom allocator using std::vector since you are so straightforwardly ruling it out? When the design is good, you can fit in optimizations/changes pretty easily and hence the term premature optimization has been coined and is stressed upon. Such is the design of STL and STL-like containers that you can easily replace the default allocator if the allocation scheme is found out to be the bottleneck.

    If its found out that pool allocation strategy might help optimize the memory allocations, one could drop out the default std::allocator and use something like boost::pool_allocator<T> which can bind seamlessly to the container.

    I agree with you in that one should not code blindly or in complete ignorance but std::vector is such a solution that can adapt to any allocation scheme that you might find effective. Using raw memory allocation and using placement new is good if it adds to performance but the allocation scheme should be another encapsulated layer, out of the logic being applied to solve the business problem. It keeps the code more maintainable and clearer and flexible to changes that might come in. Of course, that is my opinion and you are free to disagree. :)
    Last edited by exterminator; July 23rd, 2008 at 09:07 AM.

  13. #28
    Join Date
    Jul 2008
    Posts
    11

    Re: Dynamic Memory Allocation Which is best?

    Quote Originally Posted by Paul McKenzie
    This I don't understand whatsoever. Whatever you can do in 'C' can be done in C++. Nothing stops you from doing 'C' code in a C++ program.

    Regards,

    Paul McKenzie
    What I meant by this is years ago when I was looking at code optimization in terms of the number of lines of assembly generated between a C++ program and a comparable C program, C was more optimized. Solaris and Linux OS coders don't necessarily write OS functionality in C++ because they are scared to death that the resulting # of lines of assembly from a C++ program will be more than a C program and kill performance because there is more lines of assembly to execute in a program.

    Now compilers can be way better now, but that is still a belief.

  14. #29
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Dynamic Memory Allocation Which is best?

    Quote Originally Posted by why2jjj
    What I meant by this is years ago when I was looking at code optimization in terms of the number of lines of assembly generated between a C++ program and a comparable C program,
    So you're saying that this program:
    Code:
    #include <stdio.h>
    int main()
    {
       printf("Hello World");
    }
    Compiled as "main.cpp" will be different than the same program compiled as "main.c"?

    Most C++ compilers are also 'C' compilers. When these compilers encounter a file extension of ".c", the 'C' language parser is used, and the rules of 'C' take over. Therefore I am highly skeptical that the very same compiler compiling the very same code will produce different object code, depending on whether it is compiled in C++ mode or 'C' mode.

    If you're refering to using C++ language features, that is not what I'm saying. I'm saying that line by line, the very same 'C' program can be compiled as C++ (making sure that the 'C' program prototyped functions, and do not use C++ keywords such as "class", "new", etc.), and there should be no difference.

    Regards,

    Paul McKenzie

  15. #30
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Wink Re: Dynamic Memory Allocation Which is best?

    Quote Originally Posted by Bornish
    PS: we could continue this friendly discussion in pvt if you'd like, so we no longer highjack this thread; just send me a msg.

    Best regards,
    I don't think that is necessary. I think it is an important discussion to have. One of the beautiful things about OO design and the new capabilities that C++ provides is the ability to refactor a good design. I've been involved with writing large scale, embedded applications as well as windows applications in C++. I can appreciate the concern people have about a vector zero initializing large chunks of data, for instance. So long as you understand the limitations of these tools, you should still be able to write a very high performance program using object oriented programs. The fact that malloc/free might be slightly faster than new/delete is not a good enough reason to choose those mechanisms in a C++ program. While performance is important, I also believe that it is possible to spend way too much time worrying about very trivial things.

    Quote Originally Posted by why2jjj
    In my opinion, if you want performance, don't write it in C++. Write straight C code.
    It's very possible to write a program in C++ that will meet the requirements of a system that requires high performance. It's also true that sometimes you sacrifice speed in order to gain other advantages. Sure, you could write program that is written in C that also meets the requirements (and gain some performance advantages) but you have to give up many of the advantages that the C++ language provides.

    Obviously, reasonable people can disagree over these things. That's my final 2 cents on the matter.
    Last edited by kempofighter; July 24th, 2008 at 02:34 PM. Reason: fixing quote

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured