A C showstopper

R

Rui Maciel

spinoza1111 said:
You should have my email requesting that you desist from this
offensive conduct.

Are you aware of the insults you've been spreading throughout this newsgroup?


Rui Maciel
 
C

Chris M. Thomasson

I don't like this approach; firstly it doesn't
work in a freestanding environment (that doesn't
have a filesystem accessible via standard functions).
Secondly even if it does work it uses a
comparatively huge amount of work compared
to what it actually does. Nobody would use
this in production, nobody.

Fair enough. BTW, ho you happen have any suggestions on how to emulate
`asprintf()' in a pre-C99 environment without basically "re-implementing"
`printf()'?

Thanks.
 
K

Keith Thompson

Richard Heathfield said:
spinoza1111 said:
spinoza1111said:
[...]
Consider the possibility that you are mistaken.

[...]

You know he never will.

You should have my email requesting that you desist from this
offensive conduct.

You are in no position to complain about offensive conduct.

Richard, I presume you're aware of the point of my remark, namely
that *you* are in a position to improve the S/N ratio of this
newsgroup by ignoring spinoza1111. I'm at a loss to understand
why you continue to feed this troll.
 
C

Chris M. Thomasson

jacob navia said:
void LogError(char *ErrorMessage,int errorcode, char *callingFunction)
{
// Here you do not know anything about the length of the arguments
}


I guess one could do something like:
__________________________________________________________________________
void LogError(char *ErrorMessage,int errorcode, char *callingFunction)
{
char* buffer;
char int_buffer[64] = { '\0' };
int errcode_size = sprintf(int_buffer, "%d", errorcode);
size_t errmsg_size = strlen(ErrorMessage);
size_t callfunc_size = strlen(callingFunction);
size_t total_size = errmsg_size + callfunc_size + errcode_size + 6;

assert(errcode_size > -1);
buffer = malloc(total_size + 1);

if (buffer)
{
int size = sprintf(buffer,
"%s(%s): %s\n",
callingFunction,
int_buffer,
ErrorMessage);

assert(size > -1);

/* do something with `buffer' */

free(buffer);
}
}
__________________________________________________________________________
 
C

Charlton Wilbur

troll> Words, mere words, aren't "behavior".

Words are not behavior, no; but the choice of words, the writing of them
into a Usenet post, and the eventual posting of that post *are*
behavior.

troll> When a person (in Navia's case, an accomplished and
troll> intelligent person) has views that don't reinforce the low
troll> "standards" of your community, he is hounded and when he
troll> responds, you blame him for being a "drama queen".

Navia gets the reaction he does because he responds to bug reports and
disagreement as if they were personal attacks.

Charlton
 
B

Beej Jorgensen

spinoza1111 said:
"Does C have call by reference?"

In teaching, there is a simple approach, and that simple approach is
to teach this:

1. In C, all function arguments are passed "by value." [K&R2, pg 27]
2. Understanding of pointers.

"Call by reference" is extraneous at best. Keep it simple.

-Beej
 
C

Charlton Wilbur

troll> The people who complain about my "trolling" (which here means
troll> being literate above a low bound)

There are several people here who are literate above a low bound. What
marks you as a troll is not your literacy (such as it is), but your
insistence that C is a terrible language and that you are going to
demonstrate it to all of us, and your personal attacks on people who
disagree with you.

troll> You spread confusion by giving negative answers to broader
troll> questions, and unexplained answers to specific questions. You
troll> are not personally vindictive but nonetheless propagate and
troll> enable the politics of personal destruction because you have
troll> a Fundamentalist interpretation of C which insists (like
troll> Bible-thumping, God-walloping, Come to Jesus Fundamentalism
troll> with respect to Scripture) that there is only one
troll> interpretation of C.

This sort of accusation is often made by those whose grasp of technical
matters is fuzzy and inexact; someone, for instance, who cannot
distinguish between a compiler and an interpreter, and someone who
cannot distinguish between support for call by reference in the language
and a common workaround to produce the same result.

Charlton
 
J

jameskuyper

