|
-
June 1st, 2006, 05:54 AM
#1
unhandled exception when using strcpy
I am having problems with the following function:
void ConvertToString(float currentBPM, int trackPos, int nextBeatPos, int beatNum, float* range)
{
ostringstream buffer;
char* ch = "\0";
string st = "";
buffer << setprecision(3) << currentBPM << ends;
st = buffer.str();
strcpy(sendBuffer, st.c_str()); // This line is causing the app to crash!
strcat(sendBuffer, ",");
itoa(trackPos, ch, 10);
strcat(sendBuffer, ch);
strcat(sendBuffer, ",");
itoa(nextBeatPos, ch, 10);
strcat(sendBuffer, ch);
strcat(sendBuffer, ",");
itoa(beatNum, ch, 10);
strcat(sendBuffer, ch);
strcat(sendBuffer, ",");
for(int i=0; i<9; i++)
{
buffer << setprecision(3) << range[i] << ends;
st = buffer.str();
strcat(sendBuffer, st.c_str());
if(i<8)
{
strcat(sendBuffer, ",");
}
}
}
When i run in debug mode in Visual Studio 6, it runs up to the call to strcpy, then asks me to locate strcat.asm.
Am I doing something fundamentally wrong here?
Please help!
Cheers.
-
June 1st, 2006, 06:04 AM
#2
Re: unhandled exception when using strcpy
What is sendBuffer?
Where and how did you declared/defined it? Did you allocate enouph memory for copying?
-
June 1st, 2006, 06:08 AM
#3
Re: unhandled exception when using strcpy
sendBuffer is a char array. I'm trying to convert a load of floats and ints to a string in order to send the data across a network using winsock.
I declared it as char*, so there shouldn't be an issue with size.
-
June 1st, 2006, 06:18 AM
#4
Re: unhandled exception when using strcpy
 Originally Posted by no-comply
I declared it as char*, so there shouldn't be an issue with size.
Instead, you have an issue with uninitialised pointers. It would be better to declare it as std::string and use += instead of strcat.
-
June 1st, 2006, 06:18 AM
#5
Re: unhandled exception when using strcpy
 Originally Posted by no-comply
sendBuffer is a char array....
I declared it as char*, so there shouldn't be an issue with size.
If you have NOT allocated enouph memory for this "char array" then there must be and there is "an issue with size"!
-
June 1st, 2006, 06:36 AM
#6
Re: unhandled exception when using strcpy
 Originally Posted by VictorN
If you have NOT allocated enouph memory for this "char array" then there must be and there is "an issue with size"! 
totally agree to Victor.
You will need to use new operator or malloc to create enough memory.
Regards,
Ramkrishna Pawar
-
June 1st, 2006, 06:48 AM
#7
Re: unhandled exception when using strcpy
Thanks for your suggestion and quick replies.
I'm trying to use a string, which seems to work fine for concatenation, but I'm now having trouble converting the string back to a char* in order to send it with winsock!
Is there a simple way to do this?
The only answers i can find on msdn seem to relate to libraries not included with visual studio 6. Or maybe I'm not looking right. (Entirely possible)
-
June 1st, 2006, 06:50 AM
#8
Re: unhandled exception when using strcpy
string means std::string ???
You can read it by pointer use c_str() function of std::string variable, it returns const char *.
This char * will be valid untill the string varible is valid.
Regards,
Ramkrishna Pawar
-
June 1st, 2006, 07:35 AM
#9
Re: unhandled exception when using strcpy
Thanks to everyone above for all your help. I've got it working like this:
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
float currentBPM = 123.34f;
int trackPos = 900;
int nextBeatPos = 1500;
int beatNum = 1;
ostringstream buffer;
char* sendBuffer = new char[1000];
char* ch = "\0";
string st = "";
buffer << setprecision(5) << currentBPM << ",";
buffer << trackPos << ",";
buffer << nextBeatPos << ",";
buffer << beatNum << ",";
st = buffer.str();
strcpy(sendBuffer, st.c_str());
printf(sendBuffer);
return 0;
}
-
June 1st, 2006, 07:39 AM
#10
Re: unhandled exception when using strcpy
 Originally Posted by no-comply
Thanks to everyone above for all your help. I've got it working like this:
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
float currentBPM = 123.34f;
int trackPos = 900;
int nextBeatPos = 1500;
int beatNum = 1;
ostringstream buffer;
char* sendBuffer = new char[1000];
char* ch = "\0";
string st = "";
buffer << setprecision(5) << currentBPM << ",";
buffer << trackPos << ",";
buffer << nextBeatPos << ",";
buffer << beatNum << ",";
st = buffer.str();
strcpy(sendBuffer, st.c_str());
printf(sendBuffer);
return 0;
}
I really don't see the point of copying the content of std::string to a char* to use printf. First, you can simply do this:
Code:
printf(str.c_str());
and second, you can directly use std::cout:
-
June 1st, 2006, 07:41 AM
#11
Re: unhandled exception when using strcpy
It is not clear why are you creating the sendBuffer in the heap rather than on the stack, but if you do so - please, don't forget to :
Code:
delete [] sendBuffer;
before returning!
-
June 1st, 2006, 07:47 AM
#12
Re: unhandled exception when using strcpy
 Originally Posted by cilu
I really don't see the point of copying the content of std::string to a char* to use printf. First, you can simply do this:
Code:
printf(str.c_str());
and second, you can directly use std::cout:
The only reason I need to convert is because I need to send the data using sockets, and the variable I'm sending is called sendBuffer. The console app was just a way to get the function working so I can transfer the working method to my network program.
 Originally Posted by VictorN
It is not clear why are you creating the sendBuffer in the heap rather than on the stack, but if you do so - please, don't forget to :
Code:
delete [] sendBuffer;
before returning!
Thanks. Will do.
Again. Thanks for all your help peeps. You might just have saved my life!
Last edited by no-comply; June 1st, 2006 at 09:51 AM.
Reason: mis-named quotee
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|