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
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