4 Replies - 487 Views - Last Post: 26 January 2012 - 05:35 AM Rate Topic: -----

#1 janus87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 25-January 12

Memory Management in C.

Posted 25 January 2012 - 07:14 PM

Oh hey ya'll,

I'm having a little trouble understanding this issue of memory management.

Wouldn't the freeing of p2 free p1 there for if I attempt to print p1 it the program should segfault or not print that "data"?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {

	char *p1;
	p1 = (char *)malloc(sizeof(char) * 2);
	strcpy(p1, "R");

	char *p2;

	p2 = p1;
	
	printf("p1 address = %p, p1 data = %s; p2 address = %p, p2 data = %s\n", p1, p1, p2, p2);

	free(p2);

	// this should not print?
	// should segfault if p2's address is the same as p1's and if I freed p2 wouldn't that essentially free p1?
	// hrmmmmmmmmmmm......
	printf("%p -> %s\n", p1, p1);
	
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Memory Management in C.

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1526
  • View blog
  • Posts: 5,514
  • Joined: 03-August 09

Re: Memory Management in C.

Posted 25 January 2012 - 08:30 PM

printf("%p -> %s\n", p1, p1);

the second part("%s") is undefined. you can't count on it to segfault or print; you can't count on it to do anything. it's likely to simply print because when something is freed it generally isn't un-pagged. free might simply recycle the data for a later malloc or realloc call and the OS might not un-page it right away(thus recycling it too). this is all especially true if your talking about small values. The issue still stands however that this *might* happen and at some point in the program it *will* happen. so if you try to use memory that has been freed then you're dancing on the broken glass of undefined behavior and you've got to expect a cut or too(forget whose sig that's in)

This post has been edited by ishkabible: 25 January 2012 - 08:33 PM

Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2485
  • View blog
  • Posts: 8,525
  • Joined: 08-August 08

Re: Memory Management in C.

Posted 25 January 2012 - 08:32 PM

Freeing memory doesn't necessarily "erase" it. It's just free to be reused.
Was This Post Helpful? 0
  • +
  • -

#4 janus87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 25-January 12

Re: Memory Management in C.

Posted 25 January 2012 - 10:04 PM

Thanks guys for the info! I get it now.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4884
  • View blog
  • Posts: 11,279
  • Joined: 16-October 07

Re: Memory Management in C.

Posted 26 January 2012 - 05:35 AM

You want to really hurt your head; try this:
char *p1, *p2;

// don't cast this
p1 = malloc(sizeof(char) * 2);
strcpy(p1, "R");

p2 = p1;
printf("p1 address = %p, p1 data = %s; p2 address = %p, p2 data = %s\n", p1, p1, p2, p2);
free(p2);

p2 = malloc(sizeof(char) * 6);
strcpy(p2, "Hello");
printf("p1 address = %p, p1 data = %s; p2 address = %p, p2 data = %s\n", p1, p1, p2, p2);
free(p2);



While it's undefined behavior, the second malloc will usually return the same address the first did. Your malloc starts at the top of a block it decides it owns and starts giving slices. So, if you malloc, free, and immediately malloc again, chances are you're going to get the same address.

Freeing tells the system that the block of bytes associated with that address are available again. It doesn't do any more than that. The address in the pointer remains unchanged, though it's no longer valid. The area in memory is now available, but it's wasteful write anything to unused space, so the last thing there usually stays there until something else gets that memory to use and starts writing to it.

Conversely, this is where the crap comes from in uninitialized variables; it's whatever was left there before.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1