Critique my assignment please

U

user923005

Army1987 said:
On Fri, 24 Aug 2007 15:21:33 -0700, user923005 wrote:
char string[32767];
C89 does not require an implementation to allow more than 32 KB
of auto variables.

This thread, as you have quoted it here,
makes it seem as though the person who wrote that sentence,
thinks that 32767 excedes some translation limit in C89,
which it doesn't.

And it also makes it seem as though the translation
limit applies only to objects with automatic duration,
which as far as I can tell from the standard, it doesn't.

ISO/IEC 9899: 1990
5.2.4.1 Translation limits

- 32767 bytes in an object (in a hosted environment only)

Right. But it is the only data object with file scope.
If it were an auto, it could be argued that the program was not
conforming because there are several other auto variables.
 
P

pete

Martin said:
pete:

Actually, rather than any initialisation taking place at all, I
wouldn't expect either of them to result in the formation of a
variable in which to store the address of the string literal -- but
rather for it to work something like a #define.

On my machine,
the disassembly correlates to what the code means,
which is what I described.

--- new.c ------------------------------------------------------
1: /* BEGIN new.c */
2:
3: #include <ctype.h>
4: #include <string.h>
5: #include <stdio.h>
6:
7: unsigned NumberFromUpperLetter_1(char const x)
8: {
00401020 push ebp
00401021 mov ebp,esp
00401023 sub esp,8
9: static const char *UC = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
10: int c;
11: char *p;
12:
13: c = toupper((unsigned char)x);
00401026 mov eax,dword ptr [x]
00401029 and eax,0FFh
0040102E push eax
0040102F call toupper(0x004011f0)
00401034 add esp,4
00401037 mov dword ptr [c],eax
14: p = strchr(UC, c);
0040103A mov ecx,dword ptr [c]
0040103D push ecx
0040103E mov edx,dword ptr [___xt_z(0x00411a30)+10Ch]
00401044 push edx
00401045 call strchr(0x00401120)
0040104A add esp,8
0040104D mov dword ptr [p],eax
15: if (p != NULL) {
00401050 cmp dword ptr [p],0
00401054 je NumberFromUpperLetter_1(0x00401061)+41h
16: return p - &UC[0];
00401056 mov eax,dword ptr [p]
00401059 sub eax,dword ptr [___xt_z(0x00411a30)+10Ch]
0040105F jmp NumberFromUpperLetter_1(0x00401063)+43h
17: } else {
18: return 0;
00401061 xor eax,eax
19: }
20: }
00401063 mov esp,ebp
00401065 pop ebp
00401066 ret
21:
22: unsigned NumberFromUpperLetter_2(char const x)
23: {
00401067 push ebp
00401068 mov ebp,esp
0040106A sub esp,0Ch
24: const char *UC = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
0040106D mov dword ptr [UC],offset ___xt_z(0x00411a50)+12Ch
25: int c;
26: char *p;
27:
28: c = toupper((unsigned char)x);
00401074 mov eax,dword ptr [x]
00401077 and eax,0FFh
0040107C push eax
0040107D call toupper(0x004011f0)
00401082 add esp,4
00401085 mov dword ptr [c],eax
29: p = strchr(UC, c);
00401088 mov ecx,dword ptr [c]
0040108B push ecx
0040108C mov edx,dword ptr [UC]
0040108F push edx
00401090 call strchr(0x00401120)
00401095 add esp,8
00401098 mov dword ptr [p],eax
30: if (p != NULL) {
0040109B cmp dword ptr [p],0
0040109F je NumberFromUpperLetter_2(0x004010a9)+42h
31: return p - &UC[0];
004010A1 mov eax,dword ptr [p]
004010A4 sub eax,dword ptr [UC]
004010A7 jmp NumberFromUpperLetter_2(0x004010ab)+44h
32: } else {
33: return 0;
004010A9 xor eax,eax
34: }
35: }
004010AB mov esp,ebp
004010AD pop ebp
004010AE ret
36:
37: int main(void)
38: {
004010AF push ebp
004010B0 mov ebp,esp
004010B2 sub esp,8
39: unsigned a, b;
40:
41: a = NumberFromUpperLetter_1('x');
004010B5 push 78h
004010B7 call @ILT+0(_NumberFromUpperLetter_1)(0x00401000)
004010BC add esp,4
004010BF mov dword ptr [a],eax
42: b = NumberFromUpperLetter_2('x');
004010C2 push 78h
004010C4 call @ILT+5(_NumberFromUpperLetter_2)(0x00401005)
004010C9 add esp,4
004010CC mov dword ptr ,eax
43: return a - b;
004010CF mov eax,dword ptr [a]
004010D2 sub eax,dword ptr
44: }
004010D5 mov esp,ebp
004010D7 pop ebp
004010D8 ret
--- No source file ---------------------------------------------




