0

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

hari
  • 1,341
  • 2
  • 14
  • 16
  • why are you freeing the memory allocated for `path` as the next step? It would be appropriate to free the memory for `path` after you are done with it. That is, in your program it would be right before you `return 0;` – yasouser Jan 30 '12 at 10:07
  • Thanks for the reply...but i get the same unhnadled exception..any idea to solve it..? – hari Jan 30 '12 at 10:20
  • Its high time you learn to use a debugger and step through the code yourself. – yasouser Jan 30 '12 at 10:26

1 Answers1

2
char *path;

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 path as an array locally on stack, such as:

#define MAX_PATH_LENGTH 256

char path[MAX_PATH_LENGTH];

Dynamic Memory allocation:

#define MAX_PATH_LENGTH 256

char *path=(char *)malloc(MAX_PATH_LENGTH);

If you use the second approach, You need to explicitly, free the allocated memory after your usage:

free(path);

Usually, avoid using dynamic allocations(second approach) unless the memory requirement is too large to be allocated on stack.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • The cast for the return value of `malloc` is, at best, redundant; and (as in the provided code) it may hide an error the compiler would have caught otherwise. – pmg Jan 30 '12 at 09:58
  • Just now i changed the code mentioned above still the same unhandled exception – hari Jan 30 '12 at 10:02
  • @hari: You should call the `free()` after your usage of `path` is done.Right now you allocate memory to `path` and in the very next statement you deallocate it using `free()`, this leaves your pointer with no memory and the same situation as in the original problem.Call `free()` just before `return 0;` – Alok Save Jan 30 '12 at 10:14
  • @hari: Also, `scanf("%s",&path);` should be `scanf("%s",path);`, notice the absence of `&` in front of `path`. Also, I am going to suggest to pick up a good book and learn the fundamentals.You need a [good book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) to learn the language not online tutorials or blogs. – Alok Save Jan 30 '12 at 10:15
  • Hi if i use the char[256]; it will show the directory path of one folder only eg:F:\test.If i use the path as F:\test\tools it doersnot show any thing.Why am getting unhandled exception if i use dynamic memory allocation procedure – hari Jan 30 '12 at 10:19