Gopi,
here's what's happening.
You allocate the memory, which happens to fall within your alotted segment of 
memory.
Great. Now, you free it, and reassign.
The program can reassign, because you've got a nice stray pointer, but, but you 
don't know which bit in memory it's going to access.
***ALWAYS*** set your pointers to NULL when you've deallocated.

Thanks,
Tyler Littlefield
email: [EMAIL PROTECTED]
web: tysdomain-com
Visit for quality software and web design.
skype: st8amnd2005

  ----- Original Message ----- 
  From: Gopi Krishna Komanduri 
  To: [email protected] 
  Sent: Monday, November 10, 2008 11:49 AM
  Subject: [c-prog] A query related to memory allocation


  Hi All,
    I have a very basic query. I know that after deallocating memory for a 
pointer , when we try to access it , we will get access violation error. From 
top of my memory , even I tried it and worked on it. But now I dont know why 
but my code is working in wrong manner now. Can any one of you please suggest 
it please. I am using VisualStudio 2005 compiler. on windows xp.

  #include<stdio.h>
  #include<malloc.h>

  void main()
  {
      int *i;
      i=(int *)malloc(1*sizeof(int));
      *i=30;
      free(i); // freed the mem
      *i=50; //  Tried to acces it again .. Ideally , it should fail . But  it 
is not failing .. can any //one explain the reason.
  }

  Thx,
  --Gopi

  Connect with friends all over the world. Get Yahoo! India Messenger at 
http://in.messenger.yahoo.com/?wm=n/

  [Non-text portions of this message have been removed]



   

[Non-text portions of this message have been removed]

Reply via email to