/* BEGIN new.c */

#include <ctype.h>
#include <string.h>
#include <stdio.h>

unsigned NumberFromUpperLetter_1(char const x)
{
static const char *UC = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int c;
char *p;

c = toupper((unsigned char)x);
p = strchr(UC, c);
if (p != NULL) {
return p - &UC[0];
} else {
return 0;
}
}

unsigned NumberFromUpperLetter_2(char const x)
{
const char *UC = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int c;
char *p;

c = toupper((unsigned char)x);
p = strchr(UC, c);
if (p != NULL) {
return p - &UC[0];
} else {
return 0;
}
}

int main(void)
{
unsigned a, b;

a = NumberFromUpperLetter_1('x');
b = NumberFromUpperLetter_2('x');
return a - b;
}

/* END new.c */
 
P

pete

Martin said:
I've debated whether or not a definition like
static char const letters[] = "";
defines two objects. I think it doesn't.
From what I can see,
that should create an array of char's of length

1, and the single element should be a null character.
Quite identical
to:

static char const letters[1] = "";

and:

static char const letters[1] = {'\0'};

I messed up my editing there. That shuda read:
From what I can see, that should create an array of char's of length
1, and the single element should be a null character. Quite identical
to:

static char const letters[1] = "";

and:

static char const letters[1] = {'\0'};

My compiler disagrees with me about the total equivelence of

const char UC[] = {
' ','A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z','\0'
};

and

const char UC[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";


57: const char UC[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
0040118A mov ecx,7
0040118F mov esi,offset ___xt_z(0x00411a6c)+148h
00401194 lea edi,dword ptr [UC]
00401197 rep movs dword ptr es:[edi],dword ptr [esi]


39: const char UC[] = {
40: ' ','A','B','C','D','E','F','G','H','I','J','K','L','M',
004010D5 mov byte ptr [UC],20h
004010D9 mov byte ptr [ebp-23h],41h
004010DD mov byte ptr [ebp-22h],42h
004010E1 mov byte ptr [ebp-21h],43h
004010E5 mov byte ptr [ebp-20h],44h
004010E9 mov byte ptr [ebp-1Fh],45h
004010ED mov byte ptr [ebp-1Eh],46h
004010F1 mov byte ptr [ebp-1Dh],47h
004010F5 mov byte ptr [ebp-1Ch],48h
004010F9 mov byte ptr [ebp-1Bh],49h
004010FD mov byte ptr [ebp-1Ah],4Ah
00401101 mov byte ptr [ebp-19h],4Bh
00401105 mov byte ptr [ebp-18h],4Ch
00401109 mov byte ptr [ebp-17h],4Dh
41:
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z','\0'
0040110D mov byte ptr [ebp-16h],4Eh
00401111 mov byte ptr [ebp-15h],4Fh
00401115 mov byte ptr [ebp-14h],50h
00401119 mov byte ptr [ebp-13h],51h
0040111D mov byte ptr [ebp-12h],52h
00401121 mov byte ptr [ebp-11h],53h
00401125 mov byte ptr [ebp-10h],54h
00401129 mov byte ptr [ebp-0Fh],55h
0040112D mov byte ptr [ebp-0Eh],56h
00401131 mov byte ptr [ebp-0Dh],57h
00401135 mov byte ptr [ebp-0Ch],58h
00401139 mov byte ptr [ebp-0Bh],59h
0040113D mov byte ptr [ebp-0Ah],5Ah
42: };
00401141 mov byte ptr [ebp-9],0
 
M

Martin Wells

I invite you to save this message, and read it
again for your amusement once you have a few
years of professional programming under your belt :)


You assume I'll spend years working for Microsoft and become
complacent, lazy and incompetent.

No amount of "years of professional programming experience" warrants
redundant code (that is, redundant in a BAD way).

Maybe we should check if something is equal to 5 by:

if (5 == x && x != 1 && x != 2 && x != 3 && x != 4)

That sound good? :)

