the maximum memory size allowed in malloc

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

    the maximum memory size allowed in malloc

    I am wondering what is the maximum size of memory that malloc() could
    handle. Is there any limitation on that? Where am I supposed to get
    this kind of information? Thank you everybody.

  • Hariprasad Govardhanam

    #2
    Re: the maximum memory size allowed in malloc

    Hello,
    It depends on the platform that the program is working on like
    windows, linux or mac. I THINK that the amount of memory is not limited
    by anything, but physical memory. Although physical memory is extended
    by Virtual memory, malloc allocates contiguous memory (meaning side by
    side). So, it depends on the way platform handles the request.
    Saying this, I mention that I just think that is the answer. I
    hope some great programmers answer your question.

    Comment

    • those who know me have no need of my name

      #3
      Re: the maximum memory size allowed in malloc

      in comp.lang.c i read:
      [color=blue]
      >I am wondering what is the maximum size of memory that malloc() could
      >handle. Is there any limitation on that? Where am I supposed to get
      >this kind of information? Thank you everybody.[/color]

      malloc's argument is a size_t and the range of that type is [0,SIZE_MAX],
      so the maximum you can *request* is SIZE_MAX, which value varies from
      implementation to implementation and is defined in <limits.h>. whether a
      request for SIZE_MAX bytes will succeed depends on factors outside of the
      scope of this group.

      --
      a signature

      Comment

      • Mike Wahler

        #4
        Re: the maximum memory size allowed in malloc

        "Jerry" <zhiweiwang@gma il.com> wrote in message
        news:1106682835 .250935.185710@ c13g2000cwb.goo glegroups.com.. .[color=blue]
        > I am wondering what is the maximum size of memory that malloc() could
        > handle.[/color]

        The largest number (in bytes) representable by
        standard type 'size_t' (declared by <stdlib.h>
        and other headers). This value can and does
        vary among implementations . Note that this
        value isn't necessarily as large as the host
        platform's available memory.
        [color=blue]
        > Is there any limitation on that?[/color]

        See above.
        [color=blue]
        > Where am I supposed to get
        > this kind of information?[/color]

        How about a C textbook? Or the ISO C standard?

        -Mike


        Comment

        • Mike Wahler

          #5
          Re: the maximum memory size allowed in malloc


          "Hariprasad Govardhanam" <hariprasadgova rdhanam@gmail.c om> wrote in message
          news:1106683704 .656670.133010@ f14g2000cwb.goo glegroups.com.. .[color=blue]
          > Hello,
          > It depends on the platform that the program is working on like
          > windows, linux or mac. I THINK that the amount of memory is not limited
          > by anything, but physical memory.[/color]

          No, it's limited by the range of type 'size_t'.
          [color=blue]
          > Although physical memory is[/color]

          Might be.
          [color=blue]
          > extended
          > by Virtual memory,[/color]

          Not all platforms feature 'virtual memory'. C has
          no concept of virtual memory.
          [color=blue]
          >malloc allocates contiguous memory (meaning side by
          > side).[/color]

          Correct.
          [color=blue]
          >So, it depends on the way platform handles the request.[/color]

          It depends upon the C implementation.
          [color=blue]
          > Saying this, I mention that I just think that is the answer.[/color]

          Why not find out rather than guess? :-)

          -Mike


          Comment

          • Andrey Tarasevich

            #6
            Re: the maximum memory size allowed in malloc

            Jerry wrote:[color=blue]
            > I am wondering what is the maximum size of memory that malloc() could
            > handle. Is there any limitation on that? Where am I supposed to get
            > this kind of information? Thank you everybody.[/color]

            It depends on the implementation. It is definitely not greater than
            range of type 'size_t' [0, SIZE_MAX] simply because that's the type of
            'malloc's parameter.

            Also, in a real-life implementation it is quite possible that the actual
            range of sizes 'malloc' can handle is smaller than the range if 'size_t'
            type. It is possible that an implementation aliases types 'ptrdiff_t'
            and 'size_t' to integral types with the same number of bits in value
            representation. The former type is signed and the latter is unsigned.
            This automatically means that character array size (or, more generally,
            object size) in such implementation cannot be greater than SIZE_MAX/2.
            This limitation can also be applied to the maximum size 'malloc' can
            handle (which is a logical thing to do).

            --
            Best regards,
            Andrey Tarasevich

            Comment

            • Keith Thompson

              #7
              Re: the maximum memory size allowed in malloc

              those who know me have no need of my name <not-a-real-address@usa.net > writes:[color=blue]
              > in comp.lang.c i read:
              >[color=green]
              >>I am wondering what is the maximum size of memory that malloc() could
              >>handle. Is there any limitation on that? Where am I supposed to get
              >>this kind of information? Thank you everybody.[/color]
              >
              > malloc's argument is a size_t and the range of that type is [0,SIZE_MAX],
              > so the maximum you can *request* is SIZE_MAX, which value varies from
              > implementation to implementation and is defined in <limits.h>. whether a
              > request for SIZE_MAX bytes will succeed depends on factors outside of the
              > scope of this group.[/color]

              And a given implementation won't necessarily be able to specify the
              maximum size for which malloc() will succeed. Factors that affect
              whether malloc() will succeed can be related to the system as a whole
              or just to the current program. malloc(1) might succeed in some
              circumstances and fail in others.

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
              We must do something. This is something. Therefore, we must do this.

              Comment

              • italy

                #8
                Re: the maximum memory size allowed in malloc

                Sorry if my format is a bit odd. I'm using the school's library
                computer.

                The malloc() function expects an argument of type size_t. The maxium
                size is merely the size of size_t(which is implemenation-defined, look
                at limits.h). That should answer your question. Also, you shouldn't
                allocate a lot of memory at once. Once the memory is freed, it will
                become very fragmented, which of course isn't good.

                -Adam

                Comment

                • Mike Wahler

                  #9
                  Re: the maximum memory size allowed in malloc

                  "italy" <italys@gmail.c om> wrote in message
                  news:1106692613 .711105.126630@ z14g2000cwz.goo glegroups.com.. .[color=blue]
                  > Sorry if my format is a bit odd. I'm using the school's library
                  > computer.
                  >
                  > The malloc() function expects an argument of type size_t. The maxium
                  > size is merely the size of size_t(which is implemenation-defined, look
                  > at limits.h). That should answer your question. Also, you shouldn't
                  > allocate a lot of memory at once. Once the memory is freed, it will
                  > become very fragmented, which of course isn't good.[/color]

                  Issues of 'fragmentation' are outside the scope of C,
                  but what you write above is not typical behavior of
                  common operating systems. It's typically the use
                  of many small allocations which cause fragmentation,
                  not a single large one. IOW I think you've got things
                  bass ackwards. :-)

                  -Mike


                  Comment

                  • CBFalconer

                    #10
                    Re: the maximum memory size allowed in malloc

                    Jerry wrote:[color=blue]
                    >
                    > I am wondering what is the maximum size of memory that malloc() could
                    > handle. Is there any limitation on that? Where am I supposed to get
                    > this kind of information? Thank you everybody.[/color]

                    The C90 standard guarantees that you can get at least one object 32
                    kBytes in size, and this may be static, dynamic, or automatic
                    memory. C99 guarantees at least 64 kBytes. For any higher limit,
                    see your system documentation, and the results are off-topic here.

                    In practice most systems provide considerably more memory.

                    --
                    "If you want to post a followup via groups.google.c om, don't use
                    the broken "Reply" link at the bottom of the article. Click on
                    "show options" at the top of the article, then click on the
                    "Reply" at the bottom of the article headers." - Keith Thompson


                    Comment

                    • Lawrence Kirby

                      #11
                      Re: the maximum memory size allowed in malloc

                      On Tue, 25 Jan 2005 12:08:24 -0800, Hariprasad Govardhanam wrote:
                      [color=blue]
                      > Hello,
                      > It depends on the platform that the program is working on like
                      > windows, linux or mac. I THINK that the amount of memory is not limited
                      > by anything, but physical memory.[/color]

                      On systems that use virtual memory it is more likely to relate to the size
                      of available virtual memory. There are lots of possible limitations e.g.

                      The maximum value of size_t, although there have been arguments as to
                      whether calloc() could exceed that.

                      The amount of address space the implementation can make available. That
                      involves issues of overall address spac available (limited by things like
                      pointer size) and what's the largest contiguous region of memory currently
                      available (issues of frangmentation, system configuration limits etc.)
                      [color=blue]
                      > Although physical memory is extended
                      > by Virtual memory,[/color]

                      Wrong way of thinking of it. A virtual memory system doesn't have to have
                      "extra" storage e.g. swap files to be useful. On a virtual memory system a
                      program exists completely in a virtual address space, in no sense is
                      vitual memory just tacking memory on the end of "physical" memory.
                      [color=blue]
                      > malloc allocates contiguous memory (meaning side by
                      > side).[/color]

                      From the point of view of the program. On a virtual memory system that
                      would be in the virtual address space.
                      [color=blue]
                      > So, it depends on the way platform handles the request.[/color]

                      That's true of pretty much anything.

                      C itself doesn't have anything to say about virtual memory. All it cares
                      about is whether a program executes correctly and produces the correct
                      output. If an implementation that uses virtual memory can achieve that
                      (and they can) then there's no problem.

                      Lawrence

                      Comment

                      • Lawrence Kirby

                        #12
                        Re: the maximum memory size allowed in malloc

                        On Tue, 25 Jan 2005 13:22:39 -0800, Andrey Tarasevich wrote:

                        ....
                        [color=blue]
                        > Also, in a real-life implementation it is quite possible that the actual
                        > range of sizes 'malloc' can handle is smaller than the range if 'size_t'
                        > type. It is possible that an implementation aliases types 'ptrdiff_t'
                        > and 'size_t' to integral types with the same number of bits in value
                        > representation.[/color]

                        Yes, that is typical although not required.
                        [color=blue]
                        > The former type is signed and the latter is unsigned.
                        > This automatically means that character array size (or, more generally,
                        > object size) in such implementation cannot be greater than SIZE_MAX/2.[/color]

                        No, there is no requirement that ptrdiff_t be able to represent all
                        possible object sizes, it is simply the type of the result you get when
                        you take the difference of 2 pointers. The implication of this, which
                        is one with real-world consequences, is that taking the difference of two
                        pointers to elements of the same array isn't always valid; it can
                        result in undefined behaviour.

                        Lawrence

                        Comment

                        • pete

                          #13
                          Re: the maximum memory size allowed in malloc

                          Lawrence Kirby wrote:[color=blue]
                          >
                          > On Tue, 25 Jan 2005 12:08:24 -0800, Hariprasad Govardhanam wrote:
                          >[color=green]
                          > > Hello,
                          > > It depends on the platform that the program is working on like
                          > > windows, linux or mac. I THINK that the amount of memory is not limited
                          > > by anything, but physical memory.[/color]
                          >
                          > On systems that use virtual memory it is more likely to relate to the size
                          > of available virtual memory.
                          > There are lots of possible limitations e.g.
                          >
                          > The maximum value of size_t, although there have been arguments as to
                          > whether calloc() could exceed that.
                          >
                          > The amount of address space the implementation can make available.[/color]

                          It's allowable that there may be more than ((size_t)-1) bytes
                          of memory available and that more than ((size_t)-1) calls to malloc
                          could return pointers to memory before freeing any memory.

                          --
                          pete

                          Comment

                          • Lawrence Kirby

                            #14
                            Re: the maximum memory size allowed in malloc

                            On Wed, 26 Jan 2005 15:02:27 +0000, pete wrote:
                            [color=blue]
                            > Lawrence Kirby wrote:[color=green]
                            >>
                            >> On Tue, 25 Jan 2005 12:08:24 -0800, Hariprasad Govardhanam wrote:
                            >>[color=darkred]
                            >> > Hello,
                            >> > It depends on the platform that the program is working on like
                            >> > windows, linux or mac. I THINK that the amount of memory is not limited
                            >> > by anything, but physical memory.[/color]
                            >>
                            >> On systems that use virtual memory it is more likely to relate to the size
                            >> of available virtual memory.
                            >> There are lots of possible limitations e.g.
                            >>
                            >> The maximum value of size_t, although there have been arguments as to
                            >> whether calloc() could exceed that.
                            >>
                            >> The amount of address space the implementation can make available.[/color]
                            >
                            > It's allowable that there may be more than ((size_t)-1) bytes
                            > of memory available and that more than ((size_t)-1) calls to malloc
                            > could return pointers to memory before freeing any memory.[/color]

                            I was thinking of the memory available for a single allocation, but you
                            are correct that much more memory can be available to multiple
                            allocations. This was quite typical for 16 bit x86 implementations , at
                            least in certain "memory models" that they supported.

                            Lawrence



                            Comment

                            • Chris Croughton

                              #15
                              Re: the maximum memory size allowed in malloc

                              On Wed, 26 Jan 2005 02:13:56 GMT, CBFalconer
                              <cbfalconer@yah oo.com> wrote:
                              [color=blue]
                              > The C90 standard guarantees that you can get at least one object 32
                              > kBytes in size, and this may be static, dynamic, or automatic
                              > memory. C99 guarantees at least 64 kBytes. For any higher limit,
                              > see your system documentation, and the results are off-topic here.[/color]

                              Where are those in the standard(s)? I had a long look and couldn't see
                              them.


                              Hmm, it seems that there can't be a compliant C99 implementation for
                              small processors any more (and that seems like a totally artificial
                              requirement, what in the standard requires objects of 64KB?).
                              [color=blue]
                              > In practice most systems provide considerably more memory.[/color]

                              But not necessarily available to a program, and not necessarily all in
                              one chunk.

                              Chris C

                              Comment

                              Working...