From: geest3+@pitt.edu (Gregg E Economou)
Subject: Re: Compaq prefers Alpha over Merced! BUT OF COURSE, ALPHA *
Date: 1998/08/29 [thread overview]
Message-ID: <6s7m9b$7cg$1@usenet01.srv.cis.pitt.edu> (raw)
In-Reply-To: 6s6i11$jla$1@vixen.cso.uiuc.edu
hold on a sec!
When the compiler sees this, heres what it does.
In article <6s6i11$jla$1@vixen.cso.uiuc.edu>,
>How do you propose I do this? Say I write a program:
>
>main() { char *hello = "Hello"; pascal_format_puts(hello); }
>
first, it sees the char declaration and generates a macro for allocating
space at the stack. (or it generates the code itself) and investigates
the ="hello" part to count how much space. On word-aligned systems
or other such thing it figures out how much space to use. Then it ultimately
generates some code which has the effect of pushing future stack references
further by however much space was needed, and has the image (since this is
after all a compiled-in literal) contain the 'hello' in the right place.
|There is _nothing_ in the C compiler that understands that you wanted a
string. This is because there is no such thing as a 'string' in the C
compiler. When you use a library function like printf to display that
'hello' text, (in this case i am talking about how printf works but
they all do somethign like it) , the function printf is called after putting
the address of that bit of memory where the 'hello' started in a location
that printf will search for its arguments. Usually printf will have its
format string pointed to by a register and then either arguments pointed to
by registers or else the next register will point to an array of pointers
to arguments. that part is irrelevant. Printf, when told to print a string,
will loop through the memory pointed to by its argument, where you told it
there wa sa string, and printf will copy those bytes to the right place and
have some other system calls or i/o calls get them printed out, and it will
cease doing whatevver it takes to print them out when it sees one of them
is zero (null). this is the closest you get to the undertsanding of a 'string'
in this scenario. Your 'dependence' on a null-terminated 'string' "in C"
is really the fact that printf is implemented to look for things like
that when you tell it to print a string. It also reads its format string
in that fashion.
So really your dependence on 'strings' being null terminated is because
the function (in this case printf) that you are choosing to use expects
that when you are talking about 'strings' you mean 'a bunch of bytes
consecutively found in memory the last of which is a null, beginning at
some address'. Those people who things char*blorf makes blorf a string
are misled- char*blorf really means that blorf is a pointer (the compiler
will decide how much space is needed to store an address of the proper
form for whatever system it is making code for) and that the amount of
data that is nominally pointed to is one character, which C specifies to be
no less tha eight bits in length. Since it is an address and no more,
you could of course do somethin glike
int* blarf;
char*blorf=blarf;
and be legal- but the char* declaration hints that wherever you actually
refrence blorf in the source code, corresponding assembly or machine code
will be generated which operates on the refrenced contents as a character,
i.e. on some systems selecting instructons with byte operations as opposed
to word or other such operations, on other systems invoking a mask and shift
to operate on tthe value solely of this byte, and so on and so forth- it
is a directive to hint at what you are referencing with this address
and more than anything else is used to know how far down the stack one
must read to retreive it. it means nothing about a string. In C there is
no such thing as a string. There is memory. thats about all there is in C.
integers, floats, and so on are all just directives to amounts of memory
with a predetermined size. you could just as easily (though many compilers'
preprocessors will complain or forbid this) use any other kind of pointer
and treat it as a 'string' if you wished. However this would have some
ramifications at compile time when things of different sizes were allocated
and so on.
oi! sorry to everyone for having to read this hideous ridiculosity, but
for those who didnt know this stuff, now you do and now you know why
a: there is no dependenceon any kind of data tye in C beyond the
specifications that char be >7 bits long, int be >15 bits long, and long
be >31 bits long.
b: the concept of a 'string' in C is a convention which has persisted
for many reasons but are all human factors in choosing to determine the
end of some text in this manner.
happy hacking,
gregg
>How do I get the compiler to generate the hello string as a pascal
>string? Do compilers typically have a command line option to specify
>what kind of strings you can use? In particular, does gcc?
what do you mean by this?
if youd liek to make a pascal string of this, then you have to create
that structure somewhere. Your first step will probably be to count the
size of the stuff that you would call a string that starts at the address
contained in the location refrenced by hello, and then copy the data
which begins at that address and continues to the length you detrermined
into some other place in memory. Then you might continue by creating any
further aspects of the structure you need, and then of course youd need
an implementation of that print-a-pasca-string fucntion you wanted to
call. If its in a library, call it and make sure it
was declared or else extern'ed and either way linked in later.
Then youd call it, using whatever means it needs to find the pascal
string contruct in memory and oprate on it.
>
>It seems that the only semi-portable way to do this would be to compile
>the program with strings constants as writable, and convert each string
>a run-time. This is a serious, serious kludge, and I'd hardly say that
>this is a "perfectly free" task (especially since it is likely that
>many compilers do not even support writable string constants, so it's
>non-portable, which defeats the main purpose of C).
um, no, you are missing the entire point of C and its portability.
C does NOT purport to provide some hideous monstrosity of massive strucutred
langauges with heavy type enforcement. C is portable because NONE of
that stuff is forced on the programmer. In C, the most important
thing (and the only true data type) is a byte of memory. everythign else
is some quantity of bytes of memory, and the other important thing of
memory is the pointer. You have to know where in memory your stuff is.
The second important thing in C aside form memory is logic and arithmetic.
You do all that nifty stuff, including control flow and so on, and all
the time you are performing your operations to process the contents of some
locations of memory, ultimately storing your results somewhere (be it a
register, or using some instruction to send data to the bus, or back to
memory, is beyond the scope of this) and thus effecting some work done,
which is the goal of any useful nontrivial program. the third thing you
must consider with C is the possibility that you dont have all the memory
at your disposal- things like other programs, and perhaps your own stack
and so on, are places you should tread lightly, but are in no way bound
to- you have the right and the means to destroy even yourself, or
modify even yourself.
now, many comilers will do all the fun of managing your stack, i mean
after all thats part of its job, and many compilers have some macros
built in for many things. There is part of C a macro facility, as well,
which is of course the preprocessor defines and such. the preprocessor is
part of a compilation facility but doesnt compile anything per se- it
just processes the source code before compilation. Many people dont see
the distinction well since other 'compilers' have incorporated this
concept much more into the compilation stage and in many cases cannot
be separated from it. Anyway, all the stuff like i/o and system calls
and stuff, none of that is part of C itself. (regardless of whether ANSI
calls the standard library a part of the language, it isnt. it is something
which one can expect any ANSI C compiler to be able to support and operates
as specified by the standard, but it is not part of the language but
rather a set of routines written _in_ C which ANSI decided everyone should
have at their disposal because of their widespready handiness and utility. )
At this point C looks like a nice way to mask the tedium of assembly
rogramming wihtout giving up much of the direct control and without losing
performance. All the higher-level stuff is implemented _in_ C, but is not
_part_ of the language. Only the extreme simplicity of C itself the
nlanguge, allos such higher level contructs to exist and work.
I highly suggest the K&R book at this point before i have to start
talking about structs and stuff... :)
happy hacking,
gregg
next prev parent reply other threads:[~1998-08-29 4:00 UTC|newest]
Thread overview: 69+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <0095000017115526000002L062*@MHS>
[not found] ` <35E04DF1.1911@world.std.com>
[not found] ` <35E09E76.6773@earthlink.net>
1998-08-23 4:00 ` Compaq prefers Alpha over Merced! BUT OF COURSE, ALPHA * Terry C. Shannon
1998-08-23 4:00 ` Glenn C. Everhart
1998-08-24 4:00 ` Paul Sture
1998-08-25 4:00 ` Gregg E Economou
1998-08-25 4:00 ` John Wilson
1998-08-26 4:00 ` Gregg E Economou
1998-08-27 4:00 ` John Wilson
1998-08-25 4:00 ` Jordan Henderson
1998-08-26 4:00 ` Bob Koehler
[not found] ` <6s910k$2bo$1@Starbase.NeoSoft.COM>
[not found] ` <35e833be.0@news.wizvax.net>
1998-08-29 4:00 ` Jordan Henderson
1998-08-30 4:00 ` John Wilson
1998-08-30 4:00 ` JF Mezei
1998-08-30 4:00 ` Chris Torek
1998-08-31 4:00 ` Gregg E Economou
1998-08-31 4:00 ` Chris Torek
1998-08-30 4:00 ` Lawrence Kirby
1998-08-30 4:00 ` JF Mezei
1998-08-30 4:00 ` Martin Ambuhl
1998-08-31 4:00 ` Craig Franck
1998-08-30 4:00 ` JF Mezei
1998-08-31 4:00 ` firewind
1998-08-31 4:00 ` Michael Rubenstein
[not found] ` <6scur7$qtk@bgtnsc02.wo <35EA0E22.C59E9476@videotron.ca>
1998-08-31 4:00 ` Will Rose
1998-09-04 4:00 ` Thor Lancelot Simon
1998-08-30 4:00 ` Gregg E Economou
1998-08-30 4:00 ` K&R fight over ketchup (was: Compaq prefers Alpha over Merced! BUT OF COURSE, ALPHA ) Clint Comes
1998-08-31 4:00 ` Bob Koehler
1998-09-05 4:00 ` Gregg E Economou
1998-09-05 4:00 ` Compaq prefers Alpha over Merced! BUT OF COURSE, ALPHA * Peter da Silva
1998-09-05 4:00 ` Jordan Henderson
1998-09-07 4:00 ` Peter da Silva
1998-09-07 4:00 ` Jordan Henderson
1998-09-07 4:00 ` Dan Pop
1998-09-07 4:00 ` Richard Stamp
1998-09-07 4:00 ` Peter da Silva
1998-09-07 4:00 ` Jordan Henderson
1998-09-08 4:00 ` Tim Shoppa
[not found] ` <6t4cui$hfu@espresso.cafe.net>
1998-09-09 4:00 ` Tim Shoppa
1998-09-08 4:00 ` JF Mezei
1998-09-09 4:00 ` Dan Pop
1998-09-09 4:00 ` Peter da Silva
1998-09-08 4:00 ` Peter da Silva
[not found] ` <6t2im5$mke@espresso.cafe.net>
1998-09-08 4:00 ` Peter da Silva
1998-09-08 4:00 ` Peter da Silva
1998-09-08 4:00 ` JF Mezei
1998-09-08 4:00 ` Dan Pop
1998-09-08 4:00 ` Peter da Silva
1998-09-08 4:00 ` Dan Pop
1998-08-29 4:00 ` Gregg E Economou
1998-08-30 4:00 ` Jay Maynard
1998-08-26 4:00 ` Paul Sture
1998-08-27 4:00 ` Gregg E Economou
1998-08-28 4:00 ` Paul Sture
1998-08-26 4:00 ` Bob Koehler
1998-08-27 4:00 ` Peter da Silva
1998-08-31 4:00 ` Boris
1998-08-27 4:00 ` Thor Lancelot Simon
1998-08-28 4:00 ` TERENCE MURPHY
1998-08-28 4:00 ` Greg Thomas
1998-08-29 4:00 ` mathog
1998-09-01 4:00 ` Greg Thomas
1998-08-28 4:00 ` Monty Brandenberg
1998-08-28 4:00 ` George Cornelius
1998-08-29 4:00 ` Gregg E Economou [this message]
1998-09-05 4:00 ` Peter da Silva
1998-08-28 4:00 ` Bob Koehler
1998-09-05 4:00 ` Peter da Silva
1998-09-08 4:00 ` Carl Perkins
1998-08-31 4:00 ` Boris
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox