Re: [Dev-C++] getting unexpected results
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Damien <g_...@ho...> - 2003-07-30 15:11:24
|
<html><div style='background-color:'><P align=left>Hello Dawood,</P>
<P align=left>Firstly, that 'char *buf' declaration: allocate some memory to it using <STRONG>new</STRONG> or <STRONG>malloc. </STRONG>Passing such a pointer to <STRONG>cin is dangerous</STRONG>( it crashed on my WinXP). The reason why it won't work how you would like it is because str_ex[0] and str_ex[1] point to the <STRONG>same</STRONG> memory address (at buf), so if buf is changed, the other pointers that point to buf will change as well(str_ex[0] and str_ex[2]). I suggest giving the cstr member of each structure its own memory space, and copy the buf into each as seen below.</P>
<P align=left>#include <iostream.h> <BR>#include <stdlib.h> <BR>#include <string.h><BR>int main() <BR>{ <BR> struct str_misc { <BR> char* cstr; <BR> }; <BR> str_misc str_ex[2]; <BR> char* buf = new char[50];</P>
<P align=left> cin>>buf; <BR> str_ex[0].cstr = new char[50];<BR> <BR> strncpy(str_ex[0].cstr,buf,49);<BR> <BR> cout<<str_ex[0].cstr<<"index=0"<<'\n'; </P>
<P align=left> cin>>buf; <BR> <BR> str_ex[1].cstr = new char[50]; <BR> <BR> strncpy(str_ex[1].cstr,buf,49);<BR> <BR> cout<<str_ex[1].cstr<<"index=1"<<'\n'; </P>
<P align=left> cout<<str_ex[0].cstr<<" repeat index=0"<<'\n'; <BR> cout<<str_ex[1].cstr<<" repeat index=1"<<'\n'; <BR> delete str_ex[0].cstr;<BR> delete str_ex[1].cstr;<BR> system("PAUSE");<BR> return 0; <BR>} <BR><BR></P>
<DIV></DIV>----Original Message Follows----
<DIV></DIV>From: dawood <DA...@KU...>
<DIV></DIV>To: Dev C++ <DEV...@LI...>
<DIV></DIV>Subject: [Dev-C++] getting unexpected results
<DIV></DIV>Date: Tue, 22 Jul 2003 19:50:51 +0800
<DIV></DIV>
<DIV></DIV>Can anybody let me know, why the below code is not working correctly.
<DIV></DIV>
<DIV></DIV>#include <IOSTREAM.H>
<DIV></DIV>#include <STDLIB.H>
<DIV></DIV>
<DIV></DIV>int main()
<DIV></DIV>{
<DIV></DIV>struct str_misc {
<DIV></DIV>char* cstr;
<DIV></DIV>};
<DIV></DIV>str_misc str_ex[12];
<DIV></DIV>char* buf;
<DIV></DIV>
<DIV></DIV>cin>>buf;
<DIV></DIV>str_ex[0].cstr= buf;
<DIV></DIV>cout<<STR_EX[0].CSTR<<"INDEX=0"<<'\N'; DIV <>
<DIV></DIV>cin>>buf;
<DIV></DIV>str_ex[1].cstr= buf;
<DIV></DIV>cout<<STR_EX[1].CSTR<<"INDEX=1"<<'\N'; DIV <>
<DIV></DIV>cout<<STR_EX[0].CSTR<<" DIV < index="0"<<'\n';" repeat> cout<<STR_EX[1].CSTR<<" DIV < index="1"<<'\n';" repeat>
<DIV></DIV>system("PAUSE");
<DIV></DIV>return 0;
<DIV></DIV>}
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>-------------------------------------------------------
<DIV></DIV>This SF.Net email sponsored by: Free pre-built ASP.NET sites including
<DIV></DIV>Data Reports, E-commerce, Portals, and Forums are available now.
<DIV></DIV>Download today and enter to win an XBOX or Visual Studio .NET.
<DIV></DIV>http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01
<DIV></DIV>_______________________________________________
<DIV></DIV>Dev-cpp-users mailing list
<DIV></DIV>Dev...@li...
<DIV></DIV>TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
<DIV></DIV>https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
<DIV></DIV></div><br clear=all><hr>Add photos to your messages with <a href="http://g.msn.com/8HMCENUS/2749??PS=">MSN 8. </a> Get 2 months FREE*.</html>
|