Skip to main content
gamedev.net
🔒 Locked

sprintf error

Started by stilltjack Apr 10, 2002 at 12:16 AM 14 replies 350+ views
Original Post
stilltjack
stilltjack
I was having difficulty getting any of my programs to run on anyone elses computer(i have win98 and i was testing on windows xp), and by the process of elimination I discovered that sprintf caused the program to crash. I went into msvc++ on the computer with xp, tried writing a simple console program that used sprintf and it compiled fine but crashed when I ran it. What kind of stupid mistake am I making? thanks, dave
LessBread
LessBread
It''s hard to tell since you haven''t shared any code.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Blacksmith_Tony
Blacksmith_Tony
I had a problem that may have been similar to this one. I was using sprintf in a loop and declaring the string that was going to be written to in that loop as well. Something like:

for(......){
string tempString;
sprintf(tempString,"%f",anotherVar);
}

Taking the string tempString; line out of the loop so it was only declared once seemed to fix the problem IIRC.
cearny
cearny
quote:
Original post by Anonymous Poster
right anonymous, but at leat you can run a DOS CONSOLE on windows9x. On XP there is no such thing. SO if you try to run a console on XP it would CRASH!!!!!!!


NO DOS ON XP, got it?




You are as stupid as.... I can''t compare you with anything!!! And what exactly does the ''cmd'' command do under Windows XP? Prepare the computer for making toast bread?

Okay, it''s not a *DOS* console, it''s a 32-bit console, but then again so were the ones on Windows 9x/NT, so I don''t see the big difference.

And MSVC *can* do console apps, it *does* link to the LIBC/LIBCD/LIBCMT/LIBCMTD library, which *does* have sprintf implemented correctly. I can send you dozens of *console* apps that use sprintf and work perfectly under XP or 95 or 98 or NT4 or Me or 2000...

People like you - who clearly have no idea of what they''re talking about - annoy me a lot.
cearny
cearny
quote:
Original post by Blacksmith_Tony
I had a problem that may have been similar to this one. I was using sprintf in a loop and declaring the string that was going to be written to in that loop as well. Something like:

for(......){
string tempString;
sprintf(tempString,"%f",anotherVar);
}

Taking the string tempString; line out of the loop so it was only declared once seemed to fix the problem IIRC.



Hmm well the std::string class you''re using is NOT the same thing with a C char[...]. You *can''t* sprintf to a std::string!
You should sprintf to a char[...] buffer and the assign that buffer to the std::string class. All else works randomly (probably based on the ammount of storage allocated by the string and the particular STL implementation).

I think that the initial poster''s problem is that he''s getting a memory overwrite, anmely writing too much data to the char[...], and date gets written on unallocated memory. Or maybe you''re not sending the address of a variable to a scanf/sscanf call and instead sending the variable directly.

Hope it helps,
Adrian
Blacksmith_Tony
Blacksmith_Tony
Oh yeah my bad.

It was something like that anyway. Must have had a char[] in there as well. I''m sure that having them in the loop caused an access violation on the sprintf line, which was fixed by taking the declarations out. It seemed a little strange to me at the time.

Oh well it''s probably not that.

LessBread
LessBread
quote:
Original post by cearny
You are as stupid as.... I can''t compare you with anything!!! And what exactly does the ''cmd'' command do under Windows XP? Prepare the computer for making toast bread?

Okay, it''s not a *DOS* console, it''s a 32-bit console, but then again so were the ones on Windows 9x/NT, so I don''t see the big difference.

And MSVC *can* do console apps, it *does* link to the LIBC/LIBCD/LIBCMT/LIBCMTD library, which *does* have sprintf implemented correctly. I can send you dozens of *console* apps that use sprintf and work perfectly under XP or 95 or 98 or NT4 or Me or 2000...

People like you - who clearly have no idea of what they''re talking about - annoy me a lot.


Here, here! I second that.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
GeniX
GeniX
If you want to go the C++ method, consider the STL string:

std::string mystring;
std::string mystring2;

mystring = "Computer";
mystring2 = mystring + " mouse";

cout << mystring2.c_str();

Or you can use the older C method and sprintf to a char* - provided you have space allocated at the pointers terminating address...

--
OFFTOPIC: For the purposes of most console / DOS apps, the windows XP "cmd" command console will do fine. Using the simple sprintf and other similar functions will produce applications that run fine under XP''s ability to emulate dos.


regards,

GeniX

www.cryo-genix.net
regards,GeniXwww.cryo-genix.net
cearny
cearny
quote:
Original post by GeniX
If you want to go the C++ method, consider the STL string:

std::string mystring;
std::string mystring2;

mystring = "Computer";
mystring2 = mystring + " mouse";

cout << mystring2.c_str();

Or you can use the older C method and sprintf to a char* - provided you have space allocated at the pointers terminating address...

--
OFFTOPIC: For the purposes of most console / DOS apps, the windows XP "cmd" command console will do fine. Using the simple sprintf and other similar functions will produce applications that run fine under XP's ability to emulate dos.


regards,

GeniX

www.cryo-genix.net


Yes, you *could* sprintf to a std::string, provided that you first .resize(...) it to a confortable length and you do something like:

sprintf( const_cast(str.data()), "your pattern here %s", "!!!" );

This is the safest way you can do things (and believe me it is not very safe), since you can't know if in the STL implementation you use when compiling, the string class is defined as having the actual storage area the first variable in the class (like, it may have other defined before that, and thus if you write to the pointer to the string directly, you may end up overwriting other private data).

Hope this helps,
Adrian 'cearny' Cearnau

[edited by - cearny on April 10, 2002 9:24:05 AM]
spanish
spanish
dooby dooby doo!!
Arild Fines
Arild Fines
quote:
Original post by GeniX

cout << mystring2.c_str();


The .c_str() conversion is unnecessary. This is preferrable:

  
cout << mystring;



--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
stilltjack
stilltjack
Thanks fellows,
I finally got it.
dave

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.