bna-linuxiran
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [linuxiran] [OT] C programming, variable size array


From: Arash Partow
Subject: Re: [linuxiran] [OT] C programming, variable size array
Date: Sat, 13 Dec 2003 08:57:43 +1100

Hi Aryan,


>
> WOW! Did you just wrote this? Wow! I takes me two hours to write such a
> code (assuming that I knew how to write it in the first place).
>

In a industrial environment you would be expected to write code of this
complexity without even thinking about it, its nothing special. it only took
about 2 minutes


> Anyway, clear, fully commented code. Everything is pretty clear, aside
> from me not knowing malloc and realoc (which I have to learn pretty
> soon).
>

Commenting is good, but rarely do i comment that much, i just thought it
would be good for you. usually i rely on well named variables etc..


>   exit(EXIT_SUCCESS);
>    /*
>     proper posix way to exit.
>    */
>    return 0;
>
> Which one do you mean is the proper POSIX way of exiting? the exit
> function? or return 0? I always put return 0; though my incompetent
> instructor doesn't even put it. But I haven't seen the exit() function
> before, well, I haven't studdied the POSIX standard !!!
>

POSIX says a combination of both:
   exit(EXIT_SUCCESS);
   return 0;

is the way to go, this allows multitasking OSs to think ahead of time, also
the exit() method is used to exit out of the application itself, and pass
the failure or success. which can then be passed on as the application
result (great for scripts and piping) it is an accepted POSIX standard.

In this code you really don't need return however there are some OSs which
don't exit from the application with exit() because the cpu pointer is in a
stack and hence the OS wont allow spontaneous stack unwinding etc, you need
to know a little assembler before you can understand this concept.


> Also, what does sizeof() do? from the code it seems to me it is used for
> counting. Am I right?
>

sizeof is a macro which will give you the size of a variable, type or struct
in the base unit of the compiler. on gcc 1 char = 2 bytes = 16 bits.
so sizeof(char) will give you 1,

> You use if (((char)tempValue) == EOF) to determine EOF.
>
>
> The way I have learned, I use the getchar() function to do so, as in
> while ( (tmp = getchar() ) != EOF )
>
> Any advantages to any method?
>


in theory with scanf it wont return until you press enter, there is a lot
of error correcting you should be doing, also i wouldn't get an int from
stdin like that, i would get it 1 char at a time and wait for return at
which point i will examine the data accumulated.
ie:
 1. check for valid int digits and if any invalid chars are there bark an
error
    1.2 if everything is ok, use atoi (ascii to int) to convert a char*
string to an int
 2. if eof is data then break out of loop and do whatever,

but for learning scanf is just as good...

> Also now that I am here, let me abuse your knowledge, and ask one more
> thing. I just learned the gets() function, and when I use it on my gcc
> 3.2, it gives me a "the gets function is dangerous. It shouldn not be
> used" warning. I guess gets is now deprecated, but it is still being
> tought in our univ. Do you know what is the correct way of inputting a
> string of characters, not using gets ?
>


gets is very bad because it can lead to buffer-overrun problems which
1.) can cause the application and/or system to crash
2.) possibly allow a person to gain root on your system if it isn't
    patched properly.

In short don't use it.

Now about depreciation, I've been programming in c for about 9 years now
and i haven't once seen anything from the standard libraries become
depreciated. depreciation is a java phenomenon.


there are patterns in c for obtaining data from a user or
file, and they depend on what kind of data you are aquring.
but in any case they are the right way, which i'm sure you
will learn very quickly.



Regards


Arash





__________________________________________________
Be one who knows what they don't know,
Instead of being one who knows not what they don't know,
Thinking they know everything about all things.
http://www.partow.net






reply via email to

[Prev in Thread] Current Thread [Next in Thread]