A C showstopper

J

John Bode

[snip]
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.

Use the precision specification with a wildcard value as part of the
conversion specifier, then provide the precision size as an argument
to *printf:

sprintf(buf, "%.*s", (int) sizeof buf - 1, ptr);

will limit the result string to fit in buf (at least up to INT_MAX
characters).
 
R

Rui Maciel

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

Where would you intend to use snprintf() in that case?


Rui Maciel
 
S

spinoza1111

Although quite common, that's a complete misuse of Hungarian notation.
The fundamental idea was to use *high level* data types like length,
temperature, and person, not low level data types like int and double.

"Apps Hungarian" turned out to be less useful than "system Hungarian".
Simonyi's was apps, but simultaneously with his "invention", systems
Hungarian was already in use in IBM mainframe assembler programming.
Its inventors were unknown, so Simonyi gets the credit.

I read about a form of one-character "Hungarian" in a book by Diebold
published in 1960 about 1401 assembler programming. The practice
predated Simonyi.
 
S

spinoza1111

    troll> As I have said, the grammar of "troll" is isomorphic to "Jew"
    troll> in anti- Semitic tracts.

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.

Words, mere words, aren't "behavior".
    troll>  It derives from Nordic racist texts,

It doesn't, actually; it derives from the verb "to troll," as in for
fish, by dangling a line with one or more baited hooks and waiting for
the fish to bite.  The fact that it has a double meaning, in that it
also refers to a repulsive creature that hides below bridges, probably
accounts for its longevity, but it's not the original source.

The word "troll" was in fact in use in California in the 1980s by
homeowners and landlords to refer to homeless people. A T shirt was
printed up with a picture of the "not" semiotic superimposed over a
"repulsive" creature with a runny nose.

This was unfair to homeless people, because it blamed the victim for
his victimization even as the Nazis found the Jews in the ghettos the
Nazis created to be "repulsive".

When a person (in Navia's case, an accomplished and intelligent
person) has views that don't reinforce the low "standards" of your
community, he is hounded and when he responds, you blame him for being
a "drama queen". Likewise, many (not all) "trolls" are simply using
the medium as intended to reply to slurs.

    troll> and it fulfills here the function of racism: unifying the
    troll> community against the outsider, no matter how that outsider
    troll> is marked...here by his ability to write above a small lower
    troll> bound of complexity in an unusual combination with
    troll> programming skill somewhat above the norm, and the fact that
    troll> he was programming C when you were shitting your pants.

You flatter yourself overmuch.

I don't think I do. At any rate, one community norm is that the
thought leaders like Heathfield will primarily engage in negative
correction, being at pains to torpedo ideas without being able to
contribute anything new. For example, Heathfield tells us in "Another
Spinoza Challenge" that "C does not have call be reference", meaning
that "call by reference" is implemented using the value of the
address.

In this community, Job One is strangling new ideas and contributions
at birth, and the label "troll" fulfills this function.

It may sound hip and cool, but like most hip putdowns, it accomplishes
the task of domination.
 
S

spinoza1111

spinoza1111said:



Consider the possibility that you are mistaken.

I have, for too long.
Call by reference is not new, so it isn't an example of your point. C
really doesn't have call by reference, and call by reference is *not*
implemented using the value of the address.

Again, you post negatives without having the courage, or the
knowledge, to post positive information, which would be useful.

The "right answer" to the following question

"Does C have call by reference?"

is not "yes", or "no". It's not even "yes and no". It is fundamentally
an essay: "C implements call by reference using call by value at
runtime. Call by reference occurs effectively when the programmer
provides the von Neumann address to a function using the ampersand
prefix operator, where the 'Von Neumann Address' is the address of a
variable in the storage of a computer which addresses data using
integer values over which the arithmetic and comparision operators are
defined."

All you are able to do is to find "errors" in such essayistic
paragraphs, and it's always possible to do so IF one believes that
things are Fundamentally simple in life. Then, in a frightening model
of actual Fundamentalisms, you issue destructive *fatwas* against the
writer.

Pray tell me how to implement C's call "by reference" WITHOUT using
the von Neumann address.

Also, you've just contradicted yourself. You denied that it's used in
C and now you implicitly concede that it is implemented.
Call by reference is not a new idea. It is, however, not implemented
in C. If you want to "contribute" call by reference to C, the way to
do it is to persuade ISO to add it.

Oh, now we're back to square one. Make up your mind, or are you only
concerned with the politics of personal destruction?

You've been corrected by other posters.
No, it accomplishes the task of helping people to understand C better.

You are NOT accomplishing that task in the slightest. You are
spreading lies and confusion because your publisher is probably paying
you to destroy reputations online. I am familiar with your publisher's
business practices. One Chicago friend did some editing for them and
was cheated of her compensation, whereupon she set up a Web site
describing their practices. The publisher charged her under Chicago
laws governing telephone harassment and she mistakenly chose to fight
the charges *pro se* (without an attorney). She was found in contempt
of court and jailed overnight at one point.

It would not surprise me if this publisher paid you to destroy Schildt
and by implication McGraw Hill's computer books.
It is *easier* to understand C if you recognise that C is always
call-by-value. The erroneous introduction of a term like "call by
reference" simply causes confusion to those C learners who alreasdy
know what call by reference really means, and doesn't help those who
don't.

