PDA

View Full Version : very very base question



mickey
4th March 2008, 01:20
Hello, I have this simple doubt: what's the code more properly?And Why?(my question is on the vector "vec") thanks.


string str;
while(file.getline(str)) {
vector<int> vec;
..................
while (parseStr) {
vec.push_back(numberContainedInStr);
}
otherVector.push_back(vec);
}
or


string str;
vector<int> vec;
while(file.getline(str)) {
..................
while (parseStr) {
vec.push_back(numberContainedInStr);
}
otherVector.push_back(vec);
vec.clear();
}

wysota
4th March 2008, 14:35
It doesn't really matter. The second version is a bit faster, but the difference is ignorable.

mickey
4th March 2008, 21:29
but in the first, Do I allocate n vec on the stack ?(that code is inside a method of an object that'tis created without the new operator) If so (and If the file is large), couldn't I have a stack overflow or similar problem?

wysota
4th March 2008, 21:58
No, because the vector is deallocated each time you leave the loop.

mickey
5th March 2008, 00:13
No, because the vector is deallocated each time you leave the loop.
sorry but I leave the loop (the first) only when the file is at the end; so for each line I read, I allocate one vector. Isn't it? (I mean this when I spoke about stack overflow)

wysota
5th March 2008, 05:06
I suggest you subclass std::vector and place some debugging info in the constructor and destructor, run the application and see for yourself.

mickey
18th March 2008, 17:55
another base question (I have prefer do not open another post for this dubt)


vector<int> vec(10); //creating on the stack the space for 10 integer
vec.push_back(10); //copy at begin of vec the constant value 10 at position '1'

I read vector.h and I see the push_back use the insert() and this last use two function Allocate ad Deallocate; are these working on the stack, I guess?
What more on these allocate and deallocate how work? (any links)

thanks.

wysota
18th March 2008, 19:33
are these working on the stack, I guess?
No, vector allocates its data on heap.


What more on these allocate and deallocate how work? (any links)

I have an excercise for you. Go ahead and implement your own vector class - this is the best way to learn how things work. It's an easy task and the vector can be very simple, just make it work, here is an example interface to implement:


class Vector {
public:
Vector(int initialsize = 0);
Vector(const Vector &copy);
~Vector();
int &operator[](int index); // c[7] = ...
int size() const;
void reserve(int n); // reserves memory for n items
void squeeze(); // opposite of reserve();
void fill(int count, int value); // fills count items with value
void clear(); // removes all items from the vector
void push_back(); // appends an item
void insert(int position, int value); // insert an item at arbitrary position
int at(int position) const; // returns value at position
Vector operator+(const QVector &other) const; // Vector c = a+b;
Vector &operator+=(const QVector &other); // a+=b;
bool operator==(const QVector &other) const; // if(a==b) ...
Vector &operator=(const QVector &other); // a = b;
};

mickey
18th March 2008, 20:10
sorry what I don't have clear is if I create a vector in this manner: vector<int> vec; it works as a variaile (int a;) allocate on the stack (and i don't need to delete it as when I write vector<int>* vec = new vector<int>(); ) is this?

EDIT:: it's not clear what's the aim of exercise(how can it help me). BTW, the exercise is to implement a my class vector or subclassing th class std::vector????

wysota
18th March 2008, 21:04
Yes, but vector's data is still on heap. I really suggest you do the excercise.

mickey
19th March 2008, 00:14
Hello,
I have implemented my own vector class; it contains a struct Elem {...} and this last has Elem* succ, before, and T value; it seems ok; this vector works as a linkedList; the push do a new whereas the pop do a delete and both them change the pointer properly;
Hence new "mean" heap; Did you mean this? Question: why do I can rely on the fact that who has done vector.h, have used the same way of mine??? Maybe, they can have used same machanism at more low level to do the things on the stack....
Second: my end() method return the pointer to the last element of my vector; in vector.h instead it return an iterator after the last element (it is an iterator and not a pointer). Am I doing something wrong?

Thanks...

wysota
19th March 2008, 05:35
A linked list is not a vector. A requirement for the latter is that items are placed in a sequence in memory - this is not true for a linked list.

I suggest you think about a method of putting vector data on stack.

mickey
26th March 2008, 22:01
Hello,
one problem occurs with this weird push: I attach my class Person (where I use intentionally char* _sex to do the things harder). I resize the MVector on each 8 * T; I'd like to use memcpy() but with it, "Person:: operator=", doesn't start. Besides, when I delete _mem (second else branch), the local temp loose information about its _sex -> it seems _mem[]._sex and temp[]._sex refers to the same block; so I used these loop (and operator= start); but is maybe something to do better, please?


void push_back(const T val){
if ( _capacity == 0 ) {
_capacity = 8;
_mem = new T[_capacity];
_mem[_size++] = val; //first element
return;
}
if ( _size < _capacity ) {
_mem[_size++] = val;
}
else {
T* temp = new T[_capacity];
for (int i=0; i < _capacity; i++)
temp[i] = _mem[i];
//memcpy(temp, _mem, sizeof(T) * _capacity);
int oldCapacity = _capacity;
_capacity += 8;
delete []_mem;
_mem = new T[_capacity];
//memcpy(_mem, temp, sizeof(T) * oldCapacity);
for (int i=0; i < oldCapacity; i++)
_mem[i] = temp[i];
delete []temp;
_mem[_size++] = val;
}
}
//main.cpp
MVector<Person> p(1);
p.push_back( Person ("mike", 99, 'M') );
.............................................