variable array size

This is a discussion on variable array size within the C++ Programming forums, part of the General Programming Boards category; Hello How can I create an array with changing size? Shuo...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    65

    variable array size

    Hello

    How can I create an array with changing size?

    Shuo

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,991
    You can't. Use std::vector instead.
    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.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    20
    you can use
    Code:
    Buffer = new char[szBuffer];
    for dynamically creating a buffer of any size on the fly.

    but you can't change the size of an existing buffer, at least not that I am aware of...
    Last edited by klipseracer; 01-26-2008 at 08:56 PM. Reason: EDIT: additon

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,991
    True. There's no way to change the size of an existing array. The only way is to dynamically allocate, free, reallocate to change size, which is exactly what std::vector does for you.
    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.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Quote Originally Posted by klipseracer View Post
    but you can't change the size of an existing buffer, at least not that I am aware of...
    There is in C. realloc() is a function that will resize an array to a larger size. It will try to expand the memory without moving the array if it can. Pointers passed to realloc() must have been allocated with malloc or calloc, so this optimization can be intrusive in C++, unless it is wrapped in a container. std:vector can never do this, but even if it did, it wouldn't be much of an optimization.

    But most cases that you need the array size to change, you should use std::vector or std::deque. Use std::deque if the array can decrease significantly in size, as std::deque is able to decrease the size of it's buffer.
    Last edited by King Mir; 01-27-2008 at 06:24 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    std:vector can never do this, but even if it did, it wouldn't be much of an optimization.
    Do exactly what? Try to expand the array without moving the array? That's what the vector does all the time and it is an important optimization.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,258
    Try to expand the array without moving the array? That's what the vector does all the time and it is an important optimization.
    From what I see the C++ Standard makes no mention of this, so that is not what vectors do (though if it is technically feasible, a standard library implementor might implement the vector to do it).
    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

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Isn't this what the amortized constant time push_backs mean?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,258
    Isn't this what the amortized constant time push_backs mean?
    No, it does not. That means that in the long run, the complexity is constant time since the occasional linear time allocation of memory and copying over of elements becomes more and more rare.
    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

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I know vectors create a new block of memory and copy all their elements to the new one, then delete the old block; but wouldn't it be possible for a vector to use realloc() and in-place new to try to keep the same piece of memory?

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,991
    As you know, realloc isn't safe for C++ memory. Copy constructors for classes aren't called, for example.
    Plus from what I understand realloc just tries to allocate a new block of memory if it can't expand the current one and move the data over.
    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.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    std::vector must use the methods of the Allocator template parameter for its memory management. Since these don't include something realloc-like, it cannot use realloc.
    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

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    As you know, realloc isn't safe for C++ memory. Copy constructors for classes aren't called, for example.
    That's why I said realloc and in-place new.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,991
    How would you propose that? Once you use realloc, the damage is done.
    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.

  15. #15
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    How would you propose that? Once you use realloc, the damage is done.
    Yeah, I guess it couldn't use realloc() & placement new (I got the name wrong before) by default since some objects might rely on their own memory address internally... But maybe a custom allocator that uses realloc() & placement new would work if you know how the objects stored in it will handle a change of address behind the scenes.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 01:56 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

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