CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2012
    Posts
    10

    Vectors and Arrays

    Is there a difference between vectors and Arrays? If I know arrays well and I can deal with them dynamically, do I have also to know about vectors ?

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Vectors and Arrays

    Quote Originally Posted by ahmed_sobhy View Post
    Is there a difference between vectors and Arrays? If I know arrays well and I can deal with them dynamically,
    You must be using another computer language. In C++, arrays are not dynamic.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    May 2012
    Posts
    10

    Re: Vectors and Arrays

    In c++, I can use pointers with arrays to allocate memory dynamically. Am I true ? So what are vectors for ?

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Vectors and Arrays

    Quote Originally Posted by ahmed_sobhy View Post
    In c++, I can use pointers with arrays to allocate memory dynamically. Am I true ?
    You are not "true".

    An array is not a pointer. A pointer is not an array. An array in C++ has a specific definition:
    Code:
    int x[100];
    That is an array. Now how do you dynamically change the size of it in C++? You can't.

    That's what vectors are for -- a vector can be dynamically resized.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Vectors and Arrays

    Secondly, as far as dynamically allocated memory and pointers are concerned, here is the issue with them:
    Code:
    class foo
    {
        private:
           char *ptr;
    
        public:
            foo() 
            {
                 ptr = new char [10];
            }
    
            ~foo() 
            {
                delete [] ptr;
            }
    };
    
    int main()
    {
        foo x;
        foo y = x;  
    }  // double deletion error -- deleting the same address twice!
    Fix the bug in this program using dynamically allocated memory and pointers. Once you do that, then you will see why vector is much more powerful and easier to use then raw pointers and dynamically allocated memory.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; May 30th, 2012 at 05:14 AM.

  6. #6
    Join Date
    May 2012
    Posts
    10

    Re: Vectors and Arrays

    Thank you a lot for your help. It seems I will get to know more about vectors.

  7. #7
    Join Date
    May 2009
    Posts
    2,413

    Re: Vectors and Arrays

    Quote Originally Posted by ahmed_sobhy View Post
    It seems I will get to know more about vectors.
    It's important to know all main containers of the C++ standard library. That includes vector of course but also list, deque, set, map, unordered_set and unordered_map as well as array (which is not the same as the language level array).

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