Re: [LIP] free issue: contd from a previous thread.
"Josy P. Pullockara" <[email protected]>
| Newsgroups | gmane.user-groups.linux.india.programmers |
|---|---|
| Organization | National Aerospace Laboratories |
| Message-ID | <[email protected]> |
On Thu, 2004-01-22 at 16:05, Shehjar Tikoo wrote:
> freepthead(&h);
> Then when I do a fprintf on the path member of the
> pthead, I a get some junk characters instead of the
> Seg Fault.
You are not supposed to access a memory location after freeing it. The
language (C) does not guarantee any "segmentation fault". It is
dependant on implementation.
On my GNU/Linux with gcc version 3.3.1 (Mandrake Linux 9.2 3.3.1-2mdk)
the following is what I get.
$cat t.c
#include <stdio.h>
struct ptnode {
int i;
};
void
freepthead(struct ptnode** h)
{
free(*h);
*h = 0;
}
int
main(void)
{
struct ptnode* head;
head = (struct ptnode*)malloc(sizeof(struct ptnode));
head->i = 2;
printf("before free: %d\n", head->i); fflush(stdout);
freepthead(&head);
printf("after free: OK\n"); fflush(stdout);
printf("after free: %d\n", head->i); fflush(stdout);
return 0;
}
$gcc t.c
$a.out
before free: 2
after free: OK
Segmentation fault
$
This may not be the same on some other implementations such as for
SGIRIX, HPUX, SunOS or others. You should not in any way depend on this
behaviour in your programs.
--
Josy P. Pullockara <[email protected]>
National Aerospace Laboratories
-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004
Premiere Conference on Open Tools Development and Integration
See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn