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.
Re: unhandled exception when using strcpy
What is sendBuffer? :confused:
Where and how did you declared/defined it? Did you allocate enouph memory for copying?
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.
Re: unhandled exception when using strcpy
Quote:
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.
Re: unhandled exception when using strcpy
Quote:
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"! :wave: :thumbd:
Re: unhandled exception when using strcpy
Quote:
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"! :wave: :thumbd:
totally agree to Victor. :thumb:
You will need to use new operator or malloc to create enough memory.
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)
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.
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;
}
Re: unhandled exception when using strcpy
Quote:
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:
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!
Re: unhandled exception when using strcpy
Quote:
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.
Quote:
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!