Martin
 
M

Martin Wells

That has absolutely nothing to do with what I just wrote.

I'm talking about you learning the language well enough
so that you know the exact meaning
of what you yourself are writing.

I go the impression that you said what you said in response to my
taking into account how 99% of implementations actually get thinks
done (i.e. paying attention to what actually WILL happen rather than
what should theoretically happen according to the Standard).

Even if someone knows the C Standard like the back of their hand and
is competent in using it, they'll still end up writing better programs
if they have knowledge of how 99% of implementations work.
 
U

user923005

I go the impression that you said what you said in response to my
taking into account how 99% of implementations actually get thinks
done (i.e. paying attention to what actually WILL happen rather than
what should theoretically happen according to the Standard).

Even if someone knows the C Standard like the back of their hand and
is competent in using it, they'll still end up writing better programs
if they have knowledge of how 99% of implementations work.

Nobody knows how 99% of the implementations work. If you think you
do, then you have another thing coming.

Have you used compilers on:
OpenVMS
MVS
HP/UX
Ultrix
Tru64
Windows
Linux (Fedora, Redhat, etc.)
Solaris
AIX
NetBSD
Macintosh
QNX
Amiga
CP/M
OS/2
Irix
Unixware
SCO
VxWorks
Interix

I have (from pre-ANSI days up to today). And yet I know it is only a
teeny-tiny fraction of the available C compilers. Actually, the
largest majority of them are for embedded devices, and I have never
used any of those. If you think it is a good idea to memorize how
every implementation behaves for every instance of it's compilers and
for every setting of their compiler's settings, then have at it.

You are headed for frustration, because that is a mountain that nobody
can climb.
How much wiser to understand the fundamental rules about how the
language is supposed to work.
Sure, it is a very good idea to know how to work GCC and G++ along
with related tools because of the wide usage.
It is also a good idea to know how to work Visual C++ and the Intel C+
+ compiler because of the ubiquity of the Windows platform.
It is also a good idea to know how to use the SAS/C compiler for IBM
mainframes etc.
But I think it is a foolish notion to imagine that you will ever know
how 99% of the compilers work.
I would like to think that eventually, I will understand 5% of them
and be familiar with their usage, but I have no thoughts to completely
understand the quirks and nonstandard behaviors of those that I do
become familiar with.

P.S.
I also had a C compiler for the Commodore 64 just for fun, and have
done commercial work on a PDP 11/70 in C (I think it was BSD Unix, but
I can't remember for sure). I am sure that there are plenty of others
that I forgot about.
 
P

pete

I quoted what I was replying to.
You are headed for frustration, because that is a mountain that nobody
can climb.
How much wiser to understand the fundamental rules about how the
language is supposed to work.

And *that*, is what this newsgroup is all about.
 

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
473,774
Messages
2,569,600
Members
45,181
Latest member
RexGreenwa
Top