Using CString

I'm writing an application that does some work on strings

I need to have 2 CStrings that allows me to pass a parameter to a function.
These 2 strings are declared like this:

void myfunc()
{
CString VarData;
CString VarDescr;

char buf1[9];
char buf2[50];

After some manipulations like strcpy, strcat, I've:
buf1="123|456|";
buf2="\DIR1\DIR2|\DIR3|DIR4";

I strore them in the 2 CString.

I've tried 2 ways:

VarData=buf1;
VarDescr=buf2;

or

VarData.Format("%s",buf1);  
VarDescr.Format("%s",buf2);  

In either ways I've an unhandled exception when I store the datas in VarData wathever if I store in VarData or VarDescr first.

What happens?
Should I use another way to store the datas?

Thanks for a fast answer.


moktarAsked:
Who is Participating?
 
nietodConnect With a Mentor Commented:
Thankyou.
0
 
tuviCommented:
VarData=buf1;
VarDescr=buf2;

These should be correct. Did you verify buf1's actual value? Through debug maybe. Easier, just put AfxMessageBox (buf1) before the assignment to see its value.

Also, did you do anything to VarData prior to assignment?
0
 
nietodCommented:
Both ways should work fine.   Do you have NUL (0) terminators at the end of the buf1 and buf2 strings.  Without NULs to mark the end of the strings, you could get an exception.  

Can you post your code?
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
awdCommented:
What is the exact error message?
0
 
seippgCommented:
The code you posted, either method, is just fine.  Your problem lies elsewhere.  Here's a few things to check for.

1.  Make sure you haven't written past the end of an array.  You might have overwritten VarData on the stack.
2.  Is the compiler giving any warnings that you're ignoring?  
3.  Did you have any problems getting this program to link cleanly?  You might be accessing an invalid link address.  Try doing a rebuild all.
4.  Is this a UNICODE or MBCS application?  Are you using the appropriate macros if so?

Like nietod suggests, if you can post your code it would help.
0
 
bernfarrCommented:
I've had similar problems in the past with creating (empty) CStrings and then using an assignment to add values to them. Try reordering your code so that you figure out what you want to put into the CString variables and then declare them:

char buf1[9];
char buf2[50];

After some manipulations like strcpy, strcat, I've:
buf1="123|456|";
buf2="\DIR1\DIR2|\DIR3|DIR4";

CString VarData(buf1);  // or
CString VarDescr = buf2;


I never did figure out why I got the Unhandled Exceptions, but this type of approach fixed it for me. I do remember that the Unhandled Exception sometimes happened if I had a CString that was global to a function which contained a switch statement. If some case in the switch statement changed the string buffer via assignment, I would get an exception on the destructor call, I think.

0
 
nietodCommented:
That approach is unnecessary.  That does not fix your error.  It just covers it up.  It will still be lurking there and will probably come back to haunt you.
0
 
nietodCommented:
Given the evidence so far the most likely cause for an error is that the string's (buf1 and buf2) are not properly termianted.  Why?  because when two seperate and unrelated functions (the CString constructor anf Format()) attempt to use the strings they cause an exception.  Thus it indicates that the strings don't have a valid format, that is, they do not have NUL terminators.  Then when these functions access the strings, the read past what should be the end of the strings and cause an access violation.
0
 
bernfarrCommented:
I have seen similar problems as mentioned before and this approach certainly fixed them. In my case I would not have been constructing strings, so the issue of missing null terminators was not the problem (I believe).

Anyway, I'll echo previous comments and ask for the source code of the function to be posted here.
0
 
moktarAuthor Commented:
Actually, before I receive your answers, I managed to solve my problem with replacing my two arrays directly with the CString, and by building the strings with operations allowed for this type of data.

But for information, I give you the code before and after the resolution of the problem.

Before :
void myfunction(short data, CString Partner)
{
short byte[2];

char string[3];
char bufdat[9];
char path[50];      
char Nb[4];

CString VarData;
CString VarDescr;

bufdat[0]='\0';
path[0]='\0';

byte[0]=data/BASE1;
byte[1]=data-byte[0]*BASE1;


for(int j=0;j<2;j++)
      {
      
      itoa(byte[j],string,10);
      strcat(bufferdata,string);
      strcat(bufferdata,"|");
      
      strcat(path,BaseDescr1);
      itoa(byte_no,Nb,10);
      strcat(path,Nb);
      strcat(path,BaseDescr2);
      strcat(path,"|");
      byte_no++;
      }
VarDescr=path;
VarData=bufferdata; <-- The problem was here
SendPartner(Partner,0,"\0",VarDescr,VarData);

}


After:
void myfunction(short data,CString Partner)
{
short byte[4];
      
CString VarDescr;
CString VarData;
CString Byte;
CString Nb;

byte[1]=data/BASE1;
byte[0]=data-byte[1]*BASE1;

for(int j=0;j<2;j++)
      {
      
      Byte.Format("%d",byte[j]);
      VarData+=Byte;
      VarData+="|";
      
      VarDescr+=BaseDescr1;      
      Nb.Format("%d",byte_no);
      VarDescr+=Nb;
      VarDescr+=BaseDescr2;
      VarDescr+="|";
      byte_no++;
      }
SendPartner(Partner,0,"\\0",VarDescr,VarData);
}

Actually, it is also true that I haven't a null terminating character at the end of my char arrays.

But I thought that when I put the char into the CString, the things were the same as if I did:
CString a="Hello";
where there isn't the null character.

0
 
nietodCommented:
>>Actually, it is also true that I haven't a null terminating character at the end of my char arrays.
That's the problem!  

>> But I thought that when I put the char into the CString, the things were the same as if I did:
>>     CString a="Hello";
>> where there isn't the null character.
But there is a NUL there.  C allways places a NUL at the end of a string literal, thus "hello" is 6 bytes long.  There is an extra byte for the NUL.  If you try to store it in an array that is only 5 bytes long, you will get a compiler error, it knows you need 6 bytes, for example.

char Array1[5] = "Hello"; // compiler error.  No room for NUL.
char Array2[6] = "Hello"; // fine.

In your case the CString constructor and CString Format() procedures didn't know how long the strings were.  They start with the first byte and then go to the next and the next and so on untiil they come to a NUL.  Since there was none they kept going and going until they reached an invalid address.
0
 
moktarAuthor Commented:
I reopen the question in order to give the points to nietod, who really shows me what was the problem.
So let him answer to the new question.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.