C equivalent of vector

This is a discussion on C equivalent of vector within the C Programming forums, part of the General Programming Boards category; Hey guys, I've recently started developing PPC software, and found myself quite hampered by the complete lack of the C++ ...

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    C equivalent of vector

    Hey guys,
    I've recently started developing PPC software, and found myself quite hampered by the complete lack of the C++ STL (although most other C++ language features are available). As such, I was just wondering what C programmers use instead of std::vector - I do know that linked lists are one alternative, and static arrays can be used to some limited extent, but otherwise does one have to resort to plain 'ol dynamic arrays?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    otherwise does one have to resort to plain 'ol dynamic arrays?
    I always have in the past. I wouldn't be surprized if there was some library out there with structs and functions to work with them - but it's probably best if you just go ahead and start getting used to the C style.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    use c++ MFC instead of C and you can use many MFC container classes.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    I'm fairly certain that the std::vector is implemented using a dynamic array (can someone confirm/deny this?). A chunk of memory is allocated, and it gets used up as more elements are added to the vector. Once you run out of memory, a call to realloc() is made giving you room to add more elements. A linked list is better than a dynamic array if your vector has massive memory requirements. The reason is that a dynamic array requires contiguous memory whereas the linked list nodes can be allocated from random areas off the heap. Of course with a linked list implementation, you can never do random access in the form of V[x].

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>I wouldn't be surprized if there was some library out there with structs and functions to work with them
    Right, but then I suppose it would pretty much be just a 3rd-party, less-friendly std::vector.

    I'm asking this simply because I've heard that C and C++ have different ways of doing things, and I figured there might be a better alternative for scalable data structures supporting random access than dynamic arrays (from what I've heard, they're to be avoided wherever possible in C++).
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    I suppose it would pretty much be just a 3rd-party, less-friendly std::vector.
    Yup. Nothing in the standard library.

    http://www.infosys.utas.edu.au/info/...C/CStdLib.html

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Hunter2
    from what I've heard, they're to be avoided wherever possible in C++
    Hey -- I got yelled at yesterday for mentioning c++ on this C board -- and you made the same misstake?

    Its not that c arrays don't work well in c++ -- they work just as well in c++ as they do in C. But its because std::vector relieves you of all the work of maintaining the allocation and reallocation of the arrays. std::vector is really not much more than a c++ wrapper for the C arrays.

    Its not really all that hard to role your own version of std::vector if you just want the basics -- you could probably write one in just a few hours. WinCE doesn't support console operations, so you don't need to write output operators for fstream or iostream (neither of which are supported either).

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    C lacks any useful features for producing type-safe generic containers.
    A std::vector does not use realloc but generally a placement-new since realloc does not properly destruct/construct objects.
    std::vector works due to templates which produce code for each set of types you give the template. But the code to a vector is fairly straightforward and simple, anyone could write it.
    In short, if containers are an important aspect of your application, is C really offering you useful tools for accomplishing your goal? If you miss C++ and STL so much, what is stopping you from using them?

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    > If you miss C++ and STL so much, what is stopping you from using them?

    As he mentioned in the original post, his compiler (probably either eVC++ 3.0 or eVC++ 4.0) doesn't support STL.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM

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