Return values from malloc

This is a discussion on Return values from malloc within the C Programming forums, part of the General Programming Boards category; Hi, I've just been looking at the man page for malloc as I was investigating a memory related problem and ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    13

    Return values from malloc

    Hi,

    I've just been looking at the man page for malloc as I was investigating a memory related problem and noticed the following that was relevent

    If size, nelem, or elsize is 0, a unique pointer to the arena is returned.

    What exactly does this mean? is it a valid pointer? I'm using a run-time memory checker and it is saying that the program is assigning a wild pointer.

    I know this is obviously bad, and I've sorted it, but I've not heard of this before and wondered if any knew anything more?

    Thanks in advance

    Craig

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Given
    p = malloc ( 0 );

    It doesn't matter whether p is NULL or not, because you can't do anything with it in either case.
    If it's NULL, then p[0] is a segmentation fault.
    If it isn't NULL, then p[0] is a buffer overflow.
    Either way, you lose.

    > If size, nelem, or elsize is 0, a unique pointer to the arena is returned.
    p = malloc( 0 );
    q = malloc( 0 );
    It's just saying that p and q are unique pointers to 0 bytes - that is p != q

    > I'm using a run-time memory checker and it is saying that the program is assigning a wild pointer.
    From just the malloc call, or are you doing something with the pointer afterwards?

    Sounds like it could be a compatibility problem between the malloc implementation and the memory checker. Which OS/Compiler/Checker are you using?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    13
    Thanks,

    I'm using gcc 2.95.3 on solaris 9, with Insure++

    The line it is reporing the error on is

    Code:
    if ( ( tsites=malloc(number_of_sites*sizeof(IndexEntry)) )!=0 )
    It states that the program is assigning a wild pointer, and using a wild pointer in an expression.

    I understand what you say with regard p!=q, but where are these pointers? What is 'The Arena'?

    Regards

    Craig

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    heap?

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    13
    Yeah, that is what I was thinking, so therefore as it's a pointer to the heap, returned from malloc I wouldn't have thought it would have been 'wild'; not much use, certainly, but not 'wild'

    As Salem said, could be a compatability problem......

  6. #6
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Furthering what Salem said, my copy of H&S "A C Reference Manual" states on page 407:

    "If the requested size is 0, then the Standard C functions will return either a null pointer or a non-null pointer that nonetheless must not be used to access an object."

    I'm not sure what the "arena" is, other than a typo

    I would say that you might want to add a check to ensure that the number of sites variable is not 0 before you attempt to allocate the memory.....
    Mr. Blonde: You ever listen to K-Billy's "Super Sounds of the Seventies" weekend? It's my personal favorite.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > but where are these pointers? What is 'The Arena'?
    Basically, the arena is a name for the huge pool of memory where malloc gets it's memory from.

    As a test, what does this do
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    int main ( ) {
      char *p = malloc ( 0 );
      if ( p != NULL ) {
        printf( "malloc(0) returned a pointer = %p\n", (void*)p );
      } else {
        printf( "malloc(0) returned NULL\n" );
      }
      free( p );
      return 0;
    }
    Does this complain when it's traced with your debug tool?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by Salem
    > but where are these pointers? What is 'The Arena'?
    Basically, the arena is a name for the huge pool of memory where malloc gets it's memory from.
    Gee, learn something new every day.....

    Would that be synonymous with the heap?
    Mr. Blonde: You ever listen to K-Billy's "Super Sounds of the Seventies" weekend? It's my personal favorite.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Yeah, I'd call it a heap.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. is it ok like that?
    By ExDHaos in forum C++ Programming
    Replies: 8
    Last Post: 05-23-2009, 09:02 AM
  2. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 12:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21