Skarmander said:
Flash said:
Skarmander said:
Flash Gordon wrote:
Skarmander wrote:
Richard Heathfield wrote:
jaysome said:
On Fri, 07 Jul 2006 09:10:14 +0000, Richard Heathfield
<snip>
What other Standard C functions would be good candidates for the
"-function" option?
Just gets(). But I'd flag a few others as being "experts-only"
functions, which you should not use in "real" code unless you know
/exactly/ what you're doing:
<snip>
From <stdlib.h>
[...] calloc, malloc, realloc, free, [...]
Let's get this straight: you shouldn't dynamically allocate memory
in C unless you're an expert?
It is extremely easy to get wrong.
I see you did not address this point.
Sure I did, in this statement:
It would have been more obvious had you put that statement by the comment.
I don't think anybody's arguing that it's not a big source of mistakes.
Then again, plenty of common tasks in C are big sources of mistakes, as
the lengthy FAQ illustrates.
Indeed.
I have no idea why this is relevant to my statement, but you are
probably misled by my "42 memory units". I deliberately used a magic
number and didn't specify what those units were. The idea that assuming
a specific amount of memory makes things easier holds whether you're
talking 42 bytes or 42 megabytes. (Of course, there's a huge practical
difference.)
Ah, but on this embedded system the assumption of X units was broken
because it was found not to be sufficient so they increased the memory
to what was then an extremely large amount.
And my point is that as long as such programs are *not* the majority,
dynamic memory allocation is something that belongs in the arsenal of
every C programmer. We'll return to this later.
Where do you get your statistics? I know I have far more embedded
processors in my house than PCs (and I have more PCs than most people)
and it is entirely possibly I regularly use more programs on embedded
processors than I use on PCs. I have no easy way to find out whether
those embedded programs use dynamic memory allocation or not.
Well, I know that the Notepad that came with Windows 3.x, 95, 98 and
presumably ME was limited to 64K, a holdover from the segmented memory
era. Ironically, the DOS-based EDIT.COM that came with Windows 9x did
not have this limitation.
It is still in XP and I believe unchanged.
Of course, that version of Notepad is restricted to the point of being
inappropriate for anything more ambitious than a grocery list, so the
size limit is just the finishing touch.
I find it very useful when I want to be able to quickly take note. It
starts instantly and does exactly what I tell it to. I don't use it for
editing code though.
I don't. I think it *should* limit the use of C to experts (at least for
applications programming
Even for programs that have absolutely no need of dynamic memory? I've
already pointed out that there are plenty of them whether or not they
are the majority.
> -- but outside of applications programming
there are other things that require expert attention, so it probably
evens out). I don't really think it's acceptable for a C programmer to
say "well, I program C very well, but I can't really handle dynamic
memory allocation". If you said you had no idea how to use longjmp(), I
could sympathize, but dynamic memory allocation? No.
So now you are restricting your requirement to just the range of
programming tasks you know use dynamic memory allocation?
This is just me being a snob. In reality C is a tool, and it will be
used with various levels of competence. As long as the job gets done,
you won't hear anyone comment about how competently a tool was used. In
the case of static memory sllocation, you won't hear people comment
until the job instances get bigger.
There are plenty of situations where there is some other limit that
prevents the job getting bigger.
I'm sure plenty of C users can write plenty of useful programs that do
not dynamically allocate memory.
And for such programs it does not matter whether the author has learnt
dynamic memory allocation or not yet. So restricting dynamic memory
allocation to experts does *not* restrict C to experts for those programs.
> I'm also pretty sure most of the
programs run don't or shouldn't fall in this category.
I know for a fact from personal experience that it is possible to spend
years programming professionally on use programs, so whether they are
the majority or not there are plenty of them. BTW, I *did* know how to
manage dynamic memory allocation, I just did not need it.
Dynamic allocation is almost never the *simplest* solution in C.
Well, it has been the simplest solution for me in a number of instances
where I had no way of knowing in advance how much memory would be required.
The simplest solution is often the most appropriate one because
simplicity cuts development and maintenance costs -- but for general
applications, when the simplest solution involves no dynamic memory
allocation, it will often be *too* simple.
If it does not solve the problem due to being too simple then it fails
to be the simplest solution because it is not a solution. If you are
going to allow things which don't solve the problem then the "simplest
solution" is
int main(void)
{
return 0;
}
My argument is that even were it is appropriate, it tends to become
inappropriate sooner than later.
If a program lasts the entire lifetime of the kit it is designed to
support without such things having to be changed (say, 20 years), I'm
happy. The next piece of kit will require changes to the supporting
software in any case.
If dynamic memory allocations is for experts, then please let C be for
experts, so I don't have to cope with exactly this attitude anymore.
(Note: snobbery.)
I disagree. I've worked on C projects with people who have not yet
finished their degrees and are not yet expert programmers, let alone
expert C programmers, and it was no problem at all. You just have an
expert programmer reviewing their work and assisting them as necessary.
The same applies *whatever* language is being used.
"Well I thought a maximum of 12,564 sprockets would be reasonable
enough, but no worries if it's not, you just go to arbitrarylimits.h and
increase the third #define from the top to 47. Recompile and reinstall
and you're all set. That wasn't so hard, was it?"
The hardware can only cope with 12564 sprockets, a static array of 12564
sprockets is not an unreasonable amount of memory, OK I'll use a fixed size.
Oh, we are producing a new piece of hardware that can cope with more?
And you have all these other extra requirements? OK, we will spend the
extra 5 minutes on top of the 6 man-months to implement the other
requirements so as to increase the array size. Why did we limit it so we
now have to waste that 5 minutes? Well, it saved us a day of development
time and we will have to go through a *lot* of 5 minutes before we hit
the point where we've lots on that saving. Anyway, 5 minutes on top of
that 6 man-months is hardly significant, is it?
Like you said, I'm sure there are a good many situations where this will
work. But it definitely does not work in general, and software tends to
evolve in the "more general" direction.
Just as not using dynamic memory allocation does not work in general
(i.e. there are a significant set or problems where it is appropriate),
so *using* dynamic memory allocation does not work in general (i.e.
there are a significant set of problems where it is inappropriate).
There are programs I've written where the only way I could have used
dynamic memory allocation would have been something like:
{
T *p = malloc(SOME_CONSTANT);
if (p == NULL)
/* we are really in the shit now */
else {
/* do stuff */
free(p);
}
}
Personally I found
{
T p[SOME_CONSTANT];
/* do stuff */
}
to be a far simpler solution.
I will admit that some of these projects where only a few man-years of
software development, so only small programs. Oh, and the only way to
increase the amount of data that the program would be required to handle
would be to replace some hardware where one circuit board alone cost
9000UKP (18 layer double sided), I don't know how much on top of that
was spent on components, now how much the other hardware that would
require replacing cost.