473,657 Members | 2,579 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1070
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(s trHead);
8: WriteFile(hRead File, 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.ex e in 0x10215657 (msvcr71d.dll) unprocessed exception:
0xC0000005: writing location 0x0043209e access confliction.

Why?

Thanks in advance
Joseph

"Carl Daniel [VC++ MVP]" <cp************ *************** **@mvps.org.nos pam>
дÈëÏûÏ¢ news:eE******** ******@TK2MSFTN GP03.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(s trHead);
8: WriteFile(hRead File, 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.ex e in 0x10215657 (msvcr71d.dll) unprocessed exception:
0xC0000005: writing location 0x0043209e access confliction.

Why?

Thanks in advance
Joseph

"Carl Daniel [VC++ MVP]" <cp************ *************** **@mvps.org.nos pam>
дÈëÏûÏ¢ news:eE******** ******@TK2MSFTN GP03.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******@fruit fruit.comдÈëÏû Ï¢
news:uE******** ********@TK2MSF TNGP05.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(s trHead);
8: WriteFile(hRead File, 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.ex e in 0x10215657 (msvcr71d.dll) unprocessed exception:
0xC0000005: writing location 0x0043209e access confliction.

Why?

Thanks in advance
Joseph

"Carl Daniel [VC++ MVP]"
<cp*********** *************** ***@mvps.org.no spam>
дÈëÏûÏ¢ news:eE******** ******@TK2MSFTN GP03.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(s trHead);
8: WriteFile(hRead File, 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(s trHead);
8: WriteFile(hRead File, 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(hRead File, 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*@RnEeMtOsVe Et.co.yu????
news:OC******** ******@TK2MSFTN GP03.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(s trHead);
8: WriteFile(hRead File, 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(hRead File, s, s.GetLength(), &bytesout, NULL);

Jul 20 '06 #8
"Joseph Lu" <jo******@yahoo .comwrote in message
news:%2******** ********@TK2MSF TNGP03.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.nos pam>
дÈëÏûÏ¢ news:uZ******** ******@TK2MSFTN GP03.phx.gbl...
"Joseph Lu" <jo******@yahoo .comwrote in message
news:%2******** ********@TK2MSF TNGP03.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
69403
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('?'). So when i go to cast it back to a byte, i now get 63 (the ascii representation of a question mark). I see that for any byte from 128 to 256 i'll get a question mark... must be java's way of indicating that's it's negative or something.... my...
8
6174
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 calling-function for further handling. If I've confused you, here's the code snippet (it's within a simple Win32-Dll-project): char* buffer; // global buffer - this one points to my buffer (for puttiing it simple in here I assume memory is already...
19
2392
by: Jasper Dozer | last post by:
Is this a healthy way to get a pointer to point ? char *p = "longenough"; regards, jasper
1
2825
by: b83503104 | last post by:
When are they not consistent?
42
32146
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
2138
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 found. Thanks
9
2255
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
9389
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 Balaguru
43
17198
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
1640
by: MN | last post by:
I have a question : How to understand the mean of char** type ?
0
8392
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8730
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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 we have to send another system
2
1950
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.