Results 1 to 13 of 13

Thread: very very base question

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default very very base question

    Hello, I have this simple doubt: what's the code more properly?And Why?(my question is on the vector "vec") thanks.
    Qt Code:
    1. string str;
    2. while(file.getline(str)) {
    3. vector<int> vec;
    4. ..................
    5. while (parseStr) {
    6. vec.push_back(numberContainedInStr);
    7. }
    8. otherVector.push_back(vec);
    9. }
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. string str;
    2. vector<int> vec;
    3. while(file.getline(str)) {
    4. ..................
    5. while (parseStr) {
    6. vec.push_back(numberContainedInStr);
    7. }
    8. otherVector.push_back(vec);
    9. vec.clear();
    10. }
    To copy to clipboard, switch view to plain text mode 
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: very very base question

    It doesn't really matter. The second version is a bit faster, but the difference is ignorable.

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: very very base question

    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?
    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: very very base question

    No, because the vector is deallocated each time you leave the loop.

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: very very base question

    Quote Originally Posted by wysota View Post
    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)
    Regards

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: very very base question

    I suggest you subclass std::vector and place some debugging info in the constructor and destructor, run the application and see for yourself.

  7. The following user says thank you to wysota for this useful post:

    mickey (5th March 2008)

  8. #7
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: very very base question

    another base question (I have prefer do not open another post for this dubt)
    Qt Code:
    1. vector<int> vec(10); //creating on the stack the space for 10 integer
    2. vec.push_back(10); //copy at begin of vec the constant value 10 at position '1'
    To copy to clipboard, switch view to plain text mode 
    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.
    Regards

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: very very base question

    Quote Originally Posted by mickey View Post
    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:

    Qt Code:
    1. class Vector {
    2. public:
    3. Vector(int initialsize = 0);
    4. Vector(const Vector &copy);
    5. ~Vector();
    6. int &operator[](int index); // c[7] = ...
    7. int size() const;
    8. void reserve(int n); // reserves memory for n items
    9. void squeeze(); // opposite of reserve();
    10. void fill(int count, int value); // fills count items with value
    11. void clear(); // removes all items from the vector
    12. void push_back(); // appends an item
    13. void insert(int position, int value); // insert an item at arbitrary position
    14. int at(int position) const; // returns value at position
    15. Vector operator+(const QVector &other) const; // Vector c = a+b;
    16. Vector &operator+=(const QVector &other); // a+=b;
    17. bool operator==(const QVector &other) const; // if(a==b) ...
    18. Vector &operator=(const QVector &other); // a = b;
    19. };
    To copy to clipboard, switch view to plain text mode 

  10. #9
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: very very base question

    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????
    Last edited by mickey; 18th March 2008 at 21:21.
    Regards

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: very very base question

    Yes, but vector's data is still on heap. I really suggest you do the excercise.

  12. #11
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: very very base question

    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...
    Regards

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: very very base question

    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.

  14. #13
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: very very base question

    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?
    Qt Code:
    1. void push_back(const T val){
    2. if ( _capacity == 0 ) {
    3. _capacity = 8;
    4. _mem = new T[_capacity];
    5. _mem[_size++] = val; //first element
    6. return;
    7. }
    8. if ( _size < _capacity ) {
    9. _mem[_size++] = val;
    10. }
    11. else {
    12. T* temp = new T[_capacity];
    13. for (int i=0; i < _capacity; i++)
    14. temp[i] = _mem[i];
    15. //memcpy(temp, _mem, sizeof(T) * _capacity);
    16. int oldCapacity = _capacity;
    17. _capacity += 8;
    18. delete []_mem;
    19. _mem = new T[_capacity];
    20. //memcpy(_mem, temp, sizeof(T) * oldCapacity);
    21. for (int i=0; i < oldCapacity; i++)
    22. _mem[i] = temp[i];
    23. delete []temp;
    24. _mem[_size++] = val;
    25. }
    26. }
    27. //main.cpp
    28. MVector<Person> p(1);
    29. p.push_back( Person ("mike", 99, 'M') );
    30. .............................................
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files
    Regards

Similar Threads

  1. Eclipse "File->New Class" function question
    By MarkoSan in forum General Programming
    Replies: 0
    Last Post: 20th January 2008, 18:22
  2. templates / Q_OBJECT question
    By _borker_ in forum Qt Programming
    Replies: 6
    Last Post: 19th December 2007, 20:35
  3. Connecting to a base class signal?
    By AaronMK in forum Qt Programming
    Replies: 4
    Last Post: 26th October 2007, 22:37
  4. Access to QSqlTableModel::isDirty Question.
    By patrik08 in forum Qt Programming
    Replies: 3
    Last Post: 12th April 2007, 17:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.