Thread: copy() segfaults

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    Lund, Sweden
    Posts
    18

    copy() segfaults

    Does anyone know why the following code segfaults? Valgrind gives me no hints.
    I compile with 'g++ knas.cpp -pedantic -Wall -ansi -g -o knas' and get no errors/warnings.

    Code:
    #include <algorithm>
    #include <vector>
    #include <string>
    
    int main() {
      std::string str = "hello";
      std::vector<char> vec;
      copy(str.begin(), str.end(), vec.begin());
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    #include <algorithm>
    #include <vector>
    #include <string>
    
    int main() {
      std::string str = "hello";
      std::vector<char> vec;
      vec.reserve(str.length());
      copy(str.begin(), str.end(), vec.begin());
      return 0;
    }
    Kurt

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Or:
    Code:
    #include <algorithm>
    #include <vector>
    #include <string>
    #include <iterator>
    
    int main()
    {
      std::string str = "hello";
      std::vector<char> vec;
      std::copy(str.begin(), str.end(), std::back_inserter(vec) );
      return 0;
    }

    vec starts out without any memory allocated for storage of anything so trying to push data into it the wrong way is bad.


    Quote Originally Posted by ZuK View Post
    Code:
    #include <algorithm>
    #include <vector>
    #include <string>
    
    int main() {
      std::string str = "hello";
      std::vector<char> vec;
      vec.reserve(str.length());
      copy(str.begin(), str.end(), vec.begin());
      return 0;
    }
    Kurt
    Shouldn't that be resize?
    Last edited by hk_mp5kpdw; 09-29-2007 at 02:23 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Lund, Sweden
    Posts
    18
    Ah, thanks for the quick reply. Seems like that was it.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Code:
    #include <vector>
    #include <string>
    
    int main() {
      std::string str = "hello";
      std::vector<char> vec(str.begin(), str.end());
    }

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by hk_mp5kpdw View Post
    Shouldn't that be resize?
    Yes. Should have been.
    Reserve just sets the capacity of the vector that's why it wouldn't segfault.
    But the vector would still be empty after the call to copy.
    Kurt
    Last edited by ZuK; 09-29-2007 at 02:49 PM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by ZuK View Post
    Yes. Should have been.
    Reserve just sets the capacity of the vector that's why it wouldn't segfault.
    But the vector would still be empty after the call to copy.
    Kurt
    The vector would still have size 0, but if you accessed its elements by indexing as in vec[i], it would probably appear to work normally (this happened to me when I was first learning how to use vectors). However, vec.at(i) which does bounds checking would catch the problem. I still prefer the regular indexing since it's easier to read, but at() is good for debugging.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Actually, any halfway decent standard library ought to assert if you use [] with an out-of-bound index in debug mode.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by CornedBee View Post
    Actually, any halfway decent standard library ought to assert if you use [] with an out-of-bound index in debug mode.
    Thank you - I was completely unaware of this. Using -D_GLIBCXX_DEBUG with gcc generates the desired warnings, which saves me the trouble of making a second copy of the source with [] replaced by at().
    Code:
    #include <vector>
    
    int main() {
      std::vector<int> v;
      v.reserve(1);
      v[0] = 1;
    }
    Output when running executable compiled with -D_GLIBCXX_DEBUG:

    /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/debug/vector:192:
    error: attempt to subscript container with out-of-bounds index 0, but
    container only holds 0 elements.

    Objects involved in the operation:
    sequence "this" @ 0x0xbfa91890 {
    type = N15__gnu_debug_def6vectorIiSaIiEEE;
    }
    Aborted

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Gcc can't find obvious copy constructor
    By SevenThunders in forum C++ Programming
    Replies: 13
    Last Post: 03-19-2009, 02:41 PM
  2. calling copy constructor from template
    By Ancient Dragon in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2005, 01:54 PM
  3. dynamic memory alloccation & returning objects
    By haditya in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2005, 11:55 PM
  4. Copy Constructor crashing program
    By bob2509 in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2002, 04:21 PM
  5. Copy Constructor Help
    By Jubba in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 11:15 AM