malloc

E

Emmanuel Delahaye

Vimal Aravindashan a écrit :
Although the above does what you expect it to, I'd prefer
char *res = malloc( strlen("abcdef") * sizeof(char) );
OR
char *res = malloc( strlen("abcdef") * sizeof(*res) );

Off by one. Marc's solution was far better.

bang!
 
J

Joe Estock

Flash said:
Joe Estock wrote:


I was commenting on your comment, not the code. I use NULL myself, but
it is important to know that for all implementation 0 is also valid, not
because you should use it but because so many other people *do* use it.

Yes, however that could raise yet another debate - just because so many
other people use it, does it mean that we *should* use it? Even moreso
does it mean that we should treat it as if it were part of the standard?
More or less it's the same thing as "if everyone jumped off a bridge,
would you do it too?".
We all make that mistake on occasion :)



Due to the lack of a /n or flushing stdout there is even less guarantee
than normal that it will be output before the next function call, line
buffering being fairly normal (and allowed by the standard) on stdout.

Noted. I had forgotten about line buffering.

Around here, it would be advisable to not post non-standard C, at least
without appropriate disclaimers and pointing people else where for
discussing it, or you *will* get people pulling you up on it.

As others have suggested in the past, if one *really* wants to do
something like that then the portable solution is a loop reading
characters waiting for a newline, for anything else a non-standard
solution is required and such solutions do not belong on this group.

Understood. Next time I will correct the non-standard parts of the code
instead of skipping over them.

-Joe
 
F

Flash Gordon

Joe said:
Yes, however that could raise yet another debate - just because so many
other people use it, does it mean that we *should* use it?

That is open to debate.
> Even moreso
does it mean that we should treat it as if it were part of the standard?

That is not open to debate. It IS part of the standard.
More or less it's the same thing as "if everyone jumped off a bridge,
would you do it too?".

If I'm wearing a harness to which an appropriate bungee is attached,
possibly ;-)

In general, no, I try not to do things just because everyone else is.
 
J

Joe Estock

Flash Gordon wrote:
If I'm wearing a harness to which an appropriate bungee is attached,
possibly ;-)

In general, no, I try not to do things just because everyone else is.

Ha! Great answer. My age-old answer to that question always has (and
always will be) "it depends on how far it is from the bridge to the ground".

-Joe
 
M

Michael Wojcik

Ha! Great answer. My age-old answer to that question always has (and
always will be) "it depends on how far it is from the bridge to the ground".

Tsk. If "everyone jumped off a bridge", then by definition you and
I do as well.

If *everyone else* jumped off a bridge, I'm not sure there would be
room underneath it for me to jump off. It'd have to be a very high
bridge.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,466
Latest member
DrusillaYa

Latest Threads

Top