CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jun 2006
    Posts
    5

    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.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: unhandled exception when using strcpy

    What is sendBuffer?
    Where and how did you declared/defined it? Did you allocate enouph memory for copying?

  3. #3
    Join Date
    Jun 2006
    Posts
    5

    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.

  4. #4
    Join Date
    Apr 1999
    Posts
    50

    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.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    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"!

  6. #6
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    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"!
    totally agree to Victor.


    You will need to use new operator or malloc to create enough memory.
    Regards,
    Ramkrishna Pawar

  7. #7
    Join Date
    Jun 2006
    Posts
    5

    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)

  8. #8
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    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

  9. #9
    Join Date
    Jun 2006
    Posts
    5

    Thumbs up 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;
    }

  10. #10
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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:
    Code:
    cout << str;
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    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!

  12. #12
    Join Date
    Jun 2006
    Posts
    5

    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:
    Code:
    cout << str;
    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!
    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
  •  





Click Here to Expand Forum to Full Width

Featured