CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 28
  1. #1
    Join Date
    Apr 2011
    Posts
    8

    C++ question about arrays....

    Hello.
    I'm working on a program that is about to get student's information.
    so when a student uses the program, a field of memory should be allocated to him/her to save his/her information.
    unfortunaly i dont know how many students there are, so i cant use arrays because of constant size..therefor linked lists are being used...
    But i was wondering if this idea would work too or not:

    Code:
    const int OldSIZE=5;
    int *p=new int[OldSIZE];
    so we have an array with size of 5 that can hold 5 student's records and can be accessed by pointer "p".
    here is my idea and my problem that if we have a new student to save his/her data, how can i make a new array with new size that the new size is 1+OldSIZE and copy all information plus new students information to the new array?

    Code:
     size=size+1;  
    //size is 6 now. but we cant do it because it is constant
    
    int *q=new int[size];
    a code that can copy (array of pointer p) to (array of pointer q);
    delete []p;
     add the new student's info to the new array..
    how can i make this idea work??
    i hope i could explain it clearly, but ask me if ur confused what i meant...

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    Re: C++ question about arrays....

    int * q = new int(size);

  3. #3
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: C++ question about arrays....

    allocate new block of size+growth factor, loop through old block copying each element as you go to the new block, delete[] the old block. BTW manual memory management is a cardinal sin for stuff like this when you have tools such as std::vector in your arsenal. std::vector is a dynamic array that takes care of all the memory management for you. What you are doing is simulating a rudimentary std::vector
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  4. #4
    Join Date
    Apr 2011
    Location
    Sydney, Australia
    Posts
    3

    Re: C++ question about arrays....

    That's what I was going to say - why not just use a std::vector??

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

    Re: C++ question about arrays....

    Quote Originally Posted by salar View Post
    Hello.
    I'm working on a program that is about to get student's information.
    so when a student uses the program, a field of memory should be allocated to him/her to save his/her information.
    unfortunaly i dont know how many students there are, so i cant use arrays because of constant size..therefor linked lists are being used...
    But i was wondering if this idea would work too or not:

    Code:
    const int OldSIZE=5;
    int *p=new int[OldSIZE];
    so we have an array with size of 5 that can hold 5 student's records and can be accessed by pointer "p".
    Use vector, and make things much easier.
    Code:
    #include <vector>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int num;
        cout << "How many? :";
        cin >> num;
        vector<int>  p(num);
        // now p can be used just like an array of int.
        //...
    }
    That entire code allocates an "array" of num integers. Not only that, it destroys the array correctly. Not shown -- you can resize the array, insert elements anywhere within the array, etc., all without you having to do dynamic allocation by hand.

    So why are you not using this very simple, standard approach?

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Apr 2011
    Posts
    8

    Re: C++ question about arrays....

    isnt there any other ways than vectors??
    actually any other SIMPLE ways....

  7. #7
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: C++ question about arrays....

    Quote Originally Posted by salar View Post
    isnt there any other ways than vectors??
    actually any other SIMPLE ways....
    std::vector IS the simple way.
    The other simple alternative is to use an std::list. Or an std::deque.

    The alternative is either writing your own container (not simple), but you'd just end up with a vector clone. Or to manually manage memory through the use of new[] and delete[] (not simple either).
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: C++ question about arrays....

    As said, you could use a vector, but if a list is an appropriate container, why try to force it into an array?

  9. #9
    Join Date
    Apr 2011
    Posts
    8

    Re: C++ question about arrays....

    Quote Originally Posted by GCDEF View Post
    As said, you could use a vector, but if a list is an appropriate container, why try to force it into an array?
    because i need to use binary search to find students.
    binary search is not good for a list as i know is it?

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: C++ question about arrays....

    Quote Originally Posted by salar View Post
    because i need to use binary search to find students.
    binary search is not good for a list as i know is it?
    No it's not. If you need to search, a map would be perhaps a better container choice. If you want to use a binary search, your array would have to be sorted.

  11. #11
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: C++ question about arrays....

    Quote Originally Posted by salar View Post
    isnt there any other ways than vectors??
    actually any other SIMPLE ways....
    If this is a school project and if you are restricted to NOT use STL, check out realloc
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

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

    Re: C++ question about arrays....

    Quote Originally Posted by salar View Post
    isnt there any other ways than vectors??
    actually any other SIMPLE ways....
    OK, I'll ask you -- what is difficult about what I posted?

    Regards,

    Paul McKenzie

  13. #13
    Join Date
    Aug 2009
    Posts
    440

    Re: C++ question about arrays....

    To point out what others have said, if you are not restricted in what you can use for this, then use the STL. The time you spend learning how to use vectors, for example, will be extremely useful later on. You will probably spend more time debugging dynamic memory allocation and management. Let the STL do the work for you if you can.

  14. #14
    Join Date
    Apr 2011
    Posts
    8

    Re: C++ question about arrays....

    Quote Originally Posted by Paul McKenzie View Post
    OK, I'll ask you -- what is difficult about what I posted?

    Regards,

    Paul McKenzie
    Its not difficult, i meant a way by using normal arrays with pointers and new/delete operators...
    as VladimirF noticed it is a school project that my teacher has not thought us of vectors and gave us the project. so i think it should be done in another way...is there?

  15. #15
    Join Date
    Apr 2011
    Posts
    8

    Re: C++ question about arrays....

    Quote Originally Posted by VladimirF View Post
    If this is a school project and if you are restricted to NOT use STL, check out realloc
    Nop i dont think we are allowed.
    anyways thanks for the link.

Page 1 of 2 12 LastLast

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