|
|
Crash in string::assign
Here is the code
#include <vector>
#include <string>
using namespace std;
struct Pkt{
int i;
string s;
};
int main()
{
vector<Pkt> v;
v.reserve(1);
v[0].s.assign("pankaj");
}
It is crashing on the last line and I can't figure out why. From what
I understand v.reserve(1) creates a default constructed struct and
pushes it in the vector. assign() then tries to modify the default-
constructed string inside that struct, but I can't think of a reason
for this to crash. Please help
{ v.reserve(1) doesn't construct v[0], v.size() still remains zero,
and v[0] is a dangling reference. v.resize(1) does construct v[0].
-mod/sk }
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
dragoncoder
|
9/21/2010 10:34:30 PM |
|
On 22 Sep., 06:34, dragoncoder <pktiw...@gmail.com> wrote:
> Here is the code
>
> #include <vector>
> #include <string>
>
> using namespace std;
>
> struct Pkt{
> int i;
> string s;
>
> };
>
> int main()
> {
> vector<Pkt> v;
> v.reserve(1);
> v[0].s.assign("pankaj");
>
> }
>
> It is crashing on the last line and I can't figure out why. From what
> I understand v.reserve(1) creates a default constructed struct and
> pushes it in the vector.
This misconception is the source of the wrong code. The
reserve function does guarantee that sufficient memory will
be allocated at this point to be able to hold the corresponding
number of container elements. But this is just does *not*
change the size of the vector nor does it allow to access
any elements beyond the current size (which is still 0 at
the point above). A precondition of invoking the operator[]
overload in std::vector is that the index is less than size(),
which is not satisfied above.
> assign() then tries to modify the default-
> constructed string inside that struct, but I can't think of a reason
> for this to crash. Please help
If you wanted to *resize* the vector, just call the member function
resize instead of reserve. You could also construct the vector
from begin with with a given size as in
vector<Pkt> v(1);
This will also ensure that the container has now one value-initialized
element, i.e. a size of 1.
HTH & Greetings from Bremen,
Daniel Kr�gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
ISO
|
9/23/2010 3:41:05 AM
|
|
|
1 Replies
434 Views
(page loaded in 0.057 seconds)
Similiar Articles: Join two lines in pattern matching, search, then assign fields to ...Perl: How do I match a string and also assign the match to another ..... input and try to match a regular expression (ONE|TWO). and then assign ... was how to match ay ... Some text processing questions - comp.lang.vhdlIf I say: vsr msg := string(1 to 5); Then I can assign "hello" to it (length 5) but not "bye". ... all to a function - or even enquiring about L'length - will crash the ... Extract Program change number formula - comp.music.midi... not call any Windows functions in this, e.g. assignment of ... > > It is no wonder that this sub might crash from time to ... Even the > string operations are not allowed because ... Invisible window, like console app - comp.os.ms-windows.programmer ...... command-line version that could take a macro string >as ... handles to them; it'll do nothing at best and crash ... to >> use a message queue. =A0But could I somehow assign ... How to draw rectangles - comp.soft-sys.matlabDoes it crash MATLAB? Does it start quoting Monty Python? ["It's just a flesh wound."] -- Steve Lord slord@mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http ... intent(out) for pointer dummy argument - comp.lang.fortran ...In the above, the integer k is represented by some string of bits. That ... If b is disassociated, assign_ptr1 doesn't care, while assign_ptr2 crashes at runtime ... input & output in assembly - comp.lang.asm.x86... the interface to Win32 to write a beginner's assignment ... Win32 question I hear is, "Why does my program crash?" ... functions: > > INT 21h, AH = 09h: Print String ... Java Collections List : Converting from List 'Naive Solution: List<String> names= new ArrayList ... Because we can assign probabilities to features of the ... But the former means a program crash, and the latter ... ptr versus const ptr& - comp.lang.c++.moderatedSame as other assignment operator overload: use a temporary ... The program might crash before the assertion, it might ... point problem (again) - comp.lang.asm.x86... string "-6 ... Simple code encryption (xor) problem - comp.lang.asm.x86 ...I know that I could assign some IRQs to other cores, but ... of an error are >> much more serious (app or OS crash ... because my 'applications' are token/parameter strings and ... crash - C++ Program Always Crashes While doing a std::string ...I found it weird that the application was continuously crashing on a string assign. A typical crash backtrace would look as follows: #0 0x00007f2c2663bfb5 in raise ... Crash due to SIGABRT on Linux C++ PowerPC - Stack OverflowMy program crashes in string assign. I cannot corner down the exact cause of it. Multiple threads execute the same code. This is my code. char* cTemp = new char[5 ... 7/25/2012 3:18:30 PM
|
|
|
|
|
|
|
|
|