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...
Re: C++ question about arrays....
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
Re: C++ question about arrays....
That's what I was going to say - why not just use a std::vector??
Re: C++ question about arrays....
Quote:
Originally Posted by
salar
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
Re: C++ question about arrays....
isnt there any other ways than vectors??
actually any other SIMPLE ways....
Re: C++ question about arrays....
Quote:
Originally Posted by
salar
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).
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?
Re: C++ question about arrays....
Quote:
Originally Posted by
GCDEF
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?
Re: C++ question about arrays....
Quote:
Originally Posted by
salar
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.
Re: C++ question about arrays....
Quote:
Originally Posted by
salar
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
Re: C++ question about arrays....
Quote:
Originally Posted by
salar
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
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.
Re: C++ question about arrays....
Quote:
Originally Posted by
Paul McKenzie
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?
Re: C++ question about arrays....
Quote:
Originally Posted by
VladimirF
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.:D
anyways thanks for the link.