one new char question

  • Thread starter Thread starter Joseph Lu
  • Start date Start date
J

Joseph Lu

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
 
Joseph said:
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?

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
 
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

Carl Daniel said:
Joseph said:
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?

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
 
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

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

Carl Daniel said:
Joseph said:
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?
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
 
Many thanks to u & Carl!

Joseph

www.fruitfruit.com said:
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

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

"Carl Daniel [VC++ MVP]"
Joseph Lu wrote:
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?
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 said:
Thanks, Carl

Please see my code as below:

in addition to other errors previously noted:
//---------------------------------------

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;

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
 
Joseph said:
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;

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);
 
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æ said:
Joseph said:
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;

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 said:
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
 
Got it, thanks Carl!

Carl Daniel said:
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
 
Back
Top