-1
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.
Deduplicator
  • 43,322
  • 6
  • 62
  • 109
nicomp
  • 4,045
  • 4
  • 21
  • 47

1 Answers1

2

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);
Oka
  • 19,041
  • 5
  • 37
  • 50