Making the size of an array variable

This is a discussion on Making the size of an array variable within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by ChaosEngine now that you know how to use new and delete for arrays.... don't. Don't tell me ...

  1. #16
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Quote Originally Posted by ChaosEngine
    now that you know how to use new and delete for arrays....


    don't.


    Don't tell me you've never felt the urge to show off to the professor by including cool new code that the professor won't teach for another month.

    This worked fine for me, if that's all you wanted

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int num;
        cout << "Enter the number of cells: ";
    	cin >> num;
    	int array[num];
    	
    	for (int i = 0; i < num; i++)
    	{
    		array[i] = num;
    	}
    	
    	for (int i = 0; i < num; i++)
    	{
    		cout << array[i] << " ";
    	}
    	cout << endl;
        system("PAUSE");
        return 0;
    }
    Last edited by indigo0086; 03-20-2006 at 09:19 AM.

  2. #17
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Quote Originally Posted by major_small
    it's all still pretty much the same amount of code, the only thing that really changes is who writes it
    I agree with you. I use both C arrays and C++ vector. However, according to Programming Pearls, C arrays are much faster for sorting, for example (qsort vs. algorithm's sort). For simple array use, when you dont need the dynamic behavior, the std::vector implies in checks for increasing size, for example.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Another advantage of std::vector over manually created dynamic arrays would be the use of RAII. I mean, you wont miss a delete[] because some maintenance programmer decided to insert a return somewhere before it, or some function called happens to throw an exception.

    However, according to Programming Pearls, C arrays are much faster for sorting, for example (qsort vs. algorithm's sort).
    Of course, std::sort() can be used on C-style arrays as well, and can actually be faster than qsort().
    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

  4. #19
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Quote Originally Posted by laserlight
    Of course, std::sort() can be used on C-style arrays as well, and can actually be faster than qsort().
    But then you are not using std::vector anymore, I am confused. The Programming Pearls used sort on vectors.

    Also, acessing time for vector, using [] is slower than C style ;\

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    But then you are not using std::vector anymore, I am confused.
    Consider this piece of code:
    Code:
    const int size = 5;
    int bar[size] = {3, 5, 1, 4, 2};
    std::sort(bar, bar + size);
    Also, acessing time for vector, using [] is slower than C style
    Premature optimisation is evil.
    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

  6. #21
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Quote Originally Posted by laserlight
    Consider this piece of code:
    Code:
    const int size = 5;
    int bar[size] = {3, 5, 1, 4, 2};
    std::sort(bar, bar + size);
    Sorry to insist, but my point here is that you are not using vector anymore.

    Quote Originally Posted by laserlight
    Premature optimisation is evil.
    I will leave this as a personal preference. Since I work a lot with matrices, vector< vector<int> > would be twice as bad.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Sorry to insist, but my point here is that you are not using vector anymore.
    Actually, I was responding to your "qsort vs. algorithm's sort" comment.

    I will leave this as a personal preference. Since I work a lot with matrices, vector< vector<int> > would be twice as bad.
    Have you actually profiled code to confirm this, or are you just taking what is written in "Programming Pearls" as holy scripture? Does the bottleneck in your code lie with the part that sorts the array/vector, and is the improvement really so great?
    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. #23
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Uhm... I understand. Ok, std::sort on C arrays can be faster then qsort(). However, you are still using C array.

    I havenīt profiled my code, and I agree that I could have been hasty. I will try to make some test with real code (applications) and if I have significant results (no matter if proves or not my point) I will tell them. I said that because, inspecting std::vector code, I saw that operator[] instantiates an iterator object.

  9. #24
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> This worked fine for me, if that's all you wanted

    That works only on compilers (like gcc that comes with Dev-C++) that support the language extension. It is not standard C++ code. C99 allows it, which is why gcc allows it unless you turn off that extension. As soon as you switch the code to another compiler it will not work.

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    However, you are still using C array.
    I would use a C-style array if I dont need a dynamic array.

    I said that because, inspecting std::vector code, I saw that operator[] instantiates an iterator object.
    That is probably implementation specific. From what I see, a vector should be no faster than a dynamic array, but for the most part it shouldnt have terrible performance either.
    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

  11. #26
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    There is also boost::array which will likely be part of the next standard (it might already be part of TR1) which encapsulates plain, non-dynamic arrays. I would use that instead of a C-style array on a major project.

  12. #27
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by Daved
    There is also boost::array which will likely be part of the next standard (it might already be part of TR1) which encapsulates plain, non-dynamic arrays. I would use that instead of a C-style array on a major project.
    it is part of TR1.

    My main point about vectors is this... they work. If you use a vector correctly you don't need to worry about matching new and delete.

    Code:
    void PrintArray(int *pArray)
    {
        throw anException;  
    }
    
    int main()
    {
        int* pArray = new int[5000];
    
        try 
        {
            PrintArray(pArray); // need to pass size
    
            delete pArray; // ooops memory leak
        }
        catch (...){}
    }
    The reason C++ has a bad reputation for productivity is that developers spend too much time worry about stupid cr@p like matching new and deletes. If you asked a java/c#/python programmer to write code like that, they'd laugh at you. With vector, you create a vector and get on with solving the problem, not ........ing about with the language.

    vectors are:
    • generic
    • exception safe
    • "automatic"
    • know their own size


    arrays are:
    • specific, you have to write the same code over and over
    • not exception safe, they cause memory leaks unless you wrap them in a class, in which case you're just reimplementing a half assed vector
    • manual, you have to remember to delete them, copy them, etc
    • don't know their own size


    Let me put this simply and in the strongest possible terms:

    There is no place in modern C++ where a dynamically allocated array is a better solution then a container.

    if you still don't believe me RTFFAQ
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  13. #28
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    I want to get a user to input a phrase into a char array[x] and then find the length of it to be stored in a file. HOw woould I do this?

  14. #29
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You should ask your question in its own thread. For C style strings (null terminated character arrays), you can use strlen to find the length of the string. However, in C++ you should use the C++ string class if you can.

  15. #30
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Yes but I have the problem of reading the string once it has been put in the file.

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

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. help on variable size array
    By 1qaz1234 in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2005, 12:02 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM

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