void *malloc(size_t num_bytes);

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

    void *malloc(size_t num_bytes);

    Hi,
    i was reading a chapter about c pointers in "c++builder complete
    reference" and it is said there that malloc returns a pointer to the
    start of the memory allocated but i see the prototype of it saying void
    *malloc(----);
    does not void mean that the function does not return anything?
    thanks

  • Mark F. Haigh

    #2
    Re: void *malloc(size_t num_bytes);

    Alawna wrote:[color=blue]
    > Hi,
    > i was reading a chapter about c pointers in "c++builder complete
    > reference" and it is said there that malloc returns a pointer to the
    > start of the memory allocated but i see the prototype of it saying void
    > *malloc(----);
    > does not void mean that the function does not return anything?
    > thanks[/color]

    A "void *" means roughly "a pointer to something". Thus, the malloc
    function returns a pointer to a memory location where you can store
    something.

    Review your section on pointers. Don't confuse "void" with "void *".

    Mark F. Haigh
    mfhaigh@sbcglob al.net

    Comment

    • Alawna

      #3
      Re: void *malloc(size_t num_bytes);

      thanks

      Comment

      • Alawna

        #4
        Re: void *malloc(size_t num_bytes);

        so let us suppose that malloc returns an integer then its prototype
        would be int void *malloc(-----); . is this accurate?

        Comment

        • forayer

          #5
          Re: void *malloc(size_t num_bytes);

          Alawna wrote:[color=blue]
          > so let us suppose that malloc returns an integer then its prototype
          > would be int void *malloc(-----); . is this accurate?[/color]

          not exactly.

          u see, a malloc call is as below:
          <pointer var> = (pointer type cast)malloc( num bytes needed );

          now, if u want to dynamically allocate an int, u wud pass da size of an
          int to malloc as:
          malloc( sizeof(int) );
          this will allocate 2 bytes (assuming int is 2 bytes long) in some loc
          in memory, n return the _address_ to that loc.
          to use that allocated mem u need sumthin to ref it. so u use an 'int'
          ptr. so if u hav an int *p, then
          p = (int *)malloc( sizeof(int) );
          will make p point to the allocated addr.

          in case of allocation failure, malloc will return NULL, so chk 4 it b4
          usin p, or b prepared 4 sum nasty seg faults!

          cheers,
          forayer

          Comment

          • Martin Ambuhl

            #6
            Re: void *malloc(size_t num_bytes);

            Alawna wrote:[color=blue]
            > so let us suppose that malloc returns an integer then its prototype
            > would be int void *malloc(-----); . is this accurate?
            >[/color]
            No, it would be (if the integer type happens to be int)
            int malloc( /* whatever */);
            But we know that malloc does not return an int.

            Comment

            • Emmanuel Delahaye

              #7
              Re: void *malloc(size_t num_bytes);

              Alawna wrote on 19/06/05 :[color=blue]
              > Hi,
              > i was reading a chapter about c pointers in "c++builder complete
              > reference" and it is said there that malloc returns a pointer to the
              > start of the memory allocated but i see the prototype of it saying void
              > *malloc(----);
              > does not void mean that the function does not return anything?
              > thanks[/color]

              Nope. You are mixing 'void' (returns nothing) and 'void*' (returns an
              address of unspecified type).

              --
              Emmanuel
              The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
              The C-library: http://www.dinkumware.com/refxc.html

              ..sig under repair

              Comment

              • Emmanuel Delahaye

                #8
                Re: void *malloc(size_t num_bytes);

                Alawna wrote on 19/06/05 :[color=blue]
                > so let us suppose that malloc returns an integer then its prototype
                > would be int void *malloc(-----); . is this accurate?[/color]

                No. If a function must return an int, its return type will be int

                int f (void);

                Stay simple.

                --
                Emmanuel
                The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
                The C-library: http://www.dinkumware.com/refxc.html

                "C is a sharp tool"

                Comment

                • Alawna

                  #9
                  Re: void *malloc(size_t num_bytes);

                  thank you guys.
                  got it now. need more time to get used to c syntax. coming from pascal.

                  Emmanuel Delahaye wrote:[color=blue]
                  > Alawna wrote on 19/06/05 :[color=green]
                  > > so let us suppose that malloc returns an integer then its prototype
                  > > would be int void *malloc(-----); . is this accurate?[/color]
                  >
                  > No. If a function must return an int, its return type will be int
                  >
                  > int f (void);
                  >
                  > Stay simple.
                  >
                  > --
                  > Emmanuel
                  > The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
                  > The C-library: http://www.dinkumware.com/refxc.html
                  >
                  > "C is a sharp tool"[/color]

                  Comment

                  • Tim Rentsch

                    #10
                    Re: void *malloc(size_t num_bytes);

                    "Mark F. Haigh" <mfhaigh@sbcglo bal.net> writes:
                    [color=blue][color=green]
                    > > [snip][/color]
                    >
                    > A "void *" means roughly "a pointer to something".[/color]

                    So "pointer to nothing" means roughly "a pointer to something"?!??

                    That's why we like C!

                    :)

                    Comment

                    • forayer

                      #11
                      Re: void *malloc(size_t num_bytes);



                      Tim Rentsch wrote:[color=blue]
                      > "Mark F. Haigh" <mfhaigh@sbcglo bal.net> writes:
                      >[color=green][color=darkred]
                      > > > [snip][/color]
                      > >
                      > > A "void *" means roughly "a pointer to something".[/color]
                      >
                      > So "pointer to nothing" means roughly "a pointer to something"?!??[/color]

                      no, 'void *' means a pointer to any kind of thing! but it definitely
                      points to something, though this "something" can even be NULL!!!

                      Now, that's why we luv C
                      ;-D

                      Comment

                      • Flash Gordon

                        #12
                        Re: void *malloc(size_t num_bytes);

                        forayer wrote:[color=blue]
                        > Alawna wrote:
                        >[color=green]
                        >>so let us suppose that malloc returns an integer then its prototype
                        >>would be int void *malloc(-----); . is this accurate?[/color]
                        >
                        > not exactly.
                        >
                        > u see, a malloc call is as below:[/color]

                        Please don't use abbreviations like u for you, it makes it unnecessarily
                        hard to read your posts, especially for those whose native language is
                        not English.
                        [color=blue]
                        > <pointer var> = (pointer type cast)malloc( num bytes needed );[/color]

                        The cast is *not* required.
                        [color=blue]
                        > now, if u want to dynamically allocate an int, u wud pass da size of an
                        > int to malloc as:
                        > malloc( sizeof(int) );[/color]

                        A far better method is
                        ptr = malloc( N * sizeof *ptr );
                        Then you don't get the type wrong (which typically happens when
                        allocating space for more complex items such as pointers to pointers)
                        and you don't have to change the line if the type of ptr is changed.
                        [color=blue]
                        > this will allocate 2 bytes (assuming int is 2 bytes long) in some loc
                        > in memory, n return the _address_ to that loc.
                        > to use that allocated mem u need sumthin to ref it. so u use an 'int'
                        > ptr. so if u hav an int *p, then
                        > p = (int *)malloc( sizeof(int) );
                        > will make p point to the allocated addr.[/color]

                        As stated above, the cast is not required. If the compiler complains
                        without the cast then there is something else wrong, such as using a C++
                        compiler or failing to include stdlib.h
                        [color=blue]
                        > in case of allocation failure, malloc will return NULL, so chk 4 it b4
                        > usin p, or b prepared 4 sum nasty seg faults![/color]

                        That sentence took me twice as long to read as it should have because of
                        the stupid abbreviations.

                        seg faults are specific to some OSs, not required by the standard. Far
                        worse than getting seg faults can happen, such as it appearing to work
                        until you show the program to you girlfriend to demonstrate how clever
                        you are and instead of doing what you expected it prints out a love poem
                        addressed to her best friend.
                        --
                        Flash Gordon
                        Living in interesting times.
                        Although my email address says spam, it is real and I read it.

                        Comment

                        • Joe Wright

                          #13
                          Re: void *malloc(size_t num_bytes);

                          forayer wrote:[color=blue]
                          >
                          > Tim Rentsch wrote:
                          >[color=green]
                          >>"Mark F. Haigh" <mfhaigh@sbcglo bal.net> writes:
                          >>
                          >>[color=darkred]
                          >>>>[snip]
                          >>>
                          >>>A "void *" means roughly "a pointer to something".[/color]
                          >>
                          >>So "pointer to nothing" means roughly "a pointer to something"?!??[/color]
                          >
                          >
                          > no, 'void *' means a pointer to any kind of thing! but it definitely
                          > points to something, though this "something" can even be NULL!!!
                          >
                          > Now, that's why we luv C
                          > ;-D
                          >[/color]
                          No. NULL is a value, not a type. NULL valued pointers point to nothing
                          regardless the type of the pointer.

                          --
                          Joe Wright
                          "Everything should be made as simple as possible, but not simpler."
                          --- Albert Einstein ---

                          Comment

                          • forayer

                            #14
                            Re: void *malloc(size_t num_bytes);

                            Joe Wright wrote:[color=blue]
                            > No. NULL is a value, not a type. NULL valued pointers point to nothing
                            > regardless the type of the pointer.
                            >[/color]

                            all i meant to say was that a 'void *' can _sometimes_ be a pointer to
                            nothing, but not always as Tim said it to be. :-)

                            cheers,
                            forayer

                            Comment

                            • Tim Rentsch

                              #15
                              Re: void *malloc(size_t num_bytes);

                              "forayer" <theforayer@hot mail.com> writes:
                              [color=blue]
                              > Tim Rentsch wrote:[color=green]
                              > > "Mark F. Haigh" <mfhaigh@sbcglo bal.net> writes:
                              > >[color=darkred]
                              > > > > [snip]
                              > > >
                              > > > A "void *" means roughly "a pointer to something".[/color]
                              > >
                              > > So "pointer to nothing" means roughly "a pointer to something"?!??[/color]
                              >
                              > no, 'void *' means a pointer to any kind of thing! but it definitely
                              > points to something, though this "something" can even be NULL!!![/color]

                              A NULL pointer isn't a pointer to anything.

                              A 'void *' is a pointer to nothing - just dereference it
                              to get that void value. :)

                              Speaking of which, why isn't

                              void
                              foo(){
                              void *p = ...whatever...;

                              return *p;
                              }

                              allowed?

                              It's all just humor, don't take it too seriously....

                              Comment

                              Working...