I have the following code in one cpp file which is part of a project in visual c++ 2010 I’m not understanding why the call to free fails.
#include <time.h>
#include "stdafx.h"
#include <wchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
wchar_t * getRandomShortToken(){
int i;
wchar_t * token = 0;
srand((unsigned int)time(NULL));
token = (wchar_t*)malloc(sizeof(wchar_t) * 6);
memset(token, 0, sizeof(wchar_t*) *6);
for(i = 0; i < 5; i++){
token[i] = ( rand() % 20 ) + 64;
}
return token;
}
int _tmain(int argc, _TCHAR* argv[]){
wchar_t * token = 0;
system("pause");
token = getRandomShortToken();
printf("My Token is '%ls'\n', token);
system("pause");
free(token);
system("pause");
return 0;
}
Note that the token is properly populated in the printf!
Your code corrupts memory in this statement:
You probably meant:
There’s a syntax error in:
Should be: