Solved

Using CString

Posted on 1998-08-07
12
556 Views
Last Modified: 2012-06-27
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.


0
Comment
Question by:moktar
  • 5
  • 2
  • 2
  • +3
12 Comments
 
LVL 5

Expert Comment

by:tuvi
ID: 1169608
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
 
LVL 22

Expert Comment

by:nietod
ID: 1169609
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
 
LVL 1

Expert Comment

by:awd
ID: 1169610
What is the exact error message?
0
 
LVL 6

Expert Comment

by:seippg
ID: 1169611
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
 
LVL 2

Expert Comment

by:bernfarr
ID: 1169612
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
 
LVL 22

Expert Comment

by:nietod
ID: 1169613
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 22

Expert Comment

by:nietod
ID: 1169614
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
 
LVL 2

Expert Comment

by:bernfarr
ID: 1169615
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
 

Author Comment

by:moktar
ID: 1169616
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
 
LVL 22

Expert Comment

by:nietod
ID: 1169617
>>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
 

Author Comment

by:moktar
ID: 1169618
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
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 1169619
Thankyou.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now