simple pointer question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • questions?

    #1

    simple pointer question

    Let's say;

    char * first, *second;
    first=malloc(10 *sizeof(char));
    second=malloc(1 00*sizeof(char) );

    what would be the difference between first and second other than the
    difference in the address.
    I mean, when I use free() function to free the memory, how does the
    system know how large the block to free?
    Where is the information about size stored?
  • Malcolm McLean

    #2
    Re: simple pointer question


    "questions? " <universal_used @hotmail.comwro te in message
    Let's say;
    >
    char * first, *second;
    first=malloc(10 *sizeof(char));
    second=malloc(1 00*sizeof(char) );
    >
    what would be the difference between first and second other than the
    difference in the address.
    I mean, when I use free() function to free the memory, how does the
    system know how large the block to free?
    Where is the information about size stored?
    >
    The trick is to store information in the space immediately before the
    pointer. free() can then subtract a few bytes, and get all the information
    it needs.

    However modern systems use a variety of techniques. See my "memory games"
    chapter of Basic Algorithms, which is free, to understand a few simple
    allocation strategies.

    --
    Free games and programming goodies.


    Comment

    • Ben Bacarisse

      #3
      Re: simple pointer question

      "questions? " <universal_used @hotmail.comwri tes:
      char * first, *second;
      first=malloc(10 *sizeof(char));
      second=malloc(1 00*sizeof(char) );
      >
      what would be the difference between first and second other than the
      difference in the address.
      I mean, when I use free() function to free the memory, how does the
      system know how large the block to free?
      Where is the information about size stored?
      You have asked a FAQ: http://c-faq.com/malloc/freesize.html

      --
      Ben.

      Comment

      • Ivar Rosquist

        #4
        Re: simple pointer question

        On Mon, 26 Nov 2007 00:04:27 +0000, Malcolm McLean wrote:
        "questions? " <universal_used @hotmail.comwro te in message
        >Let's say;
        >>
        >char * first, *second;
        >first=malloc(1 0*sizeof(char)) ;
        >second=malloc( 100*sizeof(char ));
        >>
        >what would be the difference between first and second other than the
        >difference in the address.
        >I mean, when I use free() function to free the memory, how does the
        >system know how large the block to free? Where is the information about
        >size stored?
        >>
        The trick is to store information in the space immediately before the
        pointer. free() can then subtract a few bytes, and get all the
        information it needs.
        >
        However modern systems use a variety of techniques. See my "memory
        games" chapter of Basic Algorithms,
        To the OP: Please, don't. McLean's text has been shown in this
        group to be so riddled with conceptual errors that chances are that
        whatever you read in it will contain at least one.
        which is free,
        It has to be: Who would pay to publish such a collection of
        errors?




        Comment

        • questions?

          #5
          Re: simple pointer question

          On Nov 25, 4:05 pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
          "questions? " <universal_u... @hotmail.comwri tes:
          char * first, *second;
          first=malloc(10 *sizeof(char));
          second=malloc(1 00*sizeof(char) );
          >
          what would be the difference between first and second other than the
          difference in the address.
          I mean, when I use free() function to free the memory, how does the
          system know how large the block to free?
          Where is the information about size stored?
          >
          You have asked a FAQ:http://c-faq.com/malloc/freesize.html
          >
          --
          Ben.
          Thank you guys. Good to know the trick, Awesome!

          Comment

          • Ben Pfaff

            #6
            Re: simple pointer question

            "questions? " <universal_used @hotmail.comwri tes:
            I mean, when I use free() function to free the memory, how does the
            system know how large the block to free?
            This is in the FAQ.

            7.26: How does free() know how many bytes to free?

            A: The malloc/free implementation remembers the size of each block
            as it is allocated, so it is not necessary to remind it of the
            size when freeing.

            7.27: So can I query the malloc package to find out how big an
            allocated block is?

            A: Unfortunately, there is no standard or portable way.

            --
            char a[]="\n .CJacehknorstu" ;int putchar(int);in t main(void){unsi gned long b[]
            ={0x67dffdff,0x 9aa9aa6a,0xa77f fda9,0x7da6aa6a ,0xa67f6aaa,0xa a9aa9f6,0x11f6} ,*p
            =b,i=24;for(;p+ =!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
            2:{i++;if(i)bre ak;else default:continu e;if(0)case 1:putchar(a[i&15]);break;}}}

            Comment

            • santosh

              #7
              Re: simple pointer question

              In article
              <8faf5686-0204-4984-a340-2306d015bac0@e6 g2000prf.google groups.com>,
              questions? <universal_used @hotmail.comwro te on Monday 26 Nov 2007
              5:47 am:
              On Nov 25, 4:05 pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
              >"questions? " <universal_u... @hotmail.comwri tes:
              char * first, *second;
              first=malloc(10 *sizeof(char));
              second=malloc(1 00*sizeof(char) );
              >>
              what would be the difference between first and second other than
              the difference in the address.
              I mean, when I use free() function to free the memory, how does the
              system know how large the block to free?
              Where is the information about size stored?
              >>
              >You have asked a FAQ:http://c-faq.com/malloc/freesize.html
              Thank you guys. Good to know the trick, Awesome!
              What trick?

              That FAQ in essence says that you (the programmer) should not depend on
              the details of the implementation, unless necessary.

              Comment

              • Malcolm McLean

                #8
                Re: simple pointer question

                "santosh" <santosh.k83@gm ail.comwrote in message
                >
                >Thank you guys. Good to know the trick, Awesome!
                >
                What trick?
                >
                That FAQ in essence says that you (the programmer) should not depend on
                the details of the implementation, unless necessary.
                >
                The question was "how is this magic achieved?". The FAQ doesn't answer that.
                It tells you how to use malloc(), and that there is no portable way of
                retrieving the size. But that's a slightly different question.

                --
                Free games and programming goodies.


                Comment

                Working...