Thought I knew what I was doing 
Author Message
 Thought I knew what I was doing


says...

Quote:

>In addition to the possible problem John explained above, I'd also imagine
>that if the compiler assumes malloc() returns an int (as is the case when
>you don't include <stdlib.h>), any returned value from malloc() larger
>than the maximum limit of int will be truncated to fit the size of int before
>explicitly cast back to the pointer.  It can happen on a system where the size
>of pointers is larger than that of integers (such as Digital UNIX).

  That's certainly *one* possibility.  There are others.  If the return value
is being left on the stack, calling code may pop more or fewer bytes than were
actually returned, leading to a bogus return at the end of the calling
function.  It's nasal demon time.

David G



Mon, 23 Oct 2000 03:00:00 GMT  
 Thought I knew what I was doing

Quote:


> [snip]
> >My point is simply that using an unprototyped function
> >will generate (at least) a warning.

> I don't believe this is true.  It SHOULD produce at least a warning.  But on
> some compiler settings it will not produce one [for some of my compilers]
> and I do not believe that there is a requirement in the standard that it
> produce a diagnostic.

There isn't in C89, but C9X does require a declaration (although it
doesn't have to be a prototype) since the implicit declaration as a
function returning int has been removed.

-Larry Jones

I take it there's no qualifying exam to be a Dad. -- Calvin



Mon, 23 Oct 2000 03:00:00 GMT  
 Thought I knew what I was doing

Quote:

> I think I finally understand what this all means, and why omitting
> <stdlib.h> and explicitly casting malloc() is really an error.  In
> addition to the possible problem John explained above, I'd also imagine
> that if the compiler assumes malloc() returns an int (as is the case when
> you don't include <stdlib.h>), any returned value from malloc() larger
> than the maximum limit of int (i.e. 64K) will be truncated to fit the
> size of int before explicitly cast back to the pointer.

Not only that, but there are some systems where int return values are
put in a different register than pointer return values.  On such a
system, the value you get as an int doesn't have anything at all to do
with the actual pointer value that was returned!

-Larry Jones

I'll be a hulking, surly {*filter*}-ager before you know it!! -- Calvin



Mon, 23 Oct 2000 03:00:00 GMT  
 Thought I knew what I was doing


Quote:

> > I think I finally understand what this all means, and why omitting
> > <stdlib.h> and explicitly casting malloc() is really an error.  In
> > addition to the possible problem John explained above, I'd also imagine
> > that if the compiler assumes malloc() returns an int (as is the case when
> > you don't include <stdlib.h>), any returned value from malloc() larger
> > than the maximum limit of int (i.e. 64K) will be truncated to fit the
> > size of int before explicitly cast back to the pointer.

> The type "int" is simply not partibly combatible with any pointer type.
> But the limit of "int" is *not* defined as 64K ! This is a
> misconception. The C language defines the *minimum* range for an
> "int" as -32767 to 32767. But a compiler is free to define other
> larger ranges, like for instance some compiler have 32 bit "int"
> instead of 16 bit. The compiler must however set the constants "INT_MIN"
> and "INT_MAX" in <limits.h> the the correct upper and lower limits.

> Stephan
> (initiator of the campaign against grumpiness in c.l.c)

Two mistakes on my side.  First, I should have said "e.g." instead of
"i.e.".  I am aware that the size of ints depends upon the platforms and
compilers.  In fact, all the platforms I work on have 32-bit ints.

Second, I should have said "0x00000000 - 0xFFFFFFFF" instead of "(0
to)64K".  That would have hidden my yet another mistake of referring to
an "unsigned int" as "int". :)

Dai

--
Daigoro Toyama
Software Engineer
Vista Development Corporation
http://vista.raima.com



Tue, 24 Oct 2000 03:00:00 GMT  
 Thought I knew what I was doing

Hi
I have noticed you help many people in the c newsgroup and I will appreciate
if you could help me with this:
What is the best way to implement dynamic arrays (like the  Vector class in
Java or Dynamic arrays in VB)
Thanks
Boaz
Israel

Quote:


>> I think I finally understand what this all means, and why omitting
>> <stdlib.h> and explicitly casting malloc() is really an error.  In
>> addition to the possible problem John explained above, I'd also imagine
>> that if the compiler assumes malloc() returns an int (as is the case when
>> you don't include <stdlib.h>), any returned value from malloc() larger
>> than the maximum limit of int (i.e. 64K) will be truncated to fit the
>> size of int before explicitly cast back to the pointer.

>The type "int" is simply not partibly combatible with any pointer type.
>But the limit of "int" is *not* defined as 64K ! This is a
>misconception. The C language defines the *minimum* range for an
>"int" as -32767 to 32767. But a compiler is free to define other
>larger ranges, like for instance some compiler have 32 bit "int"
>instead of 16 bit. The compiler must however set the constants "INT_MIN"
>and "INT_MAX" in <limits.h> the the correct upper and lower limits.

>Stephan
>(initiator of the campaign against grumpiness in c.l.c)



Wed, 25 Oct 2000 03:00:00 GMT  
 Thought I knew what I was doing

Quote:

>Hi
>I have noticed you help many people in the c newsgroup and I will appreciate
>if you could help me with this:
>What is the best way to implement dynamic arrays (like the  Vector class in
>Java or Dynamic arrays in VB)

I'd visit the FAQ at http://www.eskimo.com/~scs/C-faq/top.html and
see 16.14 - 16.16.

--
Craig

Manchester, NH
I love the smell of commerce in the morning.
  -- Brodie, "Mallrats"



Wed, 25 Oct 2000 03:00:00 GMT  
 Thought I knew what I was doing

Quote:

> Hi
> I have noticed you help many people in the c newsgroup and I will appreciate
> if you could help me with this:
> What is the best way to implement dynamic arrays (like the  Vector class in
> Java or Dynamic arrays in VB)

Hi Boaz Binnun,

I would help to know, what kind of array you have in mind. A simple
one dimensional array can be allocated by one simple "malloc()" call.
It gets more complicated with multidimensional arrays and there is more
than one solution.

A lot of fine, well written and interesting stuff on array and dynamic
memory allocation is available in the comp.lang.c FAQ, especially in
the following section:
  Section 6.  Arrays and Pointers

The answer to this question includes some good sample code:
   6.16:   How can I dynamically allocate a multidimensional array?

You can get the FAQ at http://www.eskimo.com/~scs/C-faq/top.html or
at ftp://rtfm.mit.edu/pub/usenet/comp.lang.c/C-FAQ-list and it gets
posted to this newsgroup and to news.answers regularly (at the
beginning of each month).

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Fri, 27 Oct 2000 03:00:00 GMT  
 
 [ 23 post ]  Go to page: [1] [2]

 Relevant Pages 
 

 
Powered by phpBB® Forum Software