I am using this simple..yet getting error.
to read a directory path from console window and then printing the path in window..
please do check why am getting unhandled exception error:
Error:Unhandled exception at 0x1029984f (msvcr90d.dll) in new_one.exe: 0xC0000005: Access violation reading location 0x745c3a46.
#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#define MAX_PATH_LENGTH 256
int main(int argc, char *argv[])
{
int i;
int pathlength=100;
char *path=(char *)malloc(MAX_PATH_LENGTH);
free(path);
printf("Enter the path:");
scanf("%s",&path);
printf("%s",path);
getchar();
return 0;
}
Still i get the sam eexception..please give me any suggestion
Is not allocated any memory. You are writing to an unallocated pointer variable resulting in Undefined Behavior, which shows up as an segmentation fault.
You could solve the problem in two ways:
Allocating object on stack:
Create
pathas an array locally on stack, such as:Dynamic Memory allocation:
If you use the second approach, You need to explicitly,
freethe allocated memory after your usage:Usually, avoid using dynamic allocations(second approach) unless the memory requirement is too large to be allocated on stack.