Test if pointer points to allocated memory

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

    #16
    Re: Test if pointer points to allocated memory

    In article <6d1334df.04012 12316.5b824cab@ posting.google. com>,
    rahul_dev_agg@h otmail.com (rahul dev) wrote:
    [color=blue]
    > malloc() library function uses brk() and sbrk() to increase the
    > address space of a process.[/color]

    Does it? I always thought it called NewPtr (). Oh, I see, you are
    talking about completely system dependent behavior that only a complete
    idiot would rely on in portable code...
    [color=blue]
    > sbrk() is a system call that ask
    > the kernel to allocate space at the end of "data segment" of
    > the process.[/color]

    Since I use a machine that doesn't have sbrk(), or a kernel, or a data
    segment, of use could that information be?
    [color=blue]
    > So, the address returned by malloc() should always
    > be greater than the end of data segment.
    > Now, the end of data segment may be found by printing the address
    > of the symbol "end" or the address of the symbol "_end". These
    > symbols are defined with the appropriate values by the linker.
    > Or you may use "objdump" to find out the addresses of data segments
    > of your executable.
    >
    > -rd[/color]

    Comment

    • Jared Dykstra

      #17
      Re: Test if pointer points to allocated memory

      CBFalconer <cbfalconer@yah oo.com> wrote in message news:<400EC3AD. 78816531@yahoo. com>...[color=blue]
      > Andrew wrote:[color=green]
      > >
      > > Is there anyway to test if a pointer points to allocated memory or
      > > not?
      > > For example if I have a pointer such as char *p is there a standard
      > > way to test whether an assignment such as the following has been
      > > applied?
      > > p = (char *) malloc(sizeof(c har) * n);[/color]
      >
      > Don't cast malloc. It is unnecessary and hides errors.
      >
      > To all practical purposes, your answer is no. However if you
      > execute "free(p);" and the program goes BOOM you can be fairly
      > sure that p was not malloced, or has already been freed. I'm sure
      > this is a great help and comfort to you.[/color]

      Often free()ing an invalid pointer will not cause a runtime error
      immediately. Most of the time you will not encounter an error until
      the following malloc()

      CBFalconer's solution undefined and dangerous territory in which to
      tread.

      ---
      Jared Dykstra

      Comment

      • Joona I Palaste

        #18
        Re: Test if pointer points to allocated memory

        Christian Bau <christian.bau@ cbau.freeserve. co.uk> scribbled the following:[color=blue]
        > In article <6d1334df.04012 12316.5b824cab@ posting.google. com>,
        > rahul_dev_agg@h otmail.com (rahul dev) wrote:[color=green]
        >> malloc() library function uses brk() and sbrk() to increase the
        >> address space of a process.[/color][/color]
        [color=blue]
        > Does it? I always thought it called NewPtr (). Oh, I see, you are
        > talking about completely system dependent behavior that only a complete
        > idiot would rely on in portable code...[/color]
        [color=blue][color=green]
        >> sbrk() is a system call that ask
        >> the kernel to allocate space at the end of "data segment" of
        >> the process.[/color][/color]
        [color=blue]
        > Since I use a machine that doesn't have sbrk(), or a kernel, or a data
        > segment, of use could that information be?[/color]

        Interesting, what sort of computer are you using that doesn't have a
        kernel?

        --
        /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
        \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
        "You could take his life and..."
        - Mirja Tolsa

        Comment

        • Richard Bos

          #19
          Re: Test if pointer points to allocated memory

          rahul_dev_agg@h otmail.com (rahul dev) wrote:
          [color=blue][color=green]
          > > Andrew wrote:
          > >[color=darkred]
          > > > Is there anyway to test
          > > > if a pointer points to allocated memory or not?[/color][/color][/color]

          No. That is, a null pointer is guaranteed not to point to any object;
          but if a pointer is non-null, there's no way of finding out whether it
          is valid, or if so, what kind of memory it points at.
          [color=blue]
          > malloc() library function uses brk() and sbrk() to increase the
          > address space of a process.[/color]

          You don't know that. Maybe on your system they do; all the ISO C
          Standard requires is that malloc() attempts to get some memory for the
          caller, by whatever means the implementor's author thought best.

          In fact, all your comments are highly system-specific.

          Richard

          Comment

          • August Derleth

            #20
            Re: Test if pointer points to allocated memory

            Joona I Palaste wrote:
            [color=blue]
            > Christian Bau <christian.bau@ cbau.freeserve. co.uk> scribbled the following:
            >[color=green]
            >>In article <6d1334df.04012 12316.5b824cab@ posting.google. com>,
            >> rahul_dev_agg@h otmail.com (rahul dev) wrote:
            >>[color=darkred]
            >>>malloc() library function uses brk() and sbrk() to increase the
            >>>address space of a process.[/color][/color]
            >
            >[color=green]
            >>Does it? I always thought it called NewPtr (). Oh, I see, you are
            >>talking about completely system dependent behavior that only a complete
            >>idiot would rely on in portable code...[/color]
            >
            >[color=green][color=darkred]
            >>>sbrk() is a system call that ask
            >>>the kernel to allocate space at the end of "data segment" of
            >>>the process.[/color][/color]
            >
            >[color=green]
            >>Since I use a machine that doesn't have sbrk(), or a kernel, or a data
            >>segment, of use could that information be?[/color]
            >
            >
            > Interesting, what sort of computer are you using that doesn't have a
            > kernel?
            >[/color]

            Could be running an embedded system, where libc handles things right
            down to the bare metal.

            Could be running PC-DOS, where the `kernel' is simply a program loader
            and an interrupt handler. (Calling something so simple a kernel now
            would get one laughed out of the industry. ;) )

            Could be running an exokernel system, where the process can request
            specific addresses in RAM from an extremely minimal resource-protection
            procedure that is loosely called a kernel. Comparing an exokernel system
            to a monolithic kernel or even a microkernel is an exercise in
            stretching definitions, perhaps beyond their breaking point.

            The point being, as I'm sure you know, that odd systems exist
            /everywhere/ and that Standard C is a good way to handle the complexity
            (mainly because it allows us to forget about such things).

            --
            My address is yvoregnevna gjragl-guerr gjb-gubhfnaq guerr ng lnubb qbg pbz
            Note: Rot13 and convert spelled-out numbers to numerical equivalents.


            Comment

            • CBFalconer

              #21
              Re: Test if pointer points to allocated memory

              rahul dev wrote:[color=blue]
              > <E.Robert.Tisda le@jpl.nasa.gov > wrote[color=green]
              > > Andrew wrote:
              > >[color=darkred]
              > > > Is there anyway to test
              > > > if a pointer points to allocated memory or not?
              > > > For example, if I have a pointer such as char *p
              > > > is there a standard way to test whether an assignment
              > > > such as the following has been applied?[/color]
              > >[color=darkred]
              > > > char* p = (char*)malloc(s izeof(char)*n);[/color]
              > >
              > > if (p > (char*)(&p)) {
              > > // p probably points to a character in automatic storage
              > > // (the program stack)
              > > }
              > > else {
              > > // p probably points to static data or free storage
              > > }
              > >
              > > But, of course, the ANSI/ISO C standards do *not* specify this.[/color]
              >
              > malloc() library function uses brk() and sbrk() to increase the
              > address space of a process. sbrk() is a system call that ask
              > the kernel to allocate space at the end of "data segment" of
              > the process. So, the address returned by malloc() should always
              > be greater than the end of data segment.
              > Now, the end of data segment may be found by printing the address
              > of the symbol "end" or the address of the symbol "_end". These
              > symbols are defined with the appropriate values by the linker.
              > Or you may use "objdump" to find out the addresses of data segments
              > of your executable.[/color]

              This sort of answer illustrates the danger of allowing Trollsdale
              posts to go unchallenged. brk/sbrk are not part of the C
              standard, and their actions (if present) are non-portable and thus
              immaterial on c.l.c. Everybody should be aware that ANYTHING
              written by Tisdale aka Trollsdale is immediately suspect and
              probably nonsense.

              --
              Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
              Available for consulting/temporary embedded and systems.
              <http://cbfalconer.home .att.net> USE worldnet address!


              Comment

              • Joona I Palaste

                #22
                Re: Test if pointer points to allocated memory

                CBFalconer <cbfalconer@yah oo.com> scribbled the following:[color=blue]
                > rahul dev wrote:[color=green]
                >> <E.Robert.Tisda le@jpl.nasa.gov > wrote[color=darkred]
                >> > Andrew wrote:
                >> > > Is there anyway to test
                >> > > if a pointer points to allocated memory or not?
                >> > > For example, if I have a pointer such as char *p
                >> > > is there a standard way to test whether an assignment
                >> > > such as the following has been applied?
                >> >
                >> > > char* p = (char*)malloc(s izeof(char)*n);
                >> >
                >> > if (p > (char*)(&p)) {
                >> > // p probably points to a character in automatic storage
                >> > // (the program stack)
                >> > }
                >> > else {
                >> > // p probably points to static data or free storage
                >> > }
                >> >
                >> > But, of course, the ANSI/ISO C standards do *not* specify this.[/color]
                >>
                >> malloc() library function uses brk() and sbrk() to increase the
                >> address space of a process. sbrk() is a system call that ask
                >> the kernel to allocate space at the end of "data segment" of
                >> the process. So, the address returned by malloc() should always
                >> be greater than the end of data segment.
                >> Now, the end of data segment may be found by printing the address
                >> of the symbol "end" or the address of the symbol "_end". These
                >> symbols are defined with the appropriate values by the linker.
                >> Or you may use "objdump" to find out the addresses of data segments
                >> of your executable.[/color][/color]
                [color=blue]
                > This sort of answer illustrates the danger of allowing Trollsdale
                > posts to go unchallenged. brk/sbrk are not part of the C
                > standard, and their actions (if present) are non-portable and thus
                > immaterial on c.l.c. Everybody should be aware that ANYTHING
                > written by Tisdale aka Trollsdale is immediately suspect and
                > probably nonsense.[/color]

                It was rahul dev, not Trollsdale, who wrote all that crap about brk/
                sbrk and "_end" symbols.

                --
                /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
                \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
                "I am looking for myself. Have you seen me somewhere?"
                - Anon

                Comment

                • Dan Pop

                  #23
                  Re: Test if pointer points to allocated memory

                  In <buo3g3$dd7$1@o ravannahka.hels inki.fi> Joona I Palaste <palaste@cc.hel sinki.fi> writes:
                  [color=blue]
                  >Christian Bau <christian.bau@ cbau.freeserve. co.uk> scribbled the following:
                  >[color=green]
                  >> Since I use a machine that doesn't have sbrk(), or a kernel, or a data
                  >> segment, of use could that information be?[/color]
                  >
                  >Interesting, what sort of computer are you using that doesn't have a
                  >kernel?[/color]

                  Most computers don't have a kernel (they are very small chips used in
                  embedded control applications; your PC probably contains several of them).

                  OTOH, they don't have hosted C implementations , either, so Christian's
                  point about malloc on kernel-less computers is moot: freestanding
                  implementations need not provide malloc at all.

                  Dan
                  --
                  Dan Pop
                  DESY Zeuthen, RZ group
                  Email: Dan.Pop@ifh.de

                  Comment

                  • pete

                    #24
                    Re: Test if pointer points to allocated memory

                    E. Robert Tisdale wrote:[color=blue]
                    >
                    > Jack Klein wrote:
                    >[color=green]
                    > > E. Robert Tisdale wrote:
                    > >
                    > > Undefined behavior.
                    > > p and &p are not pointers to the same object
                    > > or one past the same object or array.[/color]
                    >
                    > Please elaborate. Why does that make a difference?
                    >[color=green]
                    > >
                    > >[color=darkred]
                    > >> // p probably points to a character in automatic storage
                    > >> // (the program stack)[/color]
                    > >
                    > > Not all processors have a stack.[/color]
                    >
                    > But they all have "automatic storage".
                    > The typical implementation of automatic storage is on the program stack.
                    >[color=green]
                    > > Even for those that do, you are
                    > > you are making the unwarranted and unproven assumption that
                    > > "the stack" resides at higher memory addresses than other areas.
                    > > I know of several architectures
                    > > where processor hardware requires that the stack be in low memory.[/color]
                    >
                    > Name one.
                    >[color=green][color=darkred]
                    > >> }
                    > >> else {
                    > >> // p probably points to static data or free storage
                    > >> }[/color]
                    > >
                    > > Or is uninitialized. Or null.
                    > > Or points to allocated memory that has been free.
                    > >[color=darkred]
                    > >>But, of course, the ANSI/ISO C standards do *not* specify this.[/color]
                    > >
                    > > Neither does anyone with any sense, you included.[/color]
                    >
                    > I don't know whether I have any sense or not.
                    > But my assertion is easily tested:
                    >[color=green]
                    > > cat main.c[/color]
                    > #include <stdio.h>
                    > #include <stdlib.h>
                    >
                    > int main(int argc, char* argv[]) {
                    > //char c;
                    > //char* p = &c;
                    > char* p = (char*)malloc(s izeof(char));
                    > if (p > (char*)(&p)) {
                    > fprintf(stdout, "p probably points to a character "
                    > "in automatic storage.\n");
                    > }[/color]

                    C:\Program Files\DevStudio \SharedIDE\bin\ Debug>new
                    p probably points to a character in automatic storage.

                    --
                    pete

                    Comment

                    • CBFalconer

                      #25
                      Re: Test if pointer points to allocated memory

                      Joona I Palaste wrote:[color=blue]
                      > CBFalconer <cbfalconer@yah oo.com> scribbled the following:[color=green]
                      > > rahul dev wrote:[color=darkred]
                      > >> <E.Robert.Tisda le@jpl.nasa.gov > wrote
                      > >> > Andrew wrote:[/color][/color]
                      >[color=green][color=darkred]
                      > >> > > Is there anyway to test
                      > >> > > if a pointer points to allocated memory or not?
                      > >> > > For example, if I have a pointer such as char *p
                      > >> > > is there a standard way to test whether an assignment
                      > >> > > such as the following has been applied?
                      > >> >
                      > >> > > char* p = (char*)malloc(s izeof(char)*n);
                      > >> >
                      > >> > if (p > (char*)(&p)) {
                      > >> > // p probably points to a character in automatic storage
                      > >> > // (the program stack)
                      > >> > }
                      > >> > else {
                      > >> > // p probably points to static data or free storage
                      > >> > }
                      > >> >
                      > >> > But, of course, the ANSI/ISO C standards do *not* specify this.
                      > >>
                      > >> malloc() library function uses brk() and sbrk() to increase the
                      > >> address space of a process. sbrk() is a system call that ask
                      > >> the kernel to allocate space at the end of "data segment" of
                      > >> the process. So, the address returned by malloc() should always
                      > >> be greater than the end of data segment.
                      > >> Now, the end of data segment may be found by printing the address
                      > >> of the symbol "end" or the address of the symbol "_end". These
                      > >> symbols are defined with the appropriate values by the linker.
                      > >> Or you may use "objdump" to find out the addresses of data segments
                      > >> of your executable.[/color][/color]
                      >[color=green]
                      > > This sort of answer illustrates the danger of allowing Trollsdale
                      > > ^^^^^^^^^^^^^^^ ^^^^
                      > > posts to go unchallenged. brk/sbrk are not part of the C
                      > > standard, and their actions (if present) are non-portable and thus
                      > > immaterial on c.l.c. Everybody should be aware that ANYTHING
                      > > written by Tisdale aka Trollsdale is immediately suspect and
                      > > probably nonsense.[/color]
                      >
                      > It was rahul dev, not Trollsdale, who wrote all that crap about brk/
                      > sbrk and "_end" symbols.[/color]

                      But he was triggered by the Trollsdale junk. I consider him a
                      poor sucker trolled by Trollsdale. See underlined.

                      --
                      Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
                      Available for consulting/temporary embedded and systems.
                      <http://cbfalconer.home .att.net> USE worldnet address!

                      Comment

                      • Dan Pop

                        #26
                        Re: Test if pointer points to allocated memory

                        In <FrOPb.14946$xu 6.7032@fe02.use netserver.com> August Derleth <see@sig.now> writes:
                        [color=blue]
                        >Could be running PC-DOS, where the `kernel' is simply a program loader
                        >and an interrupt handler. (Calling something so simple a kernel now
                        >would get one laughed out of the industry. ;) )[/color]

                        Nope, it is a lot more than that. It provides many of the services
                        offered by the kernels of more sophisticated OSs; it's just that many
                        of them are extremely poorly implemented (console I/O very slow, serial
                        I/O unreliable at "high" speeds because is not interrupt driven) and
                        others are designed with a complete lack of security concerns in mind.

                        Actually version 2 was not so bad, given the hardware it was supposed to
                        run on. Things started to get bad by the time IBM introduced the AT/286
                        but the OS kept treating the machine as a fast 8086 and became absolutely
                        ludicrous when the AT/386 was treated as an even faster 8086 by the OS.
                        Yet, the OS design remained unchanged until its official death, in the
                        Pentium I era.

                        Dan
                        --
                        Dan Pop
                        DESY Zeuthen, RZ group
                        Email: Dan.Pop@ifh.de

                        Comment

                        • Dan Pop

                          #27
                          Re: Test if pointer points to allocated memory

                          In <400FADAA.6C0C5 1E3@yahoo.com> CBFalconer <cbfalconer@yah oo.com> writes:
                          [color=blue]
                          >rahul dev wrote:[color=green]
                          >> <E.Robert.Tisda le@jpl.nasa.gov > wrote[color=darkred]
                          >> > Andrew wrote:
                          >> >
                          >> > > Is there anyway to test
                          >> > > if a pointer points to allocated memory or not?
                          >> > > For example, if I have a pointer such as char *p
                          >> > > is there a standard way to test whether an assignment
                          >> > > such as the following has been applied?
                          >> >
                          >> > > char* p = (char*)malloc(s izeof(char)*n);
                          >> >
                          >> > if (p > (char*)(&p)) {
                          >> > // p probably points to a character in automatic storage
                          >> > // (the program stack)
                          >> > }
                          >> > else {
                          >> > // p probably points to static data or free storage
                          >> > }
                          >> >
                          >> > But, of course, the ANSI/ISO C standards do *not* specify this.[/color]
                          >>
                          >> malloc() library function uses brk() and sbrk() to increase the
                          >> address space of a process. sbrk() is a system call that ask
                          >> the kernel to allocate space at the end of "data segment" of
                          >> the process. So, the address returned by malloc() should always
                          >> be greater than the end of data segment.
                          >> Now, the end of data segment may be found by printing the address
                          >> of the symbol "end" or the address of the symbol "_end". These
                          >> symbols are defined with the appropriate values by the linker.
                          >> Or you may use "objdump" to find out the addresses of data segments
                          >> of your executable.[/color]
                          >
                          >This sort of answer illustrates the danger of allowing Trollsdale
                          >posts to go unchallenged. brk/sbrk are not part of the C
                          >standard, and their actions (if present) are non-portable and thus
                          >immaterial on c.l.c. Everybody should be aware that ANYTHING
                          >written by Tisdale aka Trollsdale is immediately suspect and
                          >probably nonsense.[/color]

                          By what kind of logic can Trollsdale be made responsible for the
                          nonsense written by other people, rahul dev, in this case?

                          C'mon, if you engage your brain, I'm sure you can tell the difference
                          between the lines prefixed by one '>' and those with no prefix at all.
                          You may even be able to decode the meaning of the line:

                          <E.Robert.Tisda le@jpl.nasa.gov > wrote

                          appearing at the beginning of a post (right after the headers): it
                          means that all the lines prefixed by a single '>' have been written
                          by Trollsdale, while those with no prefix have been written by the
                          poster whose name appears in the From header of the post (and in the
                          signature block, if present).

                          Dan
                          --
                          Dan Pop
                          DESY Zeuthen, RZ group
                          Email: Dan.Pop@ifh.de

                          Comment

                          • Dan Pop

                            #28
                            Re: Test if pointer points to allocated memory

                            In <400FC96A.459B6 779@yahoo.com> CBFalconer <cbfalconer@yah oo.com> writes:
                            [color=blue]
                            >Joona I Palaste wrote:[color=green]
                            >> CBFalconer <cbfalconer@yah oo.com> scribbled the following:[color=darkred]
                            >> > rahul dev wrote:
                            >> >> <E.Robert.Tisda le@jpl.nasa.gov > wrote
                            >> >> > Andrew wrote:[/color]
                            >>[color=darkred]
                            >> >> > > Is there anyway to test
                            >> >> > > if a pointer points to allocated memory or not?
                            >> >> > > For example, if I have a pointer such as char *p
                            >> >> > > is there a standard way to test whether an assignment
                            >> >> > > such as the following has been applied?
                            >> >> >
                            >> >> > > char* p = (char*)malloc(s izeof(char)*n);
                            >> >> >
                            >> >> > if (p > (char*)(&p)) {
                            >> >> > // p probably points to a character in automatic storage
                            >> >> > // (the program stack)
                            >> >> > }
                            >> >> > else {
                            >> >> > // p probably points to static data or free storage
                            >> >> > }
                            >> >> >
                            >> >> > But, of course, the ANSI/ISO C standards do *not* specify this.
                            >> >>
                            >> >> malloc() library function uses brk() and sbrk() to increase the
                            >> >> address space of a process. sbrk() is a system call that ask
                            >> >> the kernel to allocate space at the end of "data segment" of
                            >> >> the process. So, the address returned by malloc() should always
                            >> >> be greater than the end of data segment.
                            >> >> Now, the end of data segment may be found by printing the address
                            >> >> of the symbol "end" or the address of the symbol "_end". These
                            >> >> symbols are defined with the appropriate values by the linker.
                            >> >> Or you may use "objdump" to find out the addresses of data segments
                            >> >> of your executable.[/color]
                            >>[color=darkred]
                            >> > This sort of answer illustrates the danger of allowing Trollsdale
                            >> > ^^^^^^^^^^^^^^^ ^^^^
                            >> > posts to go unchallenged. brk/sbrk are not part of the C
                            >> > standard, and their actions (if present) are non-portable and thus
                            >> > immaterial on c.l.c. Everybody should be aware that ANYTHING
                            >> > written by Tisdale aka Trollsdale is immediately suspect and
                            >> > probably nonsense.[/color]
                            >>
                            >> It was rahul dev, not Trollsdale, who wrote all that crap about brk/
                            >> sbrk and "_end" symbols.[/color]
                            >
                            >But he was triggered by the Trollsdale junk. I consider him a
                            >poor sucker trolled by Trollsdale. See underlined.[/color]

                            A perfect non-sequitur. Your own comments apply to the text posted by
                            rahul dev and there is no way to blame that text on Trollsdale's advice,
                            especially considering that he was explicitly saying that it was *not*
                            based on the C standard.

                            Your lame attempt to save your ass from your previous mistake fools no
                            one. Believe me, there is no substitute for engaging your brain before
                            posting.

                            Dan
                            --
                            Dan Pop
                            DESY Zeuthen, RZ group
                            Email: Dan.Pop@ifh.de

                            Comment

                            • David Resnick

                              #29
                              Re: Test if pointer points to allocated memory

                              andrew.zdenek@c a.com (Andrew) wrote in message news:<6536dc17. 0401210825.5428 3155@posting.go ogle.com>...[color=blue]
                              > Is there anyway to test if a pointer points to allocated memory or
                              > not?
                              > For example if I have a pointer such as char *p is there a standard
                              > way to test whether an assignment such as the following has been
                              > applied?
                              > p = (char *) malloc(sizeof(c har) * n);[/color]

                              Well, if you need to do this, there is one portable option. You could
                              "wrap" malloc, calloc, realloc, and free, and keep track of
                              the things allocated/freed. Of course, this only works for things
                              allocated under your control, which may not always be true of
                              all dynamically allocated memory you encounter (e.g. stuff from
                              libraries not owned by you). Anyway, the following might work
                              for you:

                              example program fragment, assumes you have some static hash
                              table accessed through the hash functions which store or
                              retrieve pointer values.

                              void *my_malloc(size _t size)
                              {
                              void *tmp = malloc(size);

                              if (tmp != NULL) {
                              hash_enter(tmp) ;
                              }

                              return tmp;
                              }

                              void my_free(void *ptr)
                              {
                              if (ptr != NULL) {
                              hash_remove(ptr );
                              free(ptr);
                              }
                              }

                              int ptr_was_allocat ed(void *ptr)
                              {
                              if (ptr != NULL && hash_contains(p tr)) {
                              return 1;
                              }
                              else {
                              return 0;
                              }
                              }

                              If the above doesn't suit you, you are out of luck in portable
                              C AFAIK. But if you are willing to use platform specific extensions,
                              you may have some options. If so, ask in a group dedicated to
                              your platform. Or you could leave such checking of
                              pointers out of your code, and use the tools available for your
                              platform while testing to detect invalid memory accesses -- for example
                              valgrind/electric fence/bounds checker/Purify/etc.

                              Good luck.

                              -David

                              Comment

                              • August Derleth

                                #30
                                Re: Test if pointer points to allocated memory

                                Dan Pop wrote:
                                [color=blue]
                                > In <FrOPb.14946$xu 6.7032@fe02.use netserver.com> August Derleth <see@sig.now> writes:
                                >
                                >[color=green]
                                >>Could be running PC-DOS, where the `kernel' is simply a program loader
                                >>and an interrupt handler. (Calling something so simple a kernel now
                                >>would get one laughed out of the industry. ;) )[/color]
                                >
                                >
                                > Nope, it is a lot more than that. It provides many of the services
                                > offered by the kernels of more sophisticated OSs; it's just that many
                                > of them are extremely poorly implemented (console I/O very slow, serial
                                > I/O unreliable at "high" speeds because is not interrupt driven) and
                                > others are designed with a complete lack of security concerns in mind.[/color]

                                Hm. I suppose I knew that (I do assembly-level DOS programming on
                                occasion, nearly always for 80386s or higher), but I tried to subsume it
                                under the rubric of `interrupt handler'. In other words, I was being
                                somewhat facetious and trying to imply that anything like PC-DOS,
                                MS-DOS, or DR-DOS (or any of the lesser-known clones) is /not/ a Real OS
                                by my (Unix-informed) standard. ;)

                                It's also interesting to hear just how broken PC-DOS truly was. Wasn't
                                that IBM's in-house project? No wonder they embraced Linux so quickly:
                                Moderately code must make them downright ecstatic, when compared to what
                                they'd have done on their own. ;)
                                [color=blue]
                                >
                                > Actually version 2 was not so bad, given the hardware it was supposed to
                                > run on. Things started to get bad by the time IBM introduced the AT/286
                                > but the OS kept treating the machine as a fast 8086 and became absolutely
                                > ludicrous when the AT/386 was treated as an even faster 8086 by the OS.
                                > Yet, the OS design remained unchanged until its official death, in the
                                > Pentium I era.[/color]

                                Bah. Bogosity and cruft, taken to an absurd degree. I can understand
                                (maybe) not using the half-baked extra features introduced in the 80286,
                                but not jumping with both feet on the 80386's 32-bit addressing and
                                general strides in the direction of becoming a Real Architecture?
                                Mind-boggling.

                                (I consider the 8086 to be an abortion of a design, and the 80386
                                Intel's semi-awakening to the world of ISA sanity. Nothing as pretty as
                                a VAX or even a Motorola 68000, but not nearly as painful to program
                                assembly for.)
                                [color=blue]
                                >
                                > Dan[/color]


                                --
                                My address is yvoregnevna gjragl-guerr gjb-gubhfnaq guerr ng lnubb qbg pbz
                                Note: Rot13 and convert spelled-out numbers to numerical equivalents.


                                Comment

                                Working...