Like Tree1Likes
  • 1 Post By laserlight

C; A Dilemma

This is a discussion on C; A Dilemma within the C++ Programming forums, part of the General Programming Boards category; As I continue to study the C++ language, I have arrived at a point of inevitability where the book author ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    178

    C; A Dilemma

    As I continue to study the C++ language, I have arrived at a point of inevitability where the book author has decided to teach pointers, C-Style strings, and arrays.

    The Author's advice is to avoid the use of pointers, C-Style strings, and arrays in favor of string, vector, and its iterator. I agree with this ideology.

    My question is while the author discourages use, can I still rely on points, C-Style Strings, and arrays when they seem applicable?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,258
    Quote Originally Posted by Imanuel
    can I still rely on points, C-Style Strings, and arrays when they seem applicable?
    Yes, if by "points" you mean "pointers".
    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

  3. #3
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    326
    Yes, you can, and in some cases you will be required to use pointers to do certain things. For example, to do *dynamic memory allocation* where you allocate memory at runtime requires a pointer. When you get into inheritance you'll see other uses as well.

    The only thing I will agree with the author on is that std::vector<T> and std::string may replace _most_ uses of arrays and strings. However, a vector is not logically an array. A vector is logically a sequence, according to the STL documentation, and an array does not necessarily represent a logical sequence. Thus, the tr1::array<T, N> class.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,258
    Quote Originally Posted by MacNilly
    However, a vector is not logically an array. A vector is logically a sequence, according to the STL documentation, and an array does not necessarily represent a logical sequence. Thus, the tr1::array<T, N> class.
    Tee hee:
    Quote Originally Posted by C++11 Clause 23.3.2.1 Paragraph 1 (part)
    The header <array> defines a class template for storing fixed-size sequences of objects.
    By design and in practice, a vector is an array with dynamic size, and insofar as an array "does not necessarily represent a logical sequence", a vector also need not represent a logical sequence. When making the choice between std::array and std::vector, the first thing that comes to mind is primarily whether I need the size to be fixed or dynamic.
    iMalc 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

  5. #5
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    326
    Quote Originally Posted by laserlight View Post
    By design and in practice, a vector is an array with dynamic size
    A vector is implemented as an array, and because of the requirements that operator[] be O(1) the only thing you can use is an array. But that's besides the point.

    and insofar as an array "does not necessarily represent a logical sequence", a vector also need not represent a logical sequence.
    Actually it does. In terms of STL concepts, vector "is a model of" Forward Sequence. As far as the meaning of that goes, a vector is a sequence.

    When making the choice between std::array and std::vector, the first thing that comes to mind is primarily whether I need the size to be fixed or dynamic.
    True enough.

  6. #6
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    326
    In support of my argument (which is admittedly a fine-point), I offer this example.

    In a std::vector, is it always true that every element 0 to size()-1, is a valid constructed object?

    I say the answer is yes.

    In a regular C++ array (not std::array<T,N>), is it true that each object is a valid constructed object? Not necessarily.

    Code:
    int *foo = new int[100];
    
    int[0] = 1;
    int[99] = 100;
    Is that a sequence? All the elements in the middle are undefined. Now maybe std::array<T,N> default-construct each element, thus making it a sequence. But I meant arrays in general.

    [EDIT]
    Opps, I meant to use malloc(100 * sizeof(int)), not operator new. I say malloc because often a container will separate allocation of memory from object construction. I just think that to be a logical sequence, each element must be defined (as in constructed), in addition to the standard mathematical definition of a sequence.
    [/EDIT]
    Last edited by MacNilly; 10-05-2011 at 03:36 PM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,258
    Quote Originally Posted by MacNilly
    A vector is implemented as an array, and because of the requirements that operator[] be O(1) the only thing you can use is an array.
    You might want to take a look at the complexity requirements of std::deque and ask yourself if a typical std::deque implementation uses an internal array, or does it do a little more than that?

    Quote Originally Posted by MacNilly
    But that's besides the point.
    What is your point? My objection to your statements in post #3 is really that I consider a vector to be a dynamic array, so the claim that "a vector is not logically an array" is ludicruous to me. Consequently, I object to your conclusion that std::array was introduced because a vector is always a sequence, whereas an array supposedly might not be a sequence.

    Quote Originally Posted by MacNilly
    Actually it does. In terms of STL concepts, vector "is a model of" Forward Sequence. As far as the meaning of that goes, a vector is a sequence.
    You have just shot yourself in the foot since I quoted the standard as clearly stating that std::array is a sequence container

    Quote Originally Posted by MacNilly
    Is that a sequence?
    Let me assume that you used malloc, and that int[0] and int[99] should be foo[0] and foo[99], respectively.

    It depends on your point of view. Logically, that is not a sequence, because it is not intended to be interpreted as a sequence. But if you replaced with a std::vector<int>(100), it would still not be a logical sequence, because it is still not intended to be interpreted as a logical sequence.

    This is what I mean by my statement that 'insofar as an array "does not necessarily represent a logical sequence", a vector also need not represent a logical sequence'. For example, an array can be used to implement a heap, and a heap is not a sequence. A vector can also be used to implement a heap, and that certainly does not make a heap a sequence.

    Quote Originally Posted by MacNilly
    All the elements in the middle are undefined. Now maybe std::array<T,N> default-construct each element, thus making it a sequence. But I meant arrays in general.
    The values of the elements are undefined. However, the objects exist, since they are of type int. You can validly assign to foo[50].

    If you used a non-POD class type, then the objects would not exist. However, you would also not have an array of objects. Rather, you would have a pool of memory for which you say, use placement new to create objects at desired locations.

    Quote Originally Posted by MacNilly
    Opps, I meant to use malloc(100 * sizeof(int)), not operator new. I say malloc because often a container will separate allocation of memory from object construction. I just think that to be a logical sequence, each element must be defined (as in constructed), in addition to the standard mathematical definition of a sequence.
    As for your conclusion that std::array was introduced because a vector is always a sequence, whereas an array supposedly might not be a sequence: it must fall apart here, because you consciously reached for malloc instead of demonstrating with std::array, and in fact you explicitly excluded std::array.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. file i/o dilemma
    By adramalech in forum C Programming
    Replies: 27
    Last Post: 11-11-2008, 12:30 AM
  2. fstream dilemma
    By Amyaayaa in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2008, 08:53 PM
  3. yet another dilemma...when they will end? :)
    By Chaplin27 in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2004, 11:31 AM
  4. dilemma
    By axon in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 12-03-2003, 01:28 PM
  5. moral dilemma
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 03-25-2002, 12:57 AM

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