Array of pointers in a struct

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

    #16
    Re: Array of pointers in a struct

    fix wrote:[color=blue]
    > Barry Schwarz wrote:
    >[color=green][color=darkred]
    >>> OK, I didn't. But why does my program can still use malloca?[/color]
    >>
    >> What is malloca?[/color]
    >
    > It's a typo.[/color]

    No, it's a non-standard extension used by some systems to allocate
    memory on the stack (on systems with a stack). This has the
    advantage/flaw that the memory is automatically deallocated when
    the function is exited and that allocation can be very quick.

    --
    A: Because it fouls the order in which people normally read text.
    Q: Why is top-posting such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?


    Comment

    • Sam Dennis

      #17
      Re: Array of pointers in a struct

      CBFalconer wrote:[color=blue]
      > fix wrote:[color=green]
      >> Barry Schwarz wrote:[color=darkred]
      >>> What is malloca?[/color]
      >>
      >> It's a typo.[/color]
      >
      > No, it's a non-standard extension used by some systems to allocate
      > memory on the stack (on systems with a stack).[/color]

      That would normally be spelt `alloca', so it'd still be a typo.

      --
      ++acr@,ka"

      Comment

      • fix

        #18
        Re: Array of pointers in a struct

        Sam Dennis wrote:
        [color=blue]
        > CBFalconer wrote:
        >[color=green]
        >>fix wrote:
        >>[color=darkred]
        >>>Barry Schwarz wrote:
        >>>
        >>>>What is malloca?
        >>>
        >>>It's a typo.[/color]
        >>
        >>No, it's a non-standard extension used by some systems to allocate
        >>memory on the stack (on systems with a stack).[/color]
        >
        >
        > That would normally be spelt `alloca', so it'd still be a typo.
        >[/color]

        Yup, it is just a typo, I didn't know about those extensions.

        Comment

        • fix

          #19
          Re: Array of pointers in a struct


          Barry Schwarz wrote:
          [color=blue]
          > On Sat, 01 May 2004 21:36:06 -0500, fix <fix@here.com > wrote:
          >
          >[color=green]
          >>
          >>Barry Schwarz wrote:
          >>
          >>[color=darkred]
          >>>>OK, I didn't. But why does my program can still use malloca?
          >>>
          >>>
          >>>What is malloca?[/color]
          >>
          >>It's a typo.
          >>
          >>[color=darkred]
          >>>Your program uses malloc because you coded it to. Under C89, if you
          >>>call a function without a prototype, the compiler assumes that the
          >>>arguments are the correct type and the function returns an int. It
          >>>then generates the appropriate clues for the linker to include that
          >>>function in you program. Since the assumed return type is incorrect
          >>>for malloc, you have invoked undefined behavior.[/color]
          >>
          >>I meant, I don't include the header and can still call malloc, does the
          >>compiler know where to find the actual malloc implementation?[/color]
          >
          >
          > Go back and read the paragraph again. The compiler doesn't find the
          > actual code for malloc. The linker does.[/color]

          OK, I got it, I was just treating the thing after I wrote my program as
          "compiling" and the program does that as the "compiler".
          [color=blue]
          > And no, you cannot still call it. Any attempt to do so invokes
          > undefined behavior, regardless of whether or not the compiler
          > generates a diagnostic.
          >
          > Do you have your warning level set to max? Many compilers will
          > generate a warning that there is no prototype in scope.[/color]

          Hm...... I have written several programs without including stdlib.h, but
          I can still do malloc. I am using two compiler, one gives me the warning
          but the other does not.
          [color=blue]
          > snip
          >[color=green][color=darkred]
          >>>The standard does not specify what the operating system will do when
          >>>you program terminates. It is never wrong to clean up after your self
          >>>and on a "not well designed system" it may in fact be necessary. I
          >>>don't have a clue about whether your system will clean up your mess or
          >>>not but in this newsgroup we don't depend on system specific behavior.[/color]
          >>
          >>Wouldn't that be too hard to find out what is allocated and what is not?
          >>That'll be a pain if that is a huge program. Is there any way to get a
          >>list of memory allocated by the program and free them all when the
          >>program quits?[/color]
          >
          >
          > It's only too hard if you code your program before you design it. A
          > well designed program will have one fclose for every successful fopen
          > and one free for every successful malloc, whether explicit or implied.
          > This is one of the reasons there is more to programming than merely
          > learning the language. (Just as an aside, it is legal to free the
          > NULL pointer returned by an unsuccessful malloc but it is not legal to
          > fclose the NULL pointer returned by an unsuccessful fopen.)
          >
          > There is no standard language feature that will tell you all the areas
          > you have allocated. You have to keep track of them as you allocate
          > them. Since you normally assign the returned address to a pointer,
          > this is not as onerous as it sounds.[/color]

          I guess there is still a long time for me to learn writing "good" C
          programs. I came from lanugages that does not have pointer, and thus no
          deallocating memory. I have done a program which require me to free all
          the memory which I have allocated, but the pain comes from the nature of
          the program - it is using nested structure, and what I have to do is to
          go deep into the structure and kill the children because their parents.

          Comment

          • Barry Schwarz

            #20
            Re: Array of pointers in a struct

            On Sun, 02 May 2004 14:29:07 -0500, fix <fix@here.com > wrote:
            [color=blue]
            >
            >Barry Schwarz wrote:
            >[color=green]
            >> On Sat, 01 May 2004 21:36:06 -0500, fix <fix@here.com > wrote:[/color][/color]
            snip[color=blue][color=green][color=darkred]
            >>>I meant, I don't include the header and can still call malloc, does the
            >>>compiler know where to find the actual malloc implementation?[/color]
            >>
            >>
            >> Go back and read the paragraph again. The compiler doesn't find the
            >> actual code for malloc. The linker does.[/color]
            >
            >OK, I got it, I was just treating the thing after I wrote my program as
            >"compiling" and the program does that as the "compiler".
            >[color=green]
            >> And no, you cannot still call it. Any attempt to do so invokes
            >> undefined behavior, regardless of whether or not the compiler
            >> generates a diagnostic.
            >>
            >> Do you have your warning level set to max? Many compilers will
            >> generate a warning that there is no prototype in scope.[/color]
            >
            >Hm...... I have written several programs without including stdlib.h, but
            >I can still do malloc. I am using two compiler, one gives me the warning
            >but the other does not.[/color]

            The presence or absence of a warning is merely a quality of
            implementation issue for the compiler.

            The fact that you invoke undefined behavior but your program APPEARS
            to work is just really rotten luck. It has caused you to learn some
            things which are in fact not correct and allowed you to develop some
            bad habits that will become more difficult to correct the later you
            try.

            If you want to keep on coding undefined behavior, that is your
            privilege. Many consider repeated posting of such code the mark of a
            troll and see no point in responding to someone not interested in
            learning.

            snip



            <<Remove the del for email>>

            Comment

            • fix

              #21
              Re: Array of pointers in a struct



              Barry Schwarz wrote:[color=blue]
              > On Sun, 02 May 2004 14:29:07 -0500, fix <fix@here.com > wrote:
              >
              >[color=green]
              >>Barry Schwarz wrote:
              >>
              >>[color=darkred]
              >>>On Sat, 01 May 2004 21:36:06 -0500, fix <fix@here.com > wrote:[/color][/color]
              >
              > snip
              >[color=green][color=darkred]
              >>>>I meant, I don't include the header and can still call malloc, does the
              >>>>compiler know where to find the actual malloc implementation?
              >>>
              >>>
              >>>Go back and read the paragraph again. The compiler doesn't find the
              >>>actual code for malloc. The linker does.[/color]
              >>
              >>OK, I got it, I was just treating the thing after I wrote my program as
              >>"compiling" and the program does that as the "compiler".
              >>
              >>[color=darkred]
              >>>And no, you cannot still call it. Any attempt to do so invokes
              >>>undefined behavior, regardless of whether or not the compiler
              >>>generates a diagnostic.
              >>>
              >>>Do you have your warning level set to max? Many compilers will
              >>>generate a warning that there is no prototype in scope.[/color]
              >>
              >>Hm...... I have written several programs without including stdlib.h, but
              >>I can still do malloc. I am using two compiler, one gives me the warning
              >>but the other does not.[/color]
              >
              >
              > The presence or absence of a warning is merely a quality of
              > implementation issue for the compiler.
              >
              > The fact that you invoke undefined behavior but your program APPEARS
              > to work is just really rotten luck. It has caused you to learn some
              > things which are in fact not correct and allowed you to develop some
              > bad habits that will become more difficult to correct the later you
              > try.
              >
              > If you want to keep on coding undefined behavior, that is your
              > privilege. Many consider repeated posting of such code the mark of a
              > troll and see no point in responding to someone not interested in
              > learning.
              >
              > snip[/color]

              But well, I learnt from this. I included the header, I don't cast my
              pointers returned by malloc anymore...... Was just wondering why it works.

              Comment

              Working...