Thread: Error: C2664 : sprintf_s() cannot convert

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    20

    Error: C2664 : sprintf_s() cannot convert

    i think i've fixed it, i'll repost with correct question in a sec

    here was my question:

    Ok, i'm on VS2005 and i've got this line of code:


    sprintf_s(RandomData,client[cNum].Name,":%s!u@h PART #Lob_18_0\r\n\0");

    that i'm trying to get into this buffer:

    char * RandomData;
    RandomVar=(22+client[cNum].nicklen);
    RandomData= new char[RandomVar];

    Here is the error:

    int sprintf_s(char *,size_t,const char *,...)' : cannot convert parameter 2 from 'char *' to 'size_t'


    I'm thinking its because it has to do with the dynamic memory allocation.

    Thanks for any help!!!
    ================

    And right after posting I relized my mistake:

    The solution, as blindingly easy as it may seem, just for any other newbies in the same spot:

    sprintf_s(RandomData,client[cNum].nicklen,":%s!u@h PART #Lob_18_0\r\n\0",client[cNum].Name);

    *sigh*... I need sleep.
    Last edited by klipseracer; 03-22-2008 at 05:27 PM.

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    20
    Ok, so now i have a new problem. This code doesn't work:
    Code:
    RandomVar=(22+client[cNum].nicklen);
    RandomData= new char[RandomVar];
    sprintf_s(RandomData,RandomVar,":%s!u@h PART #Lob_18_0\r\n",client[cNum].Name);
    Any idea? It gives me some error in a debug source file which is obviously not something i coded. I have no clue how to debug something that doesn't give me an error, I just know it breaks on sprintf_s() line.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    It gives me some error
    And you hope to receive some answer?

    Here is it - exactly same level of informativness as your question
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    20
    Quote Originally Posted by vart View Post
    And you hope to receive some answer?

    Here is it - exactly same level of informativness as your question
    More than anything i was asking about syntax correctness...

    In any case, it gives me a 'Buffer too small',0 Error in the window that pops up, then when JIT catches it it opens up dbgrptt.c and throws a break at:


    Code:
    _CRTIMP void _cdecl _CrtDbgBreak(
        void
        )
    {
        DebugBreak();         <- Right here
    }
    And then after I hit continue, it has another in dbghook.c at:

    Code:
    __declspec(noinline)
    void __cdecl _CRT_DEBUGGER_HOOK(int _Reserved)
    {                                   <- Right Here
        /* assign 0 to _debugger_hook_dummy so that the function is not folded in retail */
        (_Reserved);
        _debugger_hook_dummy = 0;
    }

    After I hit continue again, the debugging stops.


    This is what the window says:

    Debug Assertion Failed!

    Program: ...
    File: f:\sp\vctools\crt_bldself_x86\crt\src\vsprintf.c
    Line: 244

    Expression: ("Buffer too small",0)

    For information on how your program can cause an assertion failure, see the Visual C++ documentation on assets.
    Last edited by klipseracer; 03-23-2008 at 12:15 AM.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Buffer too small error is very straight-forward - you allocated buffer is not big enough to store the resulting string

    you should check that client[cNum].nicklen really represents the length of the client[cNum].Name
    and counting your characters in the format - do you allocate space for nul-character?

    as a side question - this is C++ - why you do not use std::stringstream object?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    20
    Quote Originally Posted by vart View Post
    Buffer too small error is very straight-forward - you allocated buffer is not big enough to store the resulting string

    you should check that client[cNum].nicklen really represents the length of the client[cNum].Name
    and counting your characters in the format - do you allocate space for nul-character?

    as a side question - this is C++ - why you do not use std::stringstream object?
    Hell, you should see the way I currently have it set up. its done all manually. I'll definately look into stringstream. Put it this way, I never took any coding classes, and i'm dependent on using namespace std. What does that tell ya? Thanks for the help! Btw, I already tried to make the buffer about 10 times larger than it should need to be and it did the same thing. I'll check this out and report back.


    EDIT:

    Well, I made my buffer:

    RandomData= new char[2000]; and it still says the same thing... I don't see why, but either way i'm very new to stringstream and if someone could give me an easy out and give me an example of what stringstream functions i'd use to accomplish what i'm trying to do here?

    The thing is i'm always dealing with dynamic buffers, its never just a constant stream. Some of the data is constant, but the name lengths and the commands that i'm sending/recieving with my server always change. Basically its an IRC client but for another purpose.

    Basically, what i'm doing here is constructing the outgoing packet.
    This is the code that i'm using that works but I know there has got to be a much better way. And it would be a good thing to notice that I create the dynamic buffer exactly the same here and it works fine when I sort through it manually.


    Code:
    //make buffer the size of the static data(22) plus the size of the user's nickname
    RandomData= new char[22+client[cNum].nicklen];
    RandomVar=0;
    RandomData[RandomVar]=0x3a; //insert a ':'
    RandomVar++;
    
    //insert the user's nickname into the buffer
    for(int x=0;x<(client[cNum].nicklen);x++)
    {
    RandomData[RandomVar]=client[cNum].Name[x];
    RandomVar++;
    }
    
    //insert the static data
    for(int x=0;x<21;x++)
    {
    RandomData[RandomVar]=partreply[x];
    RandomVar++;
    }
    
    //send to everyone so they all know who left the lobby
    for(int x=0;x<MAX_CONNECTIONS;x++)
    {
    if(client[x].s>0)  // 's' is the client socket
    send(client[x].s,RandomData,RandomVar,0);
    }
    And basically, this is what would get sent out to all the clients:

    ":lytar!u@h PART #Lob_18_0\r\n"

    If anyone can help me out here, or let me know what I SHOULD be doing, i'd appreciate it. I have my own little crappy way of coding sometimes. But anyhow, i'd really appreciate a little direction here since this is probably one of the simpler packets I have to construct, I have some terribly confusing ones like:

    Code:
    //add the names
    for(int y=0;y<(client[ConnectedList[xx]].nicklen);y++)
    {	
    RandomData[RandomVar]=client[ConnectedList[xx]].Name[y];
    RandomVar++;
    }
    After you access the same struct more than twice it starts to get mind boggling. And I may do this two or three times to create one packet. The reason I use a struct is to easily keep track of and access multiple client's data. I'm sure there is lots of optimization I can do, but hell. There has got to be an easier way. TIA.
    Last edited by klipseracer; 03-23-2008 at 11:20 PM.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you could use std::string

    Code:
    	std::string RandomData(":");
    	RandomData += client[x].Name;
    	RandomData += "!u@h PART #Lob_18_0\r\n";
    	send(client[x].s,RandomData.c_str(),RandomData.length(),0);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    20
    THANK YOU SO MUCH!!!!!!!!!!!!!!!!

    I will try this later.

    /bowdown

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You are lying to sprintf_s about the size of your buffer, that's why.
    sprintf_s takes three arguments: buffer, size of buffer, format string.
    Yet you pass some weird stuff into the size argument. You must pass the exact size of your buffer.

    This is so you know, using std::string is far better in this case.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    20
    Quote Originally Posted by Elysia View Post
    You are lying to sprintf_s about the size of your buffer, that's why.
    sprintf_s takes three arguments: buffer, size of buffer, format string.
    Yet you pass some weird stuff into the size argument. You must pass the exact size of your buffer.

    This is so you know, using std::string is far better in this case.
    Well, not to beat a dead horse, but I still don't understand where I went wrong. I pass the size of the buffer.. Or does it need the sizeof()??? I'm lost

    this is the code that i've been trying to use but doesn't work:

    Code:
    RandomVar=(22+client[cNum].nicklen);
    RandomData= new char[RandomVar];
    
    
    sprintf_s(RandomData,RandomVar,":%s!u@h PART #Lob_18_0\r\n",client[cNum].Name);
    As you can tell I use the size that I dynamically created the buffer with.

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you add 22 chars to the string - so buffer should be 23 chrs longer than the original string - you need place for nul-char, as I said already twice...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    20
    I understand now, I tried to make it 2000, which is way too big, and if sprintf_s needs the size to be exact, then that makes sense, In any case i'm now making great use of std::string!! Btw, how long does std::string RandomData stay alive? when does it become un 'referencable' ? and i'm only assuming that I don't need to delete it since its not like regular memory. Correct me if i'm wrong here. Thanks a bunch!

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    as any variable - it is alive until you left the scope where it is defined
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    Registered User
    Join Date
    Jan 2008
    Posts
    20
    Sorry about the crap questions, I can tell I need to brush up on my strings.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by klipseracer View Post
    I understand now, I tried to make it 2000, which is way too big, and if sprintf_s needs the size to be exact, then that makes sense, In any case i'm now making great use of std::string!! Btw, how long does std::string RandomData stay alive? when does it become un 'referencable' ? and i'm only assuming that I don't need to delete it since its not like regular memory. Correct me if i'm wrong here. Thanks a bunch!
    Not true. The buffer can be any size, as long as it's big enough. IF you pass the correct size to sprintf_s, it will perform just fine and bring up an assert if the buffer isn't big enough.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Convert to title case
    By gertie in forum C Programming
    Replies: 18
    Last Post: 07-06-2008, 10:58 PM
  3. Replies: 3
    Last Post: 08-21-2006, 06:42 AM
  4. Convert Char to Int Function
    By drdroid in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2003, 12:53 PM
  5. please help ... to convert date to string
    By mel in forum C Programming
    Replies: 1
    Last Post: 06-12-2002, 10:26 AM