On Thu, Nov 3, 2011 at 12:41 AM, Tomek CEDRO <[email protected]> wrote:
> Hey,
>
> I have some problems with pointers and need some support plz ;-)
>
I'm not sure I understand what the problem is, but I can give some general
hints.
int swd_bus_read_ack(swd_ctx_t *swdctx, swd_operation_t operation, char
> **ack){
> swd_cmd_enqueue_miso_ack(swdctx, ack);
>
You discard the return value and one of the parameters, but perhaps this is
not the complete function?
}
>
> int swd_cmd_enqueue_miso_ack(swd_ctx_t *swdctx, char **ack){
> if (swdctx==NULL) return SWD_ERROR_NULLCONTEXT;
> int res;
> swd_cmd_t *cmd;
> cmd=(swd_cmd_t *)calloc(1,sizeof(swd_cmd_t));
>
As a side note, cmd = calloc(1, sizeof(*cmd)); is preferred, in case the
type of cmd changes later.
if (cmd==NULL) return SWD_ERROR_OUTOFMEM;
> if (ack!=NULL) *ack=&cmd->ack;
>
You probably want to wait with setting *ack until after checking res below.
If swd_cmd_enqueue fails, ack will point to free'd memory, but you discard
the return value in swd_bus_read_ack so the caller won't be able to tell.
Leave *ack unchanged unless the function succeeds.
cmd->bits=SWD_ACK_BITLEN;
> cmd->cmdtype=SWD_CMDTYPE_MISO_ACK;
> res=swd_cmd_enqueue(swdctx, cmd); //should be 1 on success
> if (res<1) free(cmd);
> return res;
> }
>
> main(){
> int *ack;
swd_bus_read_ack(swdctx, operation, ack);
>
This won't even compile. You pass a pointer-to-int, but swd_bus_read_ack
expects a pointer-to-pointer-to-char.
}
>
> The problem is:
> 1. I need to use double pointers to return back the address of the
> queue element (*ack=&cmd->ack).
>
Correct.
2. If I use single pointer *ack the value of the ack is only changed
> inside swd_cmd_enqueue_miso_ack() but after its return to
> swd_bus_read_ack() the ack value is taken back to the state as it was
> before swd_cmd_enqueue_miso_ack() call.
>
You have already concluded that you need a double pointer in 1, so I don't
know why you say this. A single pointer won't work.
3. I have tried to use single pointer *ack and call
> swd_bus_read_ack(swdctx, operation, &ack) but is changes nothing.
>
Ditto. You're reusing the same name (ack) for several things in your
description (two function parameters and a variable) so it's hard to
understand what you mean.
> This makes impossible to give back data on queue_dp_read(). There is
> something wrong with these pointers!!! Help! :-)
>
_______________________________________________
Openocd-development mailing list
[email protected]
https://lists.berlios.de/mailman/listinfo/openocd-development