473,396 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

one new char question

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
Jul 18 '06 #1
9 1060
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
Jul 18 '06 #2
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]" <cp*****************************@mvps.org.nospam >
дÈëÏûÏ¢ news:eE**************@TK2MSFTNGP03.phx.gbl...
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


Jul 18 '06 #3
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]" <cp*****************************@mvps.org.nospam >
дÈëÏûÏ¢ news:eE**************@TK2MSFTNGP03.phx.gbl...
>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


Jul 18 '06 #4
Many thanks to u & Carl!

Joseph

"www.fruitfruit.com" <no******@fruitfruit.comдÈëÏûÏ¢
news:uE****************@TK2MSFTNGP05.phx.gbl...
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]"
<cp*****************************@mvps.org.nospa m>
дÈëÏûÏ¢ news:eE**************@TK2MSFTNGP03.phx.gbl...
>>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



Jul 18 '06 #5
Joseph Lu wrote:
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
Jul 18 '06 #6
Joseph Lu wrote:
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);
Jul 19 '06 #7
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æ" <ma*@RnEeMtOsVeEt.co.yu????
news:OC**************@TK2MSFTNGP03.phx.gbl...
Joseph Lu wrote:
>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);

Jul 20 '06 #8
"Joseph Lu" <jo******@yahoo.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
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
Jul 20 '06 #9
Got it, thanks Carl!

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
дÈëÏûÏ¢ news:uZ**************@TK2MSFTNGP03.phx.gbl...
"Joseph Lu" <jo******@yahoo.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>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


Jul 20 '06 #10

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

Similar topics

2
by: Nicholas Parnell | last post by:
Hi! I have the problem where I cast a bit string of "10000000" to a byte, which i get -128; this is fine so far. However, when I take this byte and cast it to a char, I get a question mark('?')....
8
by: Ekim | last post by:
my question is as follows: I've got a DLL in which I have a method GetBuffer (this one is extern, exported, is called from outside this program) which shall pass a char-buffer to the...
19
by: Jasper Dozer | last post by:
Is this a healthy way to get a pointer to point ? char *p = "longenough"; regards, jasper
1
by: b83503104 | last post by:
When are they not consistent?
42
by: S S | last post by:
Hi Everyone I have const char *p = "Hello"; So, here memory is not allocated by C++ compiler for p and hence I cannot access p to modify the contents to "Kello" p = 'K'; // error at runtime
7
by: owolablo | last post by:
Can anybody please tell me how to change the individual elements of a char variable. I need to parse through the string, check for a particular character and change it to something else if it is...
9
by: happyvalley | last post by:
I just wonder how to pass arguments to this function with a char** void oldmain(int argv, char**argc) { ........ } void main(void) { int argv;
12
by: karthikbalaguru | last post by:
Hi, How is 'Int' Faster than 'Char' ? I think , 'Char' is small and so it should be easily & efficiently . Can someone here provide some info regarding this. Thanks and Regards, Karthik...
43
by: emyl | last post by:
Hi all, here's an elementary question. Assume I have declared two variables, char *a, **b; I can then give a value to a like a="hello world";
16
by: MN | last post by:
I have a question : How to understand the mean of char** type ?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.