473,498 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Newie question

I have this pice of code (I know, I should use c++ string instead of c
string):

char *p = new char[4];
strcpy(p, "Hola");
delete [] p;

With gcc I compile it and run it with no errors, but with MS Visual C++
6.0 I get an error that says:
"DAMAGE: after Normal block(#238) at [an address].

Someone can tell me wath´s wrong?

Oct 20 '06 #1
6 1336
gabitoju wrote:
char *p = new char[4];
strcpy(p, "Hola");
"Hola" is really "Hola\0", and strcpy() finds the end of the string by the
\0. (Not by the 4 on the previous line!)

However, p only points to 4 valid characters, so strcpy() writes the \0 off
the end of the array at p. This corrupts the heap, and VC++ in Debug mode
catches that, where gcc doesn't catch it.

Tip: Use std::string. ;-)

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Oct 20 '06 #2
It works!!!

Thak you

I´m using std:string for almost all string, but I use char* new[] to
use C strtok().

Phlip ha escrito:
gabitoju wrote:
char *p = new char[4];
strcpy(p, "Hola");

"Hola" is really "Hola\0", and strcpy() finds the end of the string by the
\0. (Not by the 4 on the previous line!)

However, p only points to 4 valid characters, so strcpy() writes the \0 off
the end of the array at p. This corrupts the heap, and VC++ in Debug mode
catches that, where gcc doesn't catch it.

Tip: Use std::string. ;-)

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Oct 20 '06 #3
ga******@gmail.com wrote:
It works!!!

Thak you

I´m using std:string for almost all string, but I use char* new[] to
use C strtok().
Take a look at the "find" methods for string. They are very
valuable and do not modify the string.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
Oct 20 '06 #4
gabitoju wrote:
Thak you
Please don't top-post.
I´m using std:string for almost all string, but I use char* new[] to
use C strtok().
Like someone said, use find().

Then Google for strtok, and you'll find a gadzillion reasons not to use it.
Some even apply to newbies!

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Oct 20 '06 #5
ga******@gmail.com wrote:
I have this pice of code (I know, I should use c++ string instead of c
string):

char *p = new char[4];
strcpy(p, "Hola");
delete [] p;

With gcc I compile it and run it with no errors, but with MS Visual
C++
6.0 I get an error that says:
"DAMAGE: after Normal block(#238) at [an address].

Someone can tell me wath´s wrong?
You allocate one char less than you copy. "Hola" has 5 chars in it.
You have to count the terminating '\0'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 20 '06 #6
ga******@gmail.com wrote:
I have this pice of code (I know, I should use c++ string instead of c
string):

char *p = new char[4];
strcpy(p, "Hola");
delete [] p;

With gcc I compile it and run it with no errors, but with MS Visual C++
6.0 I get an error that says:
"DAMAGE: after Normal block(#238) at [an address].

Someone can tell me wath´s wrong?
Try this safer code:
const char hola[] = "Hola";
char * p;
p = new char [sizeof(hola)];
strcpy(p, hola);
cout << p;
delete [] p;
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
Oct 21 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2860
by: Mohammed Mazid | last post by:
Can anyone please help me on how to move to the next and previous question? Here is a snippet of my code: Private Sub cmdNext_Click() End Sub Private Sub cmdPrevious_Click() showrecord
3
4994
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
2626
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
3055
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
10
3387
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
3679
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
4013
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
4689
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
4250
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
3
2536
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7205
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4590
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1419
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
291
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.