S
spl
I am getting access violation in the below program for the free()
call, Whats wrong here and how to rectify it?
----------------------------------
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
char * CopyString(char *s)
{
int length = strlen(s);
char * t = (char *)malloc(length);
strcpy(t,s);
return t;
}
void main()
{
char *destStr;
char *sourceStr = "Test";
destStr = CopyString(sourceStr);
printf("Destination String : %s\n", destStr);
free(destStr);
}
call, Whats wrong here and how to rectify it?
----------------------------------
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
char * CopyString(char *s)
{
int length = strlen(s);
char * t = (char *)malloc(length);
strcpy(t,s);
return t;
}
void main()
{
char *destStr;
char *sourceStr = "Test";
destStr = CopyString(sourceStr);
printf("Destination String : %s\n", destStr);
free(destStr);
}