Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / July 2006

Tip: Looking for answers? Try searching our database.

one new char question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joseph Lu - 18 Jul 2006 03:57 GMT
Hi, all
   I use the following codes to create a char array with only three
elements,
/------------------------
char *strHead = new char[0];

 bytesin=(DWORD)strlen(strHead)
/---------------------------

  but why the values of bytesin is 24, why the value is not zero?

Could any body tell me why?
Thanks in advance!

Joseph
Carl Daniel [VC++ MVP] - 18 Jul 2006 04:13 GMT
> Hi, all
>    I use the following codes to create a char array with only three
[quoted text clipped - 8 lines]
>
> Could any body tell me why?

Undefined behavior.  You've allocated an array of 0 bytes and then called a
function that searches for the first 0-valued byte in that array -
immediately over-indexing the array and reading whatever garbage lies beyond
the array in memory.

What were you trying to do?  If you want an array of 3 characters, allocate
an array of 3 characters.  But even then, you can't just apply strlen to a
freshly allocated array - the newly allocated array has undefined content
(although under Windows it will frequently contain 0's).  In order to have a
defined result, you need to initialize the contents of the array before
using a function like strlen on it.

-cd
Joseph Lu - 18 Jul 2006 06:03 GMT
Thanks, Carl

Please see my code as below:

//---------------------------------------

1:  CString s;
2:  int i_num=2;
3:  s.Format("%d",i_num);
4:  char *strHead = new char[3];
5:  strHead ="XX";
6:  strcat(strHead,s.GetBuffer());
7:  bytesin= (DWORD)strlen(strHead);
8:  WriteFile(hReadFile, strHead,bytesin,&bytesout,NULL);
9:  delete strHead;
//------------------------------------

When I execute line 6 and 9, it will turn out an unprocessed exception like
the following format:
   ReadDatFiles.exe in 0x10215657 (msvcr71d.dll) unprocessed exception:
0xC0000005: writing location 0x0043209e access confliction.

Why?

Thanks in advance
Joseph

>> Hi, all
>>    I use the following codes to create a char array with only three
[quoted text clipped - 22 lines]
>
> -cd
www.fruitfruit.com - 18 Jul 2006 06:35 GMT
error 1: strHead ="XX";
correction:  strcpy(strHead, "XX");

error 2: strcat(strHead,s.GetBuffer());
correction: char *strHead = new char[3 + 1];
please note that you need a char to store the "\0".

> Thanks, Carl
>
[quoted text clipped - 48 lines]
>>
>> -cd
Joseph Lu - 18 Jul 2006 06:50 GMT
Many thanks to u & Carl!

Joseph

> error 1: strHead ="XX";
> correction:  strcpy(strHead, "XX");
[quoted text clipped - 59 lines]
>>>
>>> -cd
Carl Daniel [VC++ MVP] - 18 Jul 2006 16:18 GMT
> Thanks, Carl
>
> Please see my code as below:

in addition to other errors previously noted:

> //---------------------------------------
>
[quoted text clipped - 7 lines]
> 8:  WriteFile(hReadFile, strHead,bytesin,&bytesout,NULL);
> 9:  delete strHead;

delete [] strHead;

But why mix in a char array when you're already using CString?

Better yet, why not use the C++ standard string class?

-cd
Mihajlo Cvetanović - 19 Jul 2006 09:53 GMT
> 1:  CString s;
> 2:  int i_num=2;
[quoted text clipped - 5 lines]
> 8:  WriteFile(hReadFile, strHead,bytesin,&bytesout,NULL);
> 9:  delete strHead;

You want to write "XX2" to a file?

CString s;
int i_num=2;
s.Format("%s%d", "XX", i_num);
WriteFile(hReadFile, s, s.GetLength(), &bytesout, NULL);
Joseph Lu - 20 Jul 2006 01:28 GMT
Yes, thanks Mihajlo!

My new question is : when I use the following questions to write to a file,
it will replace the string already in that file,  but I do want to insert a
string to that file , not just write and replace. Could any body tell me the
best solution, thanks in advance!

Joseph

"Mihajlo Cvetanoviæ" <mac@RnEeMtOsVeEt.co.yu> ????
news:OCU6tBxqGHA.1852@TK2MSFTNGP03.phx.gbl...
>> 1:  CString s;
>> 2:  int i_num=2;
[quoted text clipped - 12 lines]
> s.Format("%s%d", "XX", i_num);
> WriteFile(hReadFile, s, s.GetLength(), &bytesout, NULL);
Carl Daniel [VC++ MVP] - 20 Jul 2006 02:17 GMT
> Yes, thanks Mihajlo!
>
> My new question is : when I use the following questions to write to a
> file, it will replace the string already in that file,  but I do want to
> insert a string to that file , not just write and replace. Could any body
> tell me the best solution, thanks in advance!

You can't insert into a file.  You can only append to the end, or overwrite.

To append, you just need to seek to the end of the file before writing to it
(see SetFilePointer if you're using WriteFile to write).

If you truly need to insert other than at the end, then you need to re-write
the entire file (or at least all of it that comes after the starting point
of your insertion).  Typically, editing programs (e.g. notepad) that need to
support insert/delete write an entirely new file every time you click
"Save".

-cd
Joseph Lu - 20 Jul 2006 02:28 GMT
Got it, thanks Carl!

>> Yes, thanks Mihajlo!
>>
[quoted text clipped - 16 lines]
>
> -cd

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.