Charlton Wilbur wrote:
....
There are several people here who are literate above a low bound. What
marks you as a troll is not your literacy (such as it is), but your
insistence that C is a terrible language and that you are going to
demonstrate it to all of us, and your personal attacks on people who
disagree with you.

Saying that "X" is bad, and posting it to a newsgroup devoted to "X",
is standard trollish behavior. Even if "X" is in fact bad, and you've
got good arguments backing up that assessment, you're unlikely to
convince anyone on that newsgroup, so the only purpose served by
posting such a message is to enjoy the arguments it provokes, a
fundamentally trollish behavior pattern.

However, this guy hasn't even bothered to learn enough C to post
coherent arguments against it. There's plenty of perfectly valid
criticisms of C he could have raised - not enough (IMO) to justify
dropping C, but certainly enough to confuse the issue. However, as far
as I can tell (I only see other people's responses, since I've had him
killfiled since long before the current invasion), he doesn't seem to
have raised any of them.
 
K

Kaz Kylheku

To develop the "unlimited string" processor which I discuss in the
thread A C Adventure, I have to recreate my utilities library of 1991,
which I did because then (and now) one needs to use the printf
approach to format data: but if one needs, as one often does need, to
format to storage, sprintf has a built in danger that as far as I know
(and correct me if I'm wrong) nobody has or will fix inside of C.

The problem is that any sprintf whatsoever, insofar as it formats
strings, has no control over string length. Most code I've seen
decides to create some silly "buffer" of some silly size.

But in

sprintf(buf, "%s\n", ptr)

characters past the allocated end of "buf" may be overwritten.
The C99 solution (limit the number of characters) is extraordinarily

The alternative to not limiting the number of characters is not to do the print
at all and report that it could not be done.

C99 provides both alternatives in one. The print is truncated, and the program
can tell that this happened.
poor and reinforces my Dim View of the ethics of people in that
effort: some of them participated in the "get Schildt" campaign, and
none of them seems to have been competent working programmers.

In 1991, I implemented the GNU solution, but I don't have the code
anymore. This is asprintf; its contract is to always allocate enough
memory to hold the final string.

Always?

What if the format string dynamically specifies a huge field width?

asprintf("%*s...", INT_MAX, ...)

what if there is no memory?

What if there is an overcomitting memory allocation going on?
The space appears to be allocated, but blows up when you try to use it?

``Always'' is a big word.
There are two ways of implementing asprintf. Either iterate formatting
until everything's formatted, reallocating larger and larger blocks of
memory (and copying previously formatted output characters).

If the reallocation is exponential, then each previously formatted
character is copied a constant number of times (on average over all
of the characters ever formatted). So the copying overhead is fixed,
and you do avoid performing conversions twice.
Or make a
pass through the data without doing output to find the size needed. My
1991 solution was the former. Today, when I write the code
I shall use the latter solution.

The latter solution can be written as a trivial wrapper around the C99 snprintf
function. Start with a zero-sized buffer and call snprintf (or rather
vsnprintf). The function won't try to store any characters, but it will still
return the number of characters that would have been written. Then, allocate a
buffer which can hold that many characters and call the function again.

Here it is:

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>

char *asprintf(const char *fmt, ...)
{
int chars_written;

/* compute size without printing */
{
va_list vl;
char dummy_buf[1];
va_start (vl, fmt);
chars_written = vsnprintf(dummy_buf, 0, fmt, vl);
va_end (vl);
}

/* allocate and print */
{
va_list vl;
char *buf = malloc(chars_written + 1);
va_start (vl, fmt);

if (buf != 0)
vsnprintf(buf, chars_written + 1, fmt, vl);
va_end (vl);

return buf;
}
}
The GNU solution , not the C99 solution, is the one that occurs to the
competent programmer: the C99 solution cuts off the user at the knees
blindly and is the sort of solution that occurs to managers...not
competent programmers.

As you can see, the C99 solution can be used to implement the GNU solution,
using exactly the approach that you advocate as the one you would use today:
measure the buffer first without performing any output, allocate, then
do the output.
 
J

jameskuyper

Richard said:
Do you realize that your continued postings of this type bring
annoyance to others and reduces the S/N ratio?

If even a single one of the persons he's posted such a message to in
the past year or so had ever followed Keith's advice, the improvement
in the S/N ratio would have more than made up for the reduction caused
by all of Keith's messages of this type during that same period of
time, combined. To be perfectly fair, however, I doubt whether that
has ever happened.

The same can be said about the annoyance.
 
S

spinoza1111

Are you aware of the insults you've been spreading throughout this newsgroup?

It's called self-defense. Do your homework. People have the right to
post here without their competence being challenged on matters of
interpretation.
 
S

spinoza1111

You wrote the following sentence:

«How do you know the size of the "buffer" you do need?»

Are you intentionally using words without even knowing their meaning or are you trying to avoid presenting a
concrete case where the programmer doesn'tknow the size of a string he intends to pass to snprintf() by
discussing semantics of the word "know"?

<snip nonsense>

Rui Maciel

You have little programming experience if you think the programmer
"knows" this in all cases.
 
S

spinoza1111

Richard Heathfield said:
spinoza1111 said:
spinoza1111said:
[...]
Consider the possibility that you are mistaken.
[...]
You know he never will.
You should have my email requesting that you desist from this
offensive conduct.
You are in no position to complain about offensive conduct.

Richard, I presume you're aware of the point of my remark, namely
that *you* are in a position to improve the S/N ratio of this
newsgroup by ignoring spinoza1111.  I'm at a loss to understand
why you continue to feed this troll.

As I have said, your use of "troll" is isomorphic to racism and anti-
Semitism. If you do not reply to my email, I shall complain about your
conduct to your employer, which is apparently Nokia.
 
S

spinoza1111

    >> You mean that the *usage* of "troll" is analogous to "Jew";
    >> however, in that, you're wrong.  "Jew" describes an essential
    >> quality of a person, whether ethnicity or religion, while "troll"
    >> describes a style of anti-social behavior.

    troll> Words, mere words, aren't"behavior".

Words are not behavior, no; but the choice of words, the writing of them
into a Usenet post, and the eventual posting of that post *are*
behavior.

Fascists love to talk about behavior because they suck at words. What
part of "freedom of speech" don't you understand? It's not only a
formal freedom it is also a climate of mutual respect. You destroy
that climate with your words, and your "behavior".
    troll> When a person (in Navia's case, an accomplished and
    troll> intelligent person) has views that don'treinforce the low
    troll> "standards" of your community, he is hounded and when he
    troll> responds, you blame him for being a "drama queen".

Navia gets the reaction he does because he responds to bug reports and
disagreement as if they were personal attacks.  

From what I have seen here, they probably are. Regulars here
continually interpret data in the worst possible way, for example by
generalizing slips of the "tongue" into claims that a person is
globally ignorant, and regulars here always drift offtopic into
discussions of who's competent and who's "trolling".
 
S

spinoza1111

spinoza1111   said:
"Does C have call by reference?"

In teaching, there is a simple approach, and that simple approach is
to teach this:

1. In C, all function arguments are passed "by value." [K&R2, pg 27]
2. Understanding of pointers.

"Call by reference" is extraneous at best.  Keep it simple.

You mean, keep your students in the dark about broader issues of
computer science so they become narrow little technicians, first hired
and first fired. I was a programmer for thirty years and not ten like
most little "programmers" and this was because I learned computer
science.
 
K

Kenny McCormack

spinoza1111 said:
From what I have seen here, they probably are. Regulars here
continually interpret data in the worst possible way, for example by
generalizing slips of the "tongue" into claims that a person is
globally ignorant, and regulars here always drift offtopic into
discussions of who's competent and who's "trolling".

Gee. Ya think?

In other news, water is wet.

P.S. I will point out, once again that the regs completely misuse the
term "troll". We (the ones they continually label "troll") are
contrarians and, in fact, truthtellers.

You want to know who is a troll? Bill Cunningham, for one. He keeps
the regs dancing to his tune, with such grace and style, it brings tears
to one's eyes.
 

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

Forum statistics

Threads
474,266
Messages
2,571,076
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top