The people who complain about my "trolling" (which here means being
literate above a low bound) also complain about you, Richard. You
spread confusion by giving negative answers to broader questions, and
unexplained answers to specific questions. You are not personally
vindictive but nonetheless propagate and enable the politics of
personal destruction because you have a Fundamentalist interpretation
of C which insists (like Bible-thumping, God-walloping, Come to Jesus
Fundamentalism with respect to Scripture) that there is only one
interpretation of C.
 
O

Old Wolf

I attempted to emulate `asprintf()' in a pre-C99 environment here:

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

spinoza1111

No, in this case it will be evaluated once: when asprintf is
called.

....only if you implemented asprintf in your compiler to evaluate
fortune_cookie() once, and store its result (presumably a char *
pointer) in a table that itself is of dynamic size. If on the other
hand you call snprintf repeatedly until the full string has been
created, or use malloc and a rescan repeatedly, fortune_cookie() will
return multiple values.
 
O

Old Wolf

You need to read the rest of the thread.  The reason it is a hack
is that the required length might be different on the second
pass.  

I don't see anything else in the rest of the
thread that suggests a two-pass method might
fail because the length would change (?!)
 
N

Nobody

The problem is that any sprintf whatsoever, insofar as it formats
strings, has no control over string length.
This alone demonstrates that C is not a viable programming language
anymore, and may never have been, for applications other than writing
operating systems and compilers.

This isn't exactly a "C" showstopper, although (unlike your gripes about
regexp libraries) it is at least an issue with C itself, rather than some
third-party library.

Note that the issue is specific to hosted implementations, and completely
disappears in a freestanding implementation. Even in a hosted
implementation, you aren't forced to use the standard library (the Windows
ecosystem actively frowns upon the use of the standard library).

This last point is one of the main reasons for C's success. There are
plenty of languages where working around limitations of the standard
library is somewhere between extremely difficult and impossible.
 
R

Rui Maciel

Nick said:
in the function. Where did you think he meant?

Exactly. If the person using snprintf() is also the one writing that function then nothing stops him from
adding a buffer size parameter to it.

So, again, can anyone provide an example where the programmer doesn't know the size of the string that he
must pass to snprintf()?


Rui Maciel
 
S

spinoza1111

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 know he never will.

If I thought so, I wouldn't have wasted my time. A personal email is a
heads up to the effect that I'm not some force of nature, just a
person.
 
S

spinoza1111

Exactly. If the person using snprintf() is also the one writing that function then nothing stops him from
adding a buffer size parameter to it.

So, again, can anyone provide an example where the programmer doesn't know the size of the string that he
must pass to snprintf()?

Depends on what you mean by "know". The greatest programmers don't
reify a program, and think of it as a concrete unity, never to be
changed. Over time, assertions (such as "this string will never exceed
1024 or whatever bytes) fail, therefore great programmers minimize
assertions.
 
S

spinoza1111

spinoza1111said:
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.

I believe I am. Since 2000, I have been harassed for being able to
write, and disloyalty to artifacts, in a programming world where half-
literacy and insane loyalty to artifacts have become markers of
"ability"...despite the inability of most programmers in the G-8
countries to deliver, or even stay employable. I respond to offensive
conduct unlike a lot of the cowards here.
 
S

spinoza1111

spinoza1111said:








I'm positive that C is pass-by-value.

(Sigh). Yes, Richard, we know that. Brian and Dennis sat around years
ago and figured it all out.
Wrong, and...


...wrong. C does not implement call by reference *at all*.


Wrong. Call by reference is where the implementation provides not the
value of an argument but a reference to it. In C, this does not
happen.

Gee, what's an address but a reference, Mr. Science?
Wrong. I can do much more than that. But it is certainly true that I
can find errors.

Gee, what else can you do? Can you call spirits from the vasty deep?
C does not have call by reference.


No, it is not implemented *at all*. You incorrectly interpreted my
statement "call by reference is *not* implemented using the value of
the address" as suggesting that it was implemented in some other way.
It isn't.





We're back to C, which does not implement call by reference.

In common parlance, it does, since what it does is give you the
functionality plus the von Neumann address in case you want to screw
things up.
I didn't change it, and will not do so until the facts change.


Phil Carmody appears to have misunderstood my point, but Keith
Thompson has amplified it adequately.

They beat out a kernel of thought from you.
I've been accomplishing that for some years.

What code have you written outside of banks and insurance companies?
Have we any packages from you for free?
Are you sure you qualify?

Yes. It's plain to people who are literate, that I am literate, if at
times apparently verbose. This fact is invisible to people who aren't
literate.

Read Spinoza. What makes you so confident that you can judge others
when you know your own limitations? You do know your own limitations,
I'd wot, therefore you cannot judge me as a writer on programming
topics in the slightest, since your education in both computer science
and general culture is narrower...while being, I'll be the first to
admit, deeper in C itself.

Anyone who wastes our time by insisting that C has (in no way) a call
by reference until we beat out of you your admittedly valid insight,
that C gives one the functionality of C Sharp's and VB's by reference
calls, is not a good communicator.
 
R

Rui Maciel

spinoza1111 said:
Depends on what you mean by "know".

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't know the size of a string he intends to pass to snprintf() by
discussing semantics of the word "know"?

<snip nonsense>


Rui Maciel
 

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,266
Messages
2,571,075
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top