|
From: joe on 24 Jun 2008 22:33 I am reading the "C++ Primer, 4th Edition". I compiled the following codes, // C-style character string implementation const char *pc = "a very long literal string"; const size_t len = strlen(pc +1); // space to allocate // performance test on string allocation and copy for (size_t ix = 0; ix != 1000000; ++ix) { char *pc2 = new char[len + 1]; // allocate the space strcpy(pc2, pc); // do the copy if (strcmp(pc2, pc)) // use the new string ; // do nothing delete [] pc2; // free the memory } I got an error message "Heap corruption detected: ... CRT detected that the application wrote to memory after end of heap buffer.", so i debugged it step by step and found that "delete [] pc2;" causes this error. I wonder why and how to correct it. Thanks
From: Andrew Koenig on 24 Jun 2008 22:48 "joe" <liuj0025(a)gmail.com> wrote in message news:b32b7666-5e2b-4d89-9d57-0973e6886bda(a)r37g2000prm.googlegroups.com... > const char *pc = "a very long literal string"; > const size_t len = strlen(pc +1); This looks odd. Are you sure you didn't mean const size_t len = strlen(pc) + 1; ??
From: joe on 24 Jun 2008 23:33 On Jun 25, 10:48 am, "Andrew Koenig" <a...(a)acm.org> wrote: > "joe" <liuj0...(a)gmail.com> wrote in message > > news:b32b7666-5e2b-4d89-9d57-0973e6886bda(a)r37g2000prm.googlegroups.com... > > > const char *pc = "a very long literal string"; > > const size_t len = strlen(pc +1); > > This looks odd. Are you sure you didn't mean > > const size_t len = strlen(pc) + 1; > > ?? > Thank you for your help. I copied the codes from the ebook edition, did not find this mistake. > I think i should check it carefully. > Thank you.
|
Pages: 1 Prev: cheap discount prada chanel coach puma versace wallet sneakers Next: file write |