Jump to content

  • Log In with Google      Sign In   
  • Create Account


_itoa_s, strcpy_s, strcat_s. What are these doing exactly


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 Xooch   Members   -  Reputation: 179

Like
0Likes
Like

Posted 29 November 2012 - 12:20 PM

EDIT:

Hey there,

I've just realized that the title may be kind of misleading. so I'm rewriting this part.

Basically I have added a new method which holds the following 'functions' in the title, and I believe its causing my program to break at a very strange point in my 'UpdateSentence' method.

Here is the method I have just recently added "setFPS" in my Text class.

bool Text::setFPS(int fps, ID3D11DeviceContext* deviceContext)
{
char tempS[16];
char fpsS[16];
float red = 1.0f;
float green = 1.0f;
float blue = 1.0f;
bool r;
if(fps > 9999) //Truncate the fps
{
  fps = 9999;
}
_itoa_s(fps, tempS, 10);
strcpy_s(fpsS, "FPS: ");
strcat_s(fpsS, tempS);
/*
if(fps >= 60)   //if FPS is above or equal to 60 set text colour to green
{
  red  = 0.0f;
  green = 1.0f;
  blue = 0.0f;
}
if(fps < 60)   //if FPS is below 60 set text colour to yellow
{
  red  = 1.0f;
  green = 1.0f;
  blue = 0.0f;
}
if(fps < 30)   //if FPS is below 30 set text colour to red
{
  red  = 1.0f;
  green = 0.0f;
  blue = 0.0f;
}
*/
r = UpdateSentence(sentence3, fpsS, 20, 20, red, green, blue, deviceContext);
if(!r)
{
  return false;
}
return true;
}

Now the application isn't breaking/crashing at this point, it's breaking at another method called "UpdateSentence" which you can see in the screenshot here:

http://img547.imageshack.us/img547/3619/accessviolation2.png

Here is the break I'm getting, it seems to be happening with adding colour to the function and I have no idea why its playing up now when it previously wasn't.

Also if someone needs to see more code just ask and I can show

If someone could shine some light on this I would be greatful

Edited by Xuchilbara, 29 November 2012 - 12:40 PM.


Sponsor:

#2 MJP   Moderators   -  Reputation: 10601

Like
0Likes
Like

Posted 29 November 2012 - 01:06 PM

Moving to General Programming

#3 Álvaro   Crossbones+   -  Reputation: 12455

Like
1Likes
Like

Posted 29 November 2012 - 01:12 PM

strcpy_s(fpsS, "FPS: ");
I am not sure what strcpy_s does, but if it's anything like strcpy, you are using fpsS uninitialized.

#4 frob   Moderators   -  Reputation: 19766

Like
2Likes
Like

Posted 29 November 2012 - 01:17 PM

In your debugger in the screen shot you can see that the sentence pointer is 0xCDCDCDCD. This is the debug library's pattern for uninitialized values.

You need to point it to a valid object.
Check out my personal indie blog at bryanwagstaff.com.

#5 Xooch   Members   -  Reputation: 179

Like
0Likes
Like

Posted 29 November 2012 - 01:33 PM

In your debugger in the screen shot you can see that the sentence pointer is 0xCDCDCDCD. This is the debug library's pattern for uninitialized values.

You need to point it to a valid object.


Thanks for the quick reply, although it is pointing to a valid object, I have a struct in my header file which holds the floats:

  struct SentenceType
  {
   ID3D11Buffer* vertexBuffer;
   ID3D11Buffer* indexBuffer;
   int vertexCount, indexCount, maxLength;
   float red, green, blue;
  };

and then it's equal to the float 'red' in the parameter of UpdateSentence

#6 SiCrane   Moderators   -  Reputation: 9496

Like
4Likes
Like

Posted 29 November 2012 - 01:45 PM

On 32-bit Windows, the maximum user addressable memory is 3GB, with the top 1GB of the virtual address space reserved for the kernel. If your pointer value is 0xC0000000 or higher then it can't be a legal pointer value. More specifically OxCDCDCDCD is MSVC's debug fill pattern for uninitialized memory. If you see a pointer error involving that address it generally means that you are accessing uninitialized memory, which since the debugger is identifying sentence's value as 0xCDCDCDCD that probably means that you either don't initialize sentence correctly when creating your object or that you are calling a member function on an invalid object pointer. Either way the sentence pointer isn't valid.

