Thread: Quick question about memory allocation

  1. #46
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I disagree, but this isn't the place to discuss that.

  2. #47
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I agree this is not the right thread.

    I disagree that it's technically possible to add a large number of items to a linked list, no matter how well designed, compared to a vector that does block allocations. That's like saying "You can make a standard off-the-forecourt car go as fast as a dedicated race-car". Linked lists are good at certain things - like inserting or deleting single elements. But adding/deleting large numbers of elements is not one of the strengths of a linked list.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #48
    Registered User
    Join Date
    Nov 2007
    Posts
    14
    Quote Originally Posted by CornedBee View Post
    Code:
    for(int i = n-1; i >= 0; --i)
    This one puzzles me. Any particular reason you're using --i instead of i--? I can't make out any difference to a for-header.

    Some kind of convention for reverse iteration?

  4. #49
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Doesn't matter. It's a matter of style when using it there.

  5. #50
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Araanor View Post
    This one puzzles me. Any particular reason you're using --i instead of i--? I can't make out any difference to a for-header.

    Some kind of convention for reverse iteration?
    I always use prefix instead of postfix operators unless I actually need the old value first.
    Most compilers should optimize them both to the same thing, but I just think it's a good habit to get into.

  6. #51
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Postfix increment/decrement are slower for custom objects, because they create a copy of the current object and return it by value, whereas prefix simply returns itself by reference.

    For primitives, it makes no difference, but I like my programming style to not depend on the type of the variable I'm dealing with.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #52
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by cpjust View Post
    Most compilers should optimize them both to the same thing, but I just think it's a good habit to get into.
    The compiler can only optimize it away if the constructor for the type has no side effects. Yet another reason to strive for empty constructor bodies, IMHO.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. POSIX Threads and dynamic memory allocation
    By PING in forum Linux Programming
    Replies: 1
    Last Post: 04-02-2009, 10:28 AM
  3. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  4. Memory allocation at runtime?
    By electrolove in forum C Programming
    Replies: 6
    Last Post: 02-05-2003, 11:39 AM
  5. Yet another memory question
    By Dohojar in forum C Programming
    Replies: 5
    Last Post: 03-16-2002, 01:47 PM