Usenet comp.os.linux.alpha (1997-2013)
 help / color / mirror / Atom feed
From: "Martin Sinot" <martin@spase.nl>
Subject: Re: Oddities...
Date: 1999/11/10	[thread overview]
Message-ID: <80bll5$nse$1@news.telekabel.nl> (raw)
In-Reply-To: MPG.1292ced3190663a5989695@oh.news.verio.net

John <plastic@no_spam.oh.verio.com> wrote in message
news:MPG.1292ced3190663a5989695@oh.news.verio.net...
> I was helping a friend on his intro to c course and I got
> curious as to what it would do on my alpha. Why does this
> code.. give unaligned traps, on argv[0-2]??
>
> #include <stdio.h> // Standard Sheeet.
>                    // World Domination?? FREE WODKA!!
> int I;// one lame var..
>
> main(int argc, int *argv[])
> {
>  if(argc==0){
>   printf("Usage: %s values\n", argv[1]);
>   return(1);
>  }
>  for(I=0; I<argc; I++){
>   printf(" argv[%d]: %s\n\n", I, argv[I]);
>  }
>  printf("The following are the results.\n");
>  for(I=0; I<argc; I++){
>   if(*argv[I]>101)
>    [...else if's down to zero...]

Hmm, this is actually a C question, which should go to another
newsgroup. Your problem will occur on every system that has
alignment traps (including Alphas). The problem is in the prototype
of main. The correct prototype is:

int main (int argc, char *argv[])

(note char* instead of int*).
The argv argument points to a list of char pointers, which represent
the arguments passed to the program, as zero terminated strings.
The list is terminated with a null pointer. Now these arguments are
usually simply concatenated in memory, with null bytes between them
to separate them. That means that these pointers may or may not be
aligned (probably not...). Now more downward you try to deference
such a pointer as a pointer to an int, which means that an int is being
read from a nonaligned address -- boom.

From the above program fragment I understand that you want to pass
some numbers to the program as parameters. Parameters are
always passed as strings. If you want numbers instead, you will have
to convert them yourself.
One small thing: the first "parameter" passed to the program is
always the program name, and this is also counted in argc.

Martin Sinot
martin@spase.nl





      reply	other threads:[~1999-11-10  5:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-11-10  5:00 Oddities John
1999-11-10  5:00 ` Martin Sinot [this message]
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