LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-13-2004, 12:26 PM   #1
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
malloc/free in C


hey all. i was wondering why I dont get a segmentation fault or any kind of error running this:
Code:
#include <stdio.h>
#include <stdlib.h>

int main (void)
{
  int *p;
  p = (int *) malloc (sizeof (int));

  *p = 10;
  printf ("p contains %d and memloc is %p\n", *p, p);
  free (p);

  *p = 20;
  printf ("p contains %d and memloc is %p\n", *p, p);

  return 0;
}
i was expecting the call to free to lead to an error in the next reference to p. why does this work?
thanks once again.
 
Old 02-13-2004, 04:23 PM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
malloc is responsible for maintaining the brk pointer for the process. this pointer points to the top of the heap(it grows up on linux). malloc obtains more heap space in PAGE_SIZE chunks, usually 0x1000. it then gives this memory to you when u call malloc. when you allocate more memory than malloc has left in its current chunk, it will move the brk pointer ahead by calling brk() to get another 0x1000 size chunk. when you free some memory, it doesnt necessarily mean that malloc will call brk() to shrink the heap. so, even tho u gave back the memory, it is still below the brk pointer so its still a valid reference. example:
Code:
#include <unistd.h>


int main()
{
    int y = 0;
    char    *x = NULL;


    x = malloc(100);
    if(!x)
        return 1;

    for(y = 0; y < 0x2000; y++)
        printf("x[%d] = %c \n", y, x[y]);
    

    return 0;
}
...
x[6838] =
x[6839] =
Segmentation fault

just showing you can way overstep your allocated memory as long as you stay below the brk pointer for the process. depending on how much you give back when you free, it may or may not actually move back the brk pointer.

Last edited by infamous41md; 02-13-2004 at 04:25 PM.
 
Old 02-13-2004, 08:43 PM   #3
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
thanks man.
 
Old 02-13-2004, 08:46 PM   #4
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
ohhh, one more thing - how do we get the position offff this brk ptr? is the brk ptr the same as the stack ptr? or are they different things? (yeah, im a bit confused.)
 
Old 02-13-2004, 09:11 PM   #5
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
You can't really, it's an implementation detail -- as far as the ANSI C standard is concerned, writing to memory which has been free'd always has the potential to cause the program to crash, and the behaviour will vary from OS to OS. The brk pointer isn't the same as the stack pointer, btw (the stack pointer is actually held in a processor register, it's part of the inner workings of x86 processors).

Alex
 
Old 02-13-2004, 11:00 PM   #6
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
thanks guys.
 
Old 02-14-2004, 12:40 AM   #7
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
man brk

sbrk increments the program's data space by increment
bytes. sbrk isn't a system call, it is just a C library
wrapper. Calling sbrk with an increment of 0 can be used
to find the current location of the program break.
 
Old 02-14-2004, 07:17 AM   #8
cjcuk
Member
 
Registered: Dec 2003
Distribution: Openwall, ~LFS
Posts: 128

Rep: Reputation: 15
As a piece of advice, following calls to free with a NULL assignment is not a bad thing to do. It is not foolproof, but can help you catch issues more easily.

eg:
free(ptr);
ptr = (type *)NULL;
 
Old 02-14-2004, 11:39 AM   #9
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
gotcha!
 
Old 02-14-2004, 01:31 PM   #10
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
Quote:
Originally posted by cjcuk
As a piece of advice, following calls to free with a NULL assignment is not a bad thing to do. It is not foolproof, but can help you catch issues more easily.

eg:
free(ptr);
ptr = (type *)NULL;
whenever i wrote a linked-list with structs, i would use free, and i never really checked to see if that memory was actually freed or not.

for some reason, im associating this behaviour more with native data types, than with objects i create, say with struct definitions.
 
Old 02-26-2004, 12:55 PM   #11
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
just saw this snippet someplace:
Code:
void
f (void)
{
  struct sss *s = malloc (sizeof (*s));
  char *string2 = malloc (strlen (string1) + 1);

  /* ... */

  free (s);
  free (string2);
}
2 questions here - should malloc be called there with a sizeof(*s)? or should it be sizeof(struct sss)? wont what has been coded be allocating 4 bytes, and not the size of the struct?

also, is there supposed to be an order in which we free() the allocated mem? would reversing the two free() calls above make a difference?

thanks.
 
Old 02-26-2004, 01:11 PM   #12
cjcuk
Member
 
Registered: Dec 2003
Distribution: Openwall, ~LFS
Posts: 128

Rep: Reputation: 15
Quote:
Originally posted by h/w
just saw this snippet someplace:
Code:
void
f (void)
{
  struct sss *s = malloc (sizeof (*s));
  char *string2 = malloc (strlen (string1) + 1);

  /* ... */

  free (s);
  free (string2);
}
2 questions here - should malloc be called there with a sizeof(*s)? or should it be sizeof(struct sss)? wont what has been coded be allocating 4 bytes, and not the size of the struct? [1]

also, is there supposed to be an order in which we free() the allocated mem? would reversing the two free() calls above make a difference? [2]

thanks.
[1] - No, *s is a dereferenced pointer, thus is considered the type it is pointing to. It is probably better to use sizeof(struct sss) -- as I am not sure if the compiler will always be successful in dealing with a dereferenced pointer in such a way ( though, it should be ).

[2] - No, reversing the calls is fine. Dynamic memory allocation does not work like the stack ( which is LIFO ), you can allocate and deallocate as and when necessary -- otherwise it would not be very dynamic . The point behind malloc(3) is to try to speed up this process by using clever algorithms ( eg, doug lea's or phk's ) on how memory has been and will be ( predicted ) allocated.
 
Old 02-26-2004, 01:13 PM   #13
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
cool - thanks. dont know what the heck i was thinking with question 1. d-oh!!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
*** glibc detected *** malloc() / free()/ double RohanShrivastav Programming 12 10-01-2012 10:08 AM
malloc eagle683 Programming 6 05-22-2005 02:40 PM
how does malloc() and free() functions work? kuna Programming 8 12-04-2004 03:59 AM
malloc/free and segfault - advanced question iTux Programming 3 12-10-2003 04:51 PM
Is my malloc/free thinking correct? registering Programming 6 06-18-2003 11:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration