sprintf completely stopping program

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 2070 Views - Last Post: 16 January 2012 - 05:10 PM Rate Topic: -----

#1 kevindckr  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 105
  • Joined: 09-January 12

sprintf completely stopping program

Posted 16 January 2012 - 08:52 AM

Microsoft Visual Studio gives me this message "Unhandled exception at 0x0104ee0d in AfMaster.exe: 0xC0000005: Access violation reading location 0x00000004." When I call sprintf like so
LPTSTR CLinearDim::Format(LPTSTR outStr, double inNum)
{
	if (DispModeInch())
		sprintf(outStr, sizeof(outStr), "%0.*f", InchPrec(), inNum); // This line
	else
		sprintf(outStr, sizeof(outStr), "%0.*f", MetricPrec(), inNum*INCH_MM_D);
	return outStr;
}



If I look at my stack frame it takes me all the way to here in sprintf.c

#endif  /* _COUNT_ */
        outfile->_flag = _IOWRT|_IOSTRG;
        outfile->_ptr = outfile->_base = string;

        retval = _output_l(outfile,format,NULL,arglist); // To here

        if (string == NULL)
            return(retval);



It goes one deeper to here in output.c

#endif  /* POSITIONAL_PARAMETERS */

    /* main loop -- loop while format character exist and no I/O errors */
    while ((ch = *format++) != _T('\0') && charsout >= 0) {                // To here
#ifndef FORMAT_VALIDATIONS




So if I look around I find that in sprintf.c it finds "format" to equal a bad ptr withg a value of 0x00000004. This is why I am stuck, I think this is impossible because I am sending the format string to the function as a literal string for the argument not even passing it as a variable.

This takes me to the same point no matter if I use sprintf_s or sprintf. I am not really sure where to go from here...


Thanks in advance to anyone

This post has been edited by Atli: 16 January 2012 - 08:59 AM


Is This A Good Question/Topic? 0
  • +

Replies To: sprintf completely stopping program

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4017
  • View blog
  • Posts: 12,400
  • Joined: 25-December 09

Re: sprintf completely stopping program

Posted 16 January 2012 - 09:08 AM

Look closely at the parameters you are providing sprintf(), the first parameter should be a string that is large enough to hold your "formatted" string. The second parameter should be the format string, then there should be a parameter for each format specifier specified in your second argument. Is your second argument the format specifier string?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: sprintf completely stopping program

Posted 16 January 2012 - 09:09 AM

There's no size parameter in sprintf. The arguments are (char *dest, const char *format, ... ) where ... represents the data required by the format specifications.
Was This Post Helpful? 0
  • +
  • -

#4 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: sprintf completely stopping program

Posted 16 January 2012 - 09:13 AM

Look up the declaration of sprintf again, especially that sizeof(outStr); I guess you got that from sprintf_s. Also verify if InchPrec() returns something sensible.
Was This Post Helpful? 0
  • +
  • -

#5 kevindckr  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 105
  • Joined: 09-January 12

Re: sprintf completely stopping program

Posted 16 January 2012 - 09:22 AM

This compiles with no problem
LPTSTR CLinearDim::Format(LPTSTR outStr, double inNum)
{
	if (DispModeInch())
		sprintf(outStr, sizeof(outStr), "%0.*f", InchPrec(), inNum);
	else
		sprintf(outStr, sizeof(outStr), "%0.*f", MetricPrec(), inNum*INCH_MM_D);
	return outStr;
}



If I change it to how Jim and skin suggest I get compiler errors saying

1>AfDataTypes.cpp
1>.\AfDataTypes.cpp(175) : error C2664: 'int sprintf(char *,size_t,const char *,...)' : cannot convert parameter 2 from 'const char [6]' to 'size_t'
1>        There is no context in which this conversion is possible
1>.\AfDataTypes.cpp(177) : error C2664: 'int sprintf(char *,size_t,const char *,...)' : cannot convert parameter 2 from 'const char [6]' to 'size_t'
1>        There is no context in which this conversion is possible



LPTSTR CLinearDim::Format(LPTSTR outStr, double inNum)
{
	if (DispModeInch())
		sprintf(outStr, "%0.*f", InchPrec(), inNum);
	else
		sprintf(outStr, "%0.*f", MetricPrec(), inNum*INCH_MM_D);
	return outStr;
}




When I place my mouse over the function it gives me the two options for arguments n such

// sprintf
int sprintf(char * _DstBuf, size_t_SizeInBytes, const char * _Format, ...)

int sprintf<size_t_Size>(char(&) _Dest[_Size], const char * _Format, ...)

// sprintf_s
int sprintf_s(char * _Dest, char * _Format, ...)



So I don't think it is in the way that I am calling it.

Here is the class definition. InchPrec returns 3
class CLinearDim : public CAfData
	{  
public:
	BOOL Convert(LPCTSTR inStr);
	double ConvertBeforeStoring(LPCTSTR inStr);
	LPTSTR Format(LPTSTR outStr);
	LPTSTR Format(LPTSTR outStr, double inStr);
protected:
	virtual int MetricPrec()  { return  2;}	   // default precisions
	virtual int InchPrec()	 { return  3; }
	};


Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4017
  • View blog
  • Posts: 12,400
  • Joined: 25-December 09

Re: sprintf completely stopping program

Posted 16 January 2012 - 09:29 AM

Look closely at the arguments to sprintf().
int sprintf ( char * str, const char * format, ... );


Does the second parameter look like a type size_t? Do not confuse the standard function sprintf() with the sprintf_s() function.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 kevindckr  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 105
  • Joined: 09-January 12

Re: sprintf completely stopping program

Posted 16 January 2012 - 09:34 AM

But what I am saying is that that is not the argument list it accepts. Or atleast what MVS tells me it accepts.

This is what MVS tells me about sprintf and sprintf_s. On my screen if I place my mouse over the sprintf call these are the two options I have, and if I change that sprintf to sprintf_s that is the only option it gives me (below in code tag). I do not have the option of sprintf(string, format, ...)

// sprintf
int sprintf(char * _DstBuf, size_t_SizeInBytes, const char * _Format, ...)
	 
int sprintf<size_t_Size>(char(&) _Dest[_Size], const char * _Format, ...)
	 
// sprintf_s
int sprintf_s(char * _Dest, char * _Format, ...)


Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 4017
  • View blog
  • Posts: 12,400
  • Joined: 25-December 09

Re: sprintf completely stopping program

Posted 16 January 2012 - 09:45 AM

If you are compiling this program as Unicode then you may need to use swprintf()

Jim
Was This Post Helpful? 0
  • +
  • -

#9 kevindckr  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 105
  • Joined: 09-January 12

Re: sprintf completely stopping program

Posted 16 January 2012 - 09:58 AM

I do not think I am compiling in UNICODE... How might I tell in MVS? And because I am pretty sure I'm not. Why would my sprintf take different arguments than are defined on the C++ support pages?
Was This Post Helpful? 0
  • +
  • -

#10 kevindckr  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 105
  • Joined: 09-January 12

Re: sprintf completely stopping program

Posted 16 January 2012 - 10:03 AM

I highly doubt I am using unicode because I have never done anything seen here.
Was This Post Helpful? 0
  • +
  • -

#11 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: sprintf completely stopping program

Posted 16 January 2012 - 10:09 AM

It seems bizarre. Your MSVS tooltip is showing for sprintf exactly what mine shows for sprintf_s.

What does your file #include?

This post has been edited by r.stiltskin: 16 January 2012 - 10:13 AM

Was This Post Helpful? 0
  • +
  • -

#12 kevindckr  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 105
  • Joined: 09-January 12

Re: sprintf completely stopping program

Posted 16 January 2012 - 10:13 AM

Its a massive program.

#include "stdafx.h"
#include "afmaster.h"
#include "AfDataTypes.h"
#include <strstream>
#include "mainfrm.h"
#include <cmath>
#include "cfgdata.h"
#include "CadRes.h"


Was This Post Helpful? 0
  • +
  • -

#13 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: sprintf completely stopping program

Posted 16 January 2012 - 10:17 AM

Maybe there's something in one of those headers that redefines sprintf_s to sprintf.

Why don't you try adding
#include <cstdio>
and see if that throws a redefinition error?
Was This Post Helpful? 0
  • +
  • -

#14 kevindckr  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 105
  • Joined: 09-January 12

Re: sprintf completely stopping program

Posted 16 January 2012 - 10:27 AM

Including that did nothing. This is what I have now.

I am still searching but I came to the MSDN page for sprintf and swprintf then read a little and saw the link for the _snprintf function, which does not create the problem at all. So I plan to change every sprintf call I have to _snprintf and see where that gets me.
Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg  Icon User is offline

  • member icon


Reputation: 4017
  • View blog
  • Posts: 12,400
  • Joined: 25-December 09

Re: sprintf completely stopping program

Posted 16 January 2012 - 10:32 AM

Another option would be to convert your C-string to a std::string and then use stringstream class to format this string, or possibly the boost format library.

Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2