#7 Xooch   Members   -  Reputation: 179

Like
0Likes
Like

Posted 29 November 2012 - 01:49 PM

On 32-bit Windows, the maximum user addressable memory is 3GB, with the top 1GB of the virtual address space reserved for the kernel. If your pointer value is 0xC0000000 or higher then it can't be a legal pointer value. More specifically OxCDCDCDCD is MSVC's debug fill pattern for uninitialized memory. If you see a pointer error involving that address it generally means that you are accessing uninitialized memory, which since the debugger is identifying sentence's value as 0xCDCDCDCD that probably means that you either don't initialize sentence correctly when creating your object or that you are calling a member function on an invalid object pointer. Either way the sentence pointer isn't valid.


I see, but it was working previously, before I added the "setFPS" function, but if thats the case, is there anyway of getting round it?

#8 ApochPiQ   Moderators   -  Reputation: 14664

Like
1Likes
Like

Posted 29 November 2012 - 02:27 PM

Walk up your call stack one level. Whatever is passing the "sentence" argument is passing a garbage pointer.

#9 L. Spiro   Crossbones+   -  Reputation: 12878

Like
0Likes
Like

Posted 29 November 2012 - 02:32 PM

Now the application isn't breaking/crashing at this point, it's breaking at another method called "UpdateSentence" which you can see in the screenshot here:

Then why don’t you show the code for UpdateSentence()?

Also, sentence3 is garbage when it is passed to UpdateSentence(). I assume it is a member of the Text class.
Naming conventions such as m_ would go a long way to help people realize from where variables are coming.
It needs to be initialized—this has nothing to do with the functions listed in the topic.


L. Spiro

Edited by L. Spiro, 29 November 2012 - 02:36 PM.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#10 Khatharr   Crossbones+   -  Reputation: 2934

Like
1Likes
Like

Posted 29 November 2012 - 02:33 PM

strcpy_s(fpsS, "FPS: ");
I am not sure what strcpy_s does, but if it's anything like strcpy, you are using fpsS uninitialized.


strcpy_s(char* dest, const char* src); works like strcpy but it will fail (I think with an exception? maybe just a windows error) if it would cause an overrun.

As for the error, it looks like that address is actually 0xCDCDCDE1, which suggests that somewhere you're adding 20 to an uninitialized pointer rather than initializing it.

Just put a conditional breakpoint at the start of the function there that breaks if the pointer value is 0xCDCDCDE1 and then look at the stack to see where it's coming from.

Edited by Khatharr, 29 November 2012 - 02:34 PM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#11 Álvaro   Crossbones+   -  Reputation: 12455

Like
0Likes
Like

Posted 29 November 2012 - 02:38 PM

Oh, please ignore my comment. I thought we were talking about strcat(). I need some rest. ;)

#12 Khatharr   Crossbones+   -  Reputation: 2934

Like
0Likes
Like

Posted 29 November 2012 - 02:54 PM

Oh, please ignore my comment. I thought we were talking about strcat(). I need some rest. ;)


lol, I know that feeling. Posted Image

As a side note:

[source lang="cpp"]_itoa_s(fps, tempS, 10);strcpy_s(fpsS, "FPS: ");strcat_s(fpsS, tempS);[/source]

Could be:
[source lang="cpp"]sprintf_s(fpsS, "FPS: %i", tempS);[/source]
If you want to use C style string handling.

Edited by Khatharr, 29 November 2012 - 02:55 PM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#13 Xooch   Members   -  Reputation: 179

Like
0Likes
Like

Posted 29 November 2012 - 02:54 PM


Now the application isn't breaking/crashing at this point, it's breaking at another method called "UpdateSentence" which you can see in the screenshot here:

Then why don’t you show the code for UpdateSentence()?

Also, sentence3 is garbage when it is passed to UpdateSentence(). I assume it is a member of the Text class.
Naming conventions such as m_ would go a long way to help people realize from where variables are coming.
It needs to be initialized—this has nothing to do with the functions listed in the topic.


L. Spiro


Ah thats done it, it wasn't initializing correctly and going straight to garbage, it is working now thanks alot everyone! :)

Also in essence, _itoa_s, strcpy_s, strcat_s, wasn't even the problem, sorry for being misleading :/

Edited by Xuchilbara, 29 November 2012 - 02:57 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS