Segfault for particular data

Hello everyone, I am new to programming and I can tell you that it is much harder than I thought It would be. Respect to everyone who can solve problems related to c++ bugs all day long. OK. Here is my problem. I was supposed to merge two text files into one by my function as a homework. I did it and it worked well. But for some input data (particular text file) segmentation fault occurs. I dont know what is the difference between files that merged fine and that that cannot be merged. Data inside are very similar. For loading data into memory from the file I use vectors. I searched through everything and nothin found. Any ideas?

Debugger says:

==3602== Memcheck, a memory error detector
==3602== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==3602== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==3602== Command: /home/ubuntu/NetBeansProjects/Domaca1/dist/Debug/GNU-Linux-x86/domaca1
==3602==
==3602== Invalid read of size 4
==3602== at 0x40C85E7: std::string::assign(std::string const&) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.14)
==3602== by 0x40C86C3: std::string::operator=(std::string const&) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.14)
==3602== by 0x804942C: joinFiles(char const*, char const*, char const*) (main.cpp:46)
==3602== by 0x804A1F9: main (main.cpp:174)
==3602== Address 0x42d380c is 0 bytes after a block of size 4 alloc'd
==3602== at 0x402641D: operator new(unsigned int) (vg_replace_malloc.c:255)
==3602== by 0x804AF73: __gnu_cxx::new_allocator<std::string>::allocate(unsigned int, void const*) (new_allocator.h:89)
==3602== by 0x804AB27: std::_Vector_base<std::string, std::allocator<std::string> >::_M_allocate(unsigned int) (in /home/ubuntu/NetBeansProjects/Domaca1/dist/Debug/GNU-Linux-x86/domaca1)
==3602== by 0x804A706: std::_Vector_base<std::string, std::allocator<std::string> >::_Vector_base(unsigned int, std::allocator<std::string> const&) (stl_vector.h:113)
==3602== by 0x804A3BD: std::vector<std::string, std::allocator<std::string> >::vector(unsigned int, std::string const&, std::allocator<std::string> const&) (stl_vector.h:229)
==3602== by 0x8049373: joinFiles(char const*, char const*, char const*) (main.cpp:38)
==3602== by 0x804A1F9: main (main.cpp:174)
==3602==
==3602== Invalid read of size 4
==3602== at 0x40C8689: std::string::assign(std::string const&) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.14)
==3602== by 0x40C86C3: std::string::operator=(std::string const&) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.14)
==3602== by 0x804942C: joinFiles(char const*, char const*, char const*) (main.cpp:46)
==3602== by 0x804A1F9: main (main.cpp:174)
==3602== Address 0xfffffffc is not stack'd, malloc'd or (recently) free'd
==3602==
==3602==
==3602== Process terminating with default action of signal 11 (SIGSEGV)
==3602== Access not within mapped region at address 0xFFFFFFFC
==3602== at 0x40C8689: std::string::assign(std::string const&) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.14)
==3602== by 0x40C86C3: std::string::operator=(std::string const&) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.14)
==3602== by 0x804942C: joinFiles(char const*, char const*, char const*) (main.cpp:46)
==3602== by 0x804A1F9: main (main.cpp:174)
==3602== If you believe this happened as a result of a stack
==3602== overflow in your program's main thread (unlikely but
==3602== possible), you can try to increase the size of the
==3602== main thread stack using the --main-stacksize= flag.
==3602== The main thread stack size used in this run was 8388608.
==3602==
==3602== HEAP SUMMARY:
==3602== in use at exit: 25,755 bytes in 13 blocks
==3602== total heap usage: 16 allocs, 3 frees, 34,312 bytes allocated
==3602==
==3602== LEAK SUMMARY:
==3602== definitely lost: 0 bytes in 0 blocks
==3602== indirectly lost: 0 bytes in 0 blocks
==3602== possibly lost: 115 bytes in 5 blocks
==3602== still reachable: 25,640 bytes in 8 blocks
==3602== suppressed: 0 bytes in 0 blocks
==3602== Rerun with --leak-check=full to see details of leaked memory
==3602==
==3602== For counts of detected and suppressed errors, rerun with: -v
==3602== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 19 from 8)

RUN FINISHED; Segmentation fault; real time: 2s; user: 40ms; system: 580ms


Thank you!!
Last edited on
Could you post the program code itself, that might help us to identify the cause.
Topic archived. No new replies allowed.