I have a simple C file that compile fine in the developer command prompt using a basic cl
command.
cl C:\some_path\src\test2.c /Zi
But when I set this up as a Visual Studio project and run it (x64/Debug), I get bunch of errors and warning.
What kind of basics I need to have setup so I can run the project from within Visual Studio.
The following also works:
devenv test2.exe
will load the source code, run it and debug it just fine.
Microsoft Visual Studio Community 2022 (64-bit) - Current Version 17.9.5
Error:
Severity Code Description Project File Line Suppression State Details
Error C4996 'strerror': This function or variable may be unsafe. Consider using strerror_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. test C:\some_path\nob.h 392
Warnings:
Severity Code Description Project File Line Suppression State Details
Warning C6308 'realloc' might return null pointer: assigning null pointer to '((render))->items', which is passed as an argument to 'realloc', will cause the original memory block to be leaked. test C:\some_path\nob.h 472
Here is test.c
#include <string.h>
#include <stdio.h>
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#define NOB_IMPLEMENTATION
#include "nob.h"
#define _CRT_SECURE_NO_WARNINGS
int main(void)
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
int c = 0;
nob_log(NOB_INFO, "[002] Hello World!");
int* foo = malloc(56);
_CrtDumpMemoryLeaks();
return 0;
}
To avoid the first warning include the following macro before include directives
#define _CRT_SECURE_NO_WARNINGS
As for the second warning then you need to use an intermediate pointer to store the result of a call of realloc
and test it before assigning its value to the original pointer. Otherwise you can overwrite the original pointer wil returned null value and as result you will get a memory leak.
For example
int *p = malloc( sizeof( int ) );
//...
int *tmp = realloc( p, 2 * sizeof( int ) );
if ( tmp != NULL )
{
p = tmp;
//...
}
else
{
// some other code
}
//...
free( p );
If you will just write
p = realloc( p, 2 * sizeof( int ) );
then in case of a failure of the memory reallocation the address of the previously allocated memory will be lost because the pointer p
in this case becomes equal to NULL
.