Save and Load memory data to Files in C++ ?

This is a discussion on Save and Load memory data to Files in C++ ? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by IndioDoido sorry... i've figured it out used: Code: dadosProp.reserve(50); dadosVeiculo.reserve(50); if(dadosProp.empty() == false) { for(int i=0; i>=50; ...

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by IndioDoido View Post
    sorry...
    i've figured it out

    used:
    Code:
    dadosProp.reserve(50);
    dadosVeiculo.reserve(50);
    
    	if(dadosProp.empty() == false)
    	{
    		for(int i=0; i>=50; i++)
    That's not really solving your problem, despite the fact that it may appear to work normally. The problem is that when you call dadosProp[i], for example, it's assuming that that element already exists - which means that the vector's size must be big enough. Using reserve() sets the capacity, but not the size. You can create a vector of size 50 by calling the appropriate constructor. For example, if dadosProp is a vector of ints,
    Code:
    std::vector<int> dadosProp(50);
    Alternatively, if you are going to write elements of the vector sequentially starting at index 0, you can
    do it using push_back() (I prefer this since it avoids the often unnecessary initialization which the above constructor performs on the elements). If you do this, you can still call reserve() as an optimization if you have an upper bound on how big dadosProp can get, but it's not necessary for correctness.

    To see the problem with your original code, if you're compiling with GCC, try using the -D_GLIBCXX_DEBUG option, which causes bounds checking on each vector access when you run the program.

    http://gcc.gnu.org/onlinedocs/libstdc++/debug.html

    Edit: Actually in the case of a vector of a primitive type such as int, the overhead of push_back()'s having to modify the size is probably just as bad as the initialization in the constructor. But push_back() is safer when you can use it, since it's guaranteed not to cause invalid writes such as yours. On the other hand, if the writes need to be done in any order other than sequentially starting with index 0, you have to make sure the vector has large enough size (not just capacity) when you create it.
    Last edited by robatino; 10-16-2007 at 11:46 PM.

  2. #17
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    ups!!!

    i didn't copy the right code :-S
    sorry guys.

    Code:
    dadosProp.reserve(50);
    dadosVeiculo.reserve(50);
    
    	if(dadosProp.empty() == false)
    	{
    		for(int i=0; i<50; i++)
    "Artificial Intelligence usually beats natural stupidity."

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    As I explained, your code is broken. Using reserve() is an optimization only - it doesn't affect correctness. If you compile using GCC with the option mentioned above, or alternatively replace [] with at() for subscripting to do bounds checking with each access, your program will crash with a complaint about out-of-bounds access, since reserve() only sets the capacity, and doesn't change the size, which remains at 0. You could fix it by using resize() instead of reserve(), or more simply just use the constructor which sets the size at creation.

  4. #19
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    I was confused about what GIYF is and so I thought I would Google it. Then I realized - OOOOOOOOh, Google is your friend.

    I still didn't know what JFGI, STFW, and RTFM were, but using the advice from the first one, I was able to find the others. It turns out I had heard of RTFM before.

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

Similar Threads

  1. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 10:19 PM
  2. File Save and Load
    By lukesowersby in forum C Programming
    Replies: 3
    Last Post: 05-13-2009, 01:49 AM
  3. Replies: 11
    Last Post: 04-17-2008, 03:29 AM
  4. loading files, and memory allocation
    By Rard in forum C Programming
    Replies: 14
    Last Post: 04-20-2006, 04:22 PM
  5. Replies: 3
    Last Post: 09-12-2005, 10:08 AM

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