Yes the malloc is ok.
NULL is defined as (void *) 0 and that's not the content of stream, i did
your test and he didn't put 0xaa on the output.
For example:
if(stream == NULL)
{
P2 = 0xaa;
}
else
{
P2 = 0xf0;
}
gives 0xf0 on the output
Even the simplest main with malloc fails:
void main(void)
{
unsigned char * stream;
stream = (unsigned char *) malloc(1);
if(stream == NULL)
{
P2 = 0xf0;
}
else
{
*stream = 0xaa;
P2 = *stream;
}
}
Gives nothing on the output port P2
I also took a look at the assembly file and it looks fine to me for all my
test programs.
I see that he loads 1 in the dptr, probably the 1 byte i want to allocate
with malloc, then he lcall's the subroutine _malloc, then he moves the 3
byte address in r2, r3, r4. then the NULL check is nothing more than 3 times
a compare and jump not equal on 0 value followed by placing 0xf0 on P2 like
it should be.
In the other case i see that the pointer address is loaded, followed by
putting 0xaa in the accumulator followed by an lcall to __gptrput like it
should be.
Then the pointer address is moved again in the registers followed by a lcall
to __gptrget and a move of the accumulator to P2 since __gptrget stocks his
result in the accumulator.
; main.c:12: test = (unsigned char *) malloc(1);
mov dptr,#0x0001
lcall _malloc
mov r2,dpl
mov r3,dph
mov r4,#0x00
; main.c:13: if(test == NULL)
cjne r2,#0x00,00102$
cjne r3,#0x00,00102$
cjne r4,#0x00,00102$
; main.c:15: P2 = 0xf0;
mov _P2,#0xF0
sjmp 00105$
00102$:
; main.c:19: *test = 0xaa;
mov dpl,r2
mov dph,r3
mov b,r4
mov a,#0xAA
lcall __gptrput
; main.c:20: P2 = *test;
mov dpl,r2
mov dph,r3
mov b,r4
lcall __gptrget
mov _P2,a
It makes no sense to me, the only thing i can think of is that the compiler
doesnt make the right hex file for loading in my µc...
Still thanks for the reply.
2009/7/3 Bodo Wenzel <[email protected]>
> > if(stream == NULL)
> > return NULL;
> Are you sure that malloc() returned a valid pointer?
>
> For debugging I would change this code into:
>
> if (stream == NULL) {
> P2 = 0xAA;
> return NULL;
> }
>
> Or, as I know assembler, I would look into the machine code to check it
> does
> The Right Thing(TM).
>
> HTH, Bodo
>
>
>
> ------------------------------------------------------------------------------
> _______________________________________________
> Sdcc-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/sdcc-user
>
------------------------------------------------------------------------------
_______________________________________________
Sdcc-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sdcc-user