Re: Problem with malloc
From: Tom St Denis (tom_at_securescience.net)
Date: 04/04/04
- Next message: Tom St Denis: "Re: Problem with malloc"
- Previous message: Chris Fogelklou: "Re: Problem with malloc"
- In reply to: Chris Fogelklou: "Re: Problem with malloc"
- Next in thread: Chris Fogelklou: "Re: Problem with malloc"
- Reply: Chris Fogelklou: "Re: Problem with malloc"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Tom St Denis: "Re: Problem with malloc"
- Previous message: Chris Fogelklou: "Re: Problem with malloc"
- In reply to: Chris Fogelklou: "Re: Problem with malloc"
- Next in thread: Chris Fogelklou: "Re: Problem with malloc"
- Reply: Chris Fogelklou: "Re: Problem with malloc"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|