Malloc

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

    Malloc

    Is this code correct

    char *str = (char *)malloc(10 * sizeof(char));
    char[0] = ...
    char [1] = ...
    ....
    ....
    ...
    char [9] = '\0';

    malloc() does not make any guarantees regarding the contiguity of
    memory. In that case, is ths code correct? Can we assume that the
    characters are contiguous in memory.

  • Ico

    #2
    Re: Malloc

    pavan <pavan.mail@gma il.comwrote:
    malloc() does not make any guarantees regarding the contiguity of
    memory.
    Why do you think this is the case ?

    --
    :wq
    ^X^Cy^K^X^C^C^C ^C

    Comment

    • pavan

      #3
      Re: Malloc

      On Feb 3, 11:15 am, Ico <use...@zeev.nl wrote:
      pavan <pavan.m...@gma il.comwrote:
      malloc() does not make any guarantees regarding the contiguity of
      memory.
      >
      Why do you think this is the case ?
      >
      --
      :wq
      ^X^Cy^K^X^C^C^C ^C

      if i create a chunk of memory containing a number of elements of the
      same type, i want to know if i can access the elements as ptr, ptr+1,
      ptr+2 etc... Should I use vmalloc() instead of malloc if i want to do
      this?

      Comment

      • santosh

        #4
        Re: Malloc

        pavan wrote:
        On Feb 3, 11:15 am, Ico <use...@zeev.nl wrote:
        pavan <pavan.m...@gma il.comwrote:
        malloc() does not make any guarantees regarding the contiguity of
        memory.
        Why do you think this is the case ?
        >
        if i create a chunk of memory containing a number of elements of the
        same type, i want to know if i can access the elements as ptr, ptr+1,
        ptr+2 etc...
        Yes, you can.
        Should I use vmalloc() instead of malloc if i want to do this?
        No. vmalloc() is a non-standard function. malloc() is fine.

        Comment

        • pavan

          #5
          Re: Malloc

          Does this mean that malloc returns memory that is contiguous in
          virtual address space?

          Comment

          • santosh

            #6
            Re: Malloc

            pavan wrote:
            Is this code correct
            >
            char *str = (char *)malloc(10 * sizeof(char));
            char[0] = ...
            char [1] = ...
            I hope you mean str[0], str[1] etc.? char is a type. Types are not
            directly usable. You must have an object of a type, to use it.
            ...
            ...
            ..
            char [9] = '\0';
            >
            malloc() does not make any guarantees regarding the contiguity of
            memory.
            Where do get this nonsense from. A chunk of memory returned by
            malloc() calloc() or realloc() can be regarded as an array, which is
            by definition contiguous.
            In that case, is ths code correct? Can we assume that the
            characters are contiguous in memory.
            Yes. Elements in a block allocated by a single call to *alloc() can be
            regarded as sequential.

            Comment

            • santosh

              #7
              Re: Malloc

              pavan wrote:
              Does this mean that malloc returns memory that is contiguous in
              virtual address space?
              >From the point of view of a C program, it is contiguous. Whether it's
              so in virtual/physical/whatever memory is outside the scope of C and
              moreover, totally irrelevant to most C programs.

              Comment

              • Clark S. Cox III

                #8
                Re: Malloc

                pavan wrote:
                Is this code correct
                >
                char *str = (char *)malloc(10 * sizeof(char));
                Don't cast the result of malloc. The sizeof(char) is redundant, as
                sizeof(char) is always 1.
                char[0] = ...
                char [1] = ...
                ....
                ....
                ...
                char [9] = '\0';
                >
                malloc() does not make any guarantees regarding the contiguity of
                memory.
                Of course it does, who told you otherwise?
                In that case, is ths code correct? Can we assume that the
                characters are contiguous in memory.
                >

                --
                Clark S. Cox III
                clarkcox3@gmail .com

                Comment

                • Clark S. Cox III

                  #9
                  Re: Malloc

                  pavan wrote:
                  Does this mean that malloc returns memory that is contiguous in
                  virtual address space?
                  >
                  As far as any conforming C program is concerned, malloc returns
                  contiguous memory. Period.

                  --
                  Clark S. Cox III
                  clarkcox3@gmail .com

                  Comment

                  • Mark McIntyre

                    #10
                    Re: Malloc

                    On 3 Feb 2007 08:29:50 -0800, in comp.lang.c , "pavan"
                    <pavan.mail@gma il.comwrote:
                    >Does this mean that malloc returns memory that is contiguous in
                    >virtual address space?
                    The C language doesn't need to know anything about how the OS lays out
                    memory in any virtual address space. As far as C is concerned, any
                    memory returned by malloc is a contiguous block.

                    If your programme definitely needs to know precisely how your OS is
                    choosing and allocating memory, it may be that C is the wrong language
                    to use.


                    --
                    Mark McIntyre

                    "Debugging is twice as hard as writing the code in the first place.
                    Therefore, if you write the code as cleverly as possible, you are,
                    by definition, not smart enough to debug it."
                    --Brian Kernighan

                    Comment

                    • Richard Heathfield

                      #11
                      Re: Malloc

                      pavan said:
                      Is this code correct
                      >
                      char *str = (char *)malloc(10 * sizeof(char));
                      char[0] = ...
                      char [1] = ...
                      ...
                      ...
                      ..
                      char [9] = '\0';
                      No. Wrap it in a function, lose the cast, add a check to determine whether
                      str is NULL after the malloc call, change sizeof(char) to sizeof *str,
                      #include <stdlib.hand change char[0] etc to str[0] etc, and you'll have
                      something you can use.
                      malloc() does not make any guarantees regarding the contiguity of
                      memory.
                      "The order and contiguity of storage allocated *by successive calls*
                      to the calloc , malloc , and realloc functions is unspecified." Since malloc
                      can be used to allocate storage for any object, we may deduce that the
                      storage thus allocated in a single call is contiguous.

                      --
                      Richard Heathfield
                      "Usenet is a strange place" - dmr 29/7/1999

                      email: rjh at the above domain, - www.

                      Comment

                      • santosh

                        #12
                        Re: Malloc

                        Mark McIntyre wrote:
                        On 3 Feb 2007 08:29:50 -0800, in comp.lang.c , "pavan"
                        <pavan.mail@gma il.comwrote:
                        >
                        Does this mean that malloc returns memory that is contiguous in
                        virtual address space?
                        >
                        The C language doesn't need to know anything about how the OS lays out
                        memory in any virtual address space. As far as C is concerned, any
                        memory returned by malloc is a contiguous block.
                        >
                        If your programme definitely needs to know precisely how your OS is
                        choosing and allocating memory, it may be that C is the wrong language
                        to use.
                        Why? Most of the OSes are written in C.

                        Comment

                        • jacob.navia

                          #13
                          Re: Malloc

                          pavan a écrit :
                          Does this mean that malloc returns memory that is contiguous in
                          virtual address space?
                          >
                          Yes

                          Comment

                          • CBFalconer

                            #14
                            Re: Malloc

                            pavan wrote:
                            >
                            Is this code correct
                            >
                            char *str = (char *)malloc(10 * sizeof(char));
                            char[0] = ...
                            char [1] = ...
                            ...
                            ...
                            ..
                            char [9] = '\0';
                            >
                            malloc() does not make any guarantees regarding the contiguity of
                            memory. In that case, is ths code correct? Can we assume that the
                            characters are contiguous in memory.
                            Yes you can, but you should correct your call on malloc.
                            sizeof(char) is 1 by definition, and the only function of the cast
                            is to suppress the needed error message which will point out the
                            failure to #include <stdlib.h>. A better phrase would be:

                            char *str = malloc(10 * sizeof *str);

                            which can be retyped by changing only the "char", and will not
                            suppress any error messages.

                            --
                            <http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>

                            "A man who is right every time is not likely to do very much."
                            -- Francis Crick, co-discover of DNA
                            "There is nothing more amazing than stupidity in action."
                            -- Thomas Matthews


                            Comment

                            • Kenneth Brody

                              #15
                              Re: Malloc

                              santosh wrote:
                              >
                              Mark McIntyre wrote:
                              [...]
                              If your programme definitely needs to know precisely how your OS is
                              choosing and allocating memory, it may be that C is the wrong language
                              to use.
                              >
                              Why? Most of the OSes are written in C.
                              In clc, "the C language" means "as defined by the ISO standard".
                              More precisely, what he probably meant was "you will need to go
                              outside the scope of the standard, and use some system- specific
                              methods to do what you need". Whether that means using C for most
                              of your work, plus using some implementation-specific additions,
                              or using a different language entirely, is up to you.

                              That, and I have no idea what percentage of operating systems are
                              written in C. However, given clc's definition of C, I would
                              venture that, as far as clc is concerned, no true "operating
                              system" is, or possibly even could be, written in standard C.

                              --
                              +-------------------------+--------------------+-----------------------+
                              | Kenneth J. Brody | www.hvcomputer.com | #include |
                              | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
                              +-------------------------+--------------------+-----------------------+
                              Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>


                              Comment

                              Working...