Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello ,

We have the following code in our C++ layer.... we are using the DLL of this code as a COM component.. but the below line of throwing the below error? Would someone please help me on this?

Code:

sprintf(temp,"%02d%02d%04d",month,day,year);

here is the declaratio of temp object: char * temp;

Error Message:

Unhandled exception has occred in your application.If you click continue, the application will ignore this error and attempt to continue. If you click Quit, the application will close immediately.

Arithmetic operation resulted in an overflow.

Would you please let me know the equalent mehtod i can use to resolve the issue.?
Posted

1 solution

You have to allocate space for the storage of the temp variable. In your case you should declare it either as an array of characters or do dynamic memory allocation to do this (add one character for the terminating null):

1) Array of characters -

char temp[9];

2) Dynamic memory allocation:

a) malloc - char *temp = malloc(9);
To avoid memory leaks when done with temp, use: free(temp);

b) new - char *temp = new char[9];
To avoid memory leaks when done with temp, use: delete[] temp;
 
Share this answer
 
Comments
nv3 18-Jul-13 13:53pm    
My 5, David.
Ravindranath.net 18-Jul-13 13:59pm    
I read that sprintf usually takes an educated guess as to the new buffer size,i mean the memory size? isn't true?
[no name] 18-Jul-13 23:08pm    
If true then sprintf would have to return a buffer which it doesn't. The answer is correct.
Ravindranath.net 19-Jul-13 11:49am    
I am really in a big trouble... this is how i am allocating the memory to character pointer...

temp = (char *)calloc(8, sizeof(char));

and this is how i am assinging the value to temp... with todays date..

sprintf(temp,"%02d%02d%04d",month,day,year);

In the above.. if it i increase it from 8 to 9.. then the sprintf will work fine with out any issues but it is breaking the application in other place becasue i am copying the value of temp to some other variable that has length only 8...i increased that one aslo to nine... but it has so mnay links like that.. is there any other way that i can assign the value to temp without increase the lenght to 9.... i really appreciate your help.. i am a C# developer but unluckly working onthis project.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900