-3

If we twice free() the same char pointer which is allocated with malloc(), will it cause segfalut?

void Allocate() 
{     
   char *y;
   y = (char *) malloc (sizeof(char) * 200);    
   free (y);
   strcpy(y,"helloworld");
   free (y);
}

int main()
{
     Allocate();
     return 0;
}
Dinesh
  • 1,825
  • 5
  • 31
  • 40
Jeyamaran
  • 783
  • 1
  • 9
  • 16
  • I checked it doesn't dump core. – Jeyamaran Mar 17 '14 at 09:57
  • @mangusta: A self-check validates something for a particular compiler during a particular moon-phase. Sometimes that is enough; other times it's best to check the Standard (or SO). – pmg Mar 17 '14 at 09:59

3 Answers3

2

It's undefined behavior, so don't do it.

Even more, don't use the memory after calling free() in it, this is a total no-no:

free (y);
strcpy(y,"helloworld");

The above is also undefined behavior.

Also, don't cast the return value of malloc() in C.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
0

Freeing the same memory twice will result in undefined behaviour, as will writing to memory which has been freed (see the strcpy).

The C Standard defines Undefined Behaviour in Section §1.3.12 as

behaviour, such as might arise upon use of an erroneous program construct or erroneous data, for which this International Standard imposes no requirements [3].

Undefined behaviour may also be expected when this International Standard omits the description of any explicit definition of behavior.

(3) = permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or with- out the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).

As to what's happening I would guess the following:

  1. You free y

  2. You then strcpy over what was once y which may corrupt the heap; this itself is unlikely to segfault, but might (see above)

  3. You then allocate z, which uses the possibly corrupted heap, and may segfault or misallocate the memory (i.e. return an address which cannot be used).

  4. You then strcpy into a possibly misallocated bit of memory and thus may segfault.

  5. You then free y again, with a heap which is possibly corrupt, which may segfault etc.

If you really want to know, use debug libc and gdb to find out.

abligh
  • 24,573
  • 4
  • 47
  • 84
  • Those thing i hv already knw. But i just need will it core dump at any situation. – Jeyamaran Mar 17 '14 at 10:14
  • The question says "will it cause segfalut" *(sic)*, not "will it core dump". Segfaults do not always cause a core dump. You will have to do (OS dependent) `ulimit -c unlimited` before running it to get a core dump. – abligh Mar 17 '14 at 20:38
0

Freeing same pointer twice may cause heap corruption. In your program after deallocating you are trying to copy a string which will cause segfault.

s.s
  • 138
  • 8