trueinViso
trueinViso

Reputation: 1394

Debug assertion failed in visual studio

I am getting this error in visual studio: Debug Assertion Failed! Expression: (L"Buffer is too small" && 0)

It happens at the following lines of code:

program_name = (char *) malloc(strlen(argv[0])+1);
strcpy_s(program_name,sizeof(program_name),argv[0]);

It appears the size of the buffer it is trying to copy the character array to is too small but I am not sure why?

Upvotes: 0

Views: 5498

Answers (1)

Jesse Good
Jesse Good

Reputation: 52365

sizeof(program_name) returns the size of a pointer to char. That is not what you want. Replace that with strlen(argv[0])+1 to pass the size of the allocated buffer.

However, also note that in C++, it would be better just to use std::string:

std::string program_name(argv[0]);

Upvotes: 3

Related Questions