Error with a vector

This is a discussion on Error with a vector within the C++ Programming forums, part of the General Programming Boards category; Something is illegal with that code? Code: for(i = 0; numero % (teste.at(teste.capacity()-o)) == 0; i++, o++) Im getting a ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    57

    Error with a vector

    Something is illegal with that code?

    Code:
    for(i = 0; numero %  (teste.at(teste.capacity()-o)) == 0; i++, o++)
    Im getting a runtime error "std:ut_of_range at memory location xxxxxx".
    And if i put a "6" in the place of the "(teste.at(teste.capacity()-o))" the code runs.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >teste.at(teste.capacity()-o)
    You probably want teste.size() rather than teste.capacity(). The capacity is the amount of memory allocated to the container, which is likely to be more than the number of actual items stored in it.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    57
    Hmmm thanks for the advice, but still giving the same error :P

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    I noticed you didn't initialize o. If this value is larger than the capacity() (or size() in the same regards), then at() will be taking a negative number, giving you an error in which your out of bounds in memory. I actually thought at() did bounds checking. It apparently doesn't or I'm just not identifying the actual error. Did you mean to initialize o?
    Last edited by SlyMaelstrom; 09-28-2006 at 07:03 PM.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    Make sure o is between 1 and teste.size(). If it is 0 it will give an invalid index.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    57
    Yes it is. When the loop is execulte by the first time, o = 0.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Well there you go... vector indices (like all other C++ subscript indices) start at 0. There for the capacity is one more than the last index. o should start at one, as Daved said.
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    57
    Worked, thanks =]

  9. #9
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Quote Originally Posted by SlyMaelstrom
    There for the capacity is one more than the last index..
    You mean the size is one more than the last index
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Quote Originally Posted by JaWiB
    You mean the size is one more than the last index
    Whoops, nice catch. Though, in my defense, I believe the capacity is the size unless it's explicitly set.
    Sent from my iPadŽ

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    The capacity is usually much more than the size as Prelude mentioned. Otherwise, every push_back would require another expensive re-allocation.

  12. #12
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Quote Originally Posted by SlyMaelstrom
    Whoops, nice catch. Though, in my defense, I believe the capacity is the size unless it's explicitly set.
    Quite likely not true. I don't know if the standard makes any guarantees for capacity() (esp. as it relates to size() ), but just try something like this:
    Code:
    #include <vector>
    #include <iostream>
    
    int main()
    {
      std::vector<int> vec;
    
      for (int i=0;i<50;++i)
      {
        std::cout<<"Size: "<<vec.size()<<' ';
        std::cout<<"Capacity: "<<vec.capacity()<<'\n';
        vec.push_back(i);
        
      }
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  13. #13
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    I acutally though vectors did allocate memory on a need-for basis. Here's what I got from cppreference.com
    C++ containers are designed to grow in size dynamically. This frees the programmer from having to worry about storing an arbitrary number of elements in a container. However, sometimes the programmer can improve the performance of her program by giving hints to the compiler about the size of the containers that the program will use. These hints come in the form of the reserve() function and the constructor used in the above example, which tell the compiler how large the container is expected to get.
    I haven't checked on my compiler what the capacity is when no reserve is given, but I always suspected it was zero when no elements exist in the vector.

    In response to Jawib: Very interesting results
    Code:
    Size: 0 Capacity: 0
    Size: 1 Capacity: 1
    Size: 2 Capacity: 2
    Size: 3 Capacity: 4
    Size: 4 Capacity: 4
    Size: 5 Capacity: 8
    Size: 6 Capacity: 8
    Size: 7 Capacity: 8
    Size: 8 Capacity: 8
    Size: 9 Capacity: 16
    Size: 10 Capacity: 16
    Size: 11 Capacity: 16
    Size: 12 Capacity: 16
    Size: 13 Capacity: 16
    Size: 14 Capacity: 16
    Size: 15 Capacity: 16
    Size: 16 Capacity: 16
    Size: 17 Capacity: 32
    Size: 18 Capacity: 32
    Size: 19 Capacity: 32
    Size: 20 Capacity: 32
    Size: 21 Capacity: 32
    Size: 22 Capacity: 32
    Size: 23 Capacity: 32
    Size: 24 Capacity: 32
    Size: 25 Capacity: 32
    Size: 26 Capacity: 32
    Size: 27 Capacity: 32
    Size: 28 Capacity: 32
    Size: 29 Capacity: 32
    Size: 30 Capacity: 32
    Size: 31 Capacity: 32
    Size: 32 Capacity: 32
    Size: 33 Capacity: 64
    Size: 34 Capacity: 64
    Size: 35 Capacity: 64
    Size: 36 Capacity: 64
    Size: 37 Capacity: 64
    Size: 38 Capacity: 64
    Size: 39 Capacity: 64
    Size: 40 Capacity: 64
    Size: 41 Capacity: 64
    Size: 42 Capacity: 64
    Size: 43 Capacity: 64
    Size: 44 Capacity: 64
    Size: 45 Capacity: 64
    Size: 46 Capacity: 64
    Size: 47 Capacity: 64
    Size: 48 Capacity: 64
    Size: 49 Capacity: 64
    I'll have to take a look when I have the time to see how my implementation processes this.
    Last edited by SlyMaelstrom; 09-28-2006 at 07:39 PM.
    Sent from my iPadŽ

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    57
    Here it gives the same error:
    Code:
    for(int i = 1; i < teste.size(); i++){
    file << teste.at(teste.front()+i);
    }
    Last edited by Tropicalia; 09-28-2006 at 07:40 PM.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    Why do you have front() there? The front() function returns the first value in the vector. You probably just want i.

    Also, in that loop, i = 0 would be valid if you are trying to output all values in the vector.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM

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