Problems with a malloc'd char* to String

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

    #16
    Re: Problems with a malloc'd char* to String

    Kevin Goodsell wrote:[color=blue]
    > p = (mytype *)malloc(n * sizeof(mytype)) ;[/color]


    Yes, if there is any chance of p changing type this might be a good
    idea. However, I don't think it would be good to do this a lot because
    then you have your namespace riddled with a lot of typedefs. There are
    work arounds, but I see it getting ugly fast. IMHO you would only want
    to do this if your 'p' has a wide scope and gets allocated a lot, which
    isn't too terribly often since we have classes like std::vector to
    protect us from that need.

    Of course we also have templates so I think it rather rare that you
    would need to use a malloc where you are unaware of what the type is
    going to be in which you need to know. But your point is well taken and
    should be kept in mind for those rare cases.

    NR

    Comment

    • llewelly

      #17
      Re: Problems with a malloc'd char* to String

      llewelly <llewelly.at@xm ission.dot.com> writes:
      [color=blue]
      > Default User <first.last@com pany.com> writes:
      >[color=green]
      >> Kevin Goodsell wrote:[color=darkred]
      >>>
      >>> Default User wrote:[/color]
      >>[color=darkred]
      >>> >>That's true, assuming that you use malloc() correctly. It just so
      >>> >>happens that people use it incorrectly quite often. 'new' avoids many of
      >>> >>the problems people have with malloc().
      >>> >
      >>> >
      >>> > Mainly due to typechecking, or lack thereof.
      >>>
      >>> The size calculation, also. People get that wrong quite often.[/color]
      >>
      >> If you use the handy-dandy comp.lang.c method, it's harder to screw up:
      >>
      >> char *str;
      >>
      >> str = (char*) malloc (num * sizeof *str);[/color]
      >
      > Funny you should demo it with the one type with which it is usually a
      > mistake. Most people point a char* at a malloc'd block to have
      > dynamicly sized string. That block, if used for a string, would be
      > a very short string. ('\0' terminator only.)[/color]

      Funny I completely missed 'num' ... *sigh*

      Comment

      • Default User

        #18
        Re: Problems with a malloc'd char* to String

        Kevin Goodsell wrote:
        [color=blue]
        > Now, the comp.lang.c-approved method for mallocing is not quite what
        > Brian posted, it's actually this:
        >
        > // given:
        > char *str;
        >
        > str = malloc(n * sizeof *str);[/color]

        Yeah, but I did want to post something topical for CLC++ :)

        It works a lot better in C case, as you mention later.


        All in all, use new in C++. It's shorter and it's type safe. For a POD
        type, a good compiler will likely produce virtually identical code
        anyway.



        Brian Rodenborn

        Comment

        Working...