Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does this sprintf_s throw an exception?

char filePrefix[] = "test";
char fileName[100]; fileName[0] = 0;
sprintf_s(fileName, "%s", filePrefix);

I can’t figure out why there’s an exception writing into fileName in the sprintf_s

Exception thrown at 0x00007FF885E3F3A9 (ucrtbased.dll) in foo.exe: 0xC0000005: Access violation writing location 0x0000008331F00000.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

From the documentation, the second argument to sprintf_s should be the size of the destination buffer.

char filePrefix[] = "test";
char fileName[100];
fileName[0] = 0;
sprintf_s(fileName, sizeof fileName, "%s", filePrefix);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading