Re: Problem with malloc

From: Tom St Denis (tom_at_securescience.net)
Date: 04/04/04


Date: Sun, 04 Apr 2004 13:47:19 GMT

Chris Fogelklou wrote:

>> > Anyone know why engineers get such a bad reputation for being
> antisocial?
>> > Instead of wasting my time berating you for being a "stupid windows
> user"
>> > I'll answer the question...
>>
>> And even at that you failed.
>
> Hmmm... thanks.

You are most certainly welcome.

>> That's not the prototype for malloc. It's
>>
>> void *malloc(size_t size);
>
> It's irrelevant to the answer, and normally size_t is an int anyway. I
> could have said it's a size_t, but it might have created more problems
> than it solved for the question-asker.

Um first off, no "size_t" isn't int. I'd think if anything it would be
"unsigned" not just an int. Anyways the type is size_t. Why bother
replying with "seemingly useful facts" if you're just going to mess with
them?

>> > So, you get a void pointer in return... you need to cast it to
>> > something useable, like your matrix, for instance...
>>
>> Um, no. In C you need not cast void pointers.
>
> Um, yes you do, if you want truly portable code.

No, no you don't. The C standards [C90, C99] don't require you to cast a
void pointer to any other pointer type. If you're code is supposed to work
in C++ as well then maybe. However, putting the cast in can mask a more
serious problem that you forget to include stdlib.h.

>> > //Allocate memory for matrix and store in pointer. Cast so compiler
>> > doesn't puke.
>> > my_matrix_t *pmatrix = (my_matrix_t *)malloc(sizeof(my_matrix_t));
>>
>> If your compiler pukes at that (without the cast) then you need a new
>> compiler.
>
> Alright... Do you want to write one up for our embedded system? There
> have two compilers, one based on GCC, and neither of them like it if you
> equate two pointer types to each other without casting.

Um dude I've coded for embedded platforms before [from 8085, AVR, 68xx
series to the ARM based boards] and in all cases I've never casted a void
pointer to something.

In particular for the ARM boards you can use GCC and it will accept the
non-cast code just fine.

Me thinks you might want to preface your responses with "I used casts cuz my
non-standard compiler asked me to". This is afterall comp.lang.c and we're
not talking about "vendor lang c"

>>>This isn't dynamic though [hint: read the OPs question] it's always 20x20
> This is true, but I got the impression that he had no idea how to use
> malloc. I was trying to solve his confusion relating to malloc itself.

Point him to the manual pages. Why bother answering his question with code?
How the hell do you think all these "coders" get out there? They take an
ounce of knowledge and spread it so thin to get a job....

If the dude can't learn from a manual page [or googling on the net] then too
F'ing bad.

Tom



Relevant Pages

  • Re: problem with memcpy and pointers/arrays confusion - again
    ... this second method is known as an explicit conversion, or cast. ... The cast, in effect, tells the compiler: ... the malloc function. ... function taking a size_t as a parameter and returning a void pointer (i.e. ...
    (comp.lang.c)
  • Re: Why the compiler applies sign extension to unsigned data?
    ... I want the value of p to be made unsigned *before* the cast. ... want the cast to generate a sign extension. ... ULONG_PTR is ULONG and casting the pointer to ULONG_PTR first is the ... on the compiler and on the knowledge of the size of the ptr variable, ...
    (microsoft.public.development.device.drivers)
  • Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
    ... If your compiler ... it works with a cast. ... Pointer arithmetic, as you probably know, is scaled by ... not sure about concerning realloc(). ...
    (comp.lang.c)
  • Re: Another ANSI C question about volatile
    ... > The cast is on the address of x. ... > lvalue of type 'pointer to volatile int'. ... > volatile, ... > that a compiler, needing to reread xp each time, would then ...
    (comp.lang.c)
  • Re: Problem with malloc
    ... In C you need not cast void pointers. ... >> If your compiler pukes at that then you need ... Errors from such statements usually indicate a failure to ...
    (comp.lang.c)