Re: Dynamically allocating memory for handles

From: Bert (maatjesharing_at_gmx.de)
Date: 07/09/04


Date: 8 Jul 2004 16:06:24 -0700

Robert W Hand <rwhand@NOSPAMoperamail.com> wrote in message news:<0nmqe0legl984vp3dah4t91nvims9gfion@4ax.com>...
>
> In C, you do not need the cast, but you may be compiling in C++ where
> you do need it.

Ok, it's compiled as C.

> sizeof(thread_handle) is wrong. It should be sizeof
> *thread_handle or sizeof(HANDLE).
>
> HANDLE* thread_handle = malloc(max_threads*sizeof *thread_handle);

HANDLE* thread_handle;

thread_handle=malloc(max_threads*sizeof(*thread_handle));

I assume that is ok?

> You are allocating max_threads-number of objects of type HANDLE. So
> you need the size of HANDLE, not the size of pointer to HANDLE. That
> said, it might not make a difference if HANDLE and pointer to HANDLE
> are the same size.

Thanks for explaining that.

Should handles be initialized? Is there a benefit?

Will this work:

HANDLE* thread_handle;

thread_handle=calloc(max_threads, sizeof(*thread_handle));
if (thread_handle==NULL) return 1;

> >if (thread_handle==NULL) return 1;
> >
> >thread_handle[handle_counter]=(HANDLE) _beginthreadex(NULL, 0, (void
> >*) func, &data, 0, NULL);
>
> I have no way to know if handle_counter is non-negative

It's set to zero.

handle_counter=0;

(for loop to point to a data struct)
{ thread_handle[handle_counter]=(HANDLE) _beginthreadex(NULL, 0, (void
*) func, &data, 0, NULL);

if (thread_handle[handle_counter]!=0) handle_counter++;
}

> and less than max_threads.

That's checked too in the loop.

> Is the cast safe?

It's an unsigned int (32 bit) being cast to HANDLE.

http://msdn.microsoft.com/library/en-us/vclib/html/_crt__beginthread.2c_._beginthreadex.asp

> Unless you provide the definitions,
> the details are off-topic here. HTH.

I know. Thanks for your answer.

Bert



Relevant Pages

  • Re: Independent thread fireing events in ATL Service synchronization requirements?
    ... and hence the loop will not execute, ... m_vec itself is not a pointer, so it cannot be NULL for sure. ... >> Did you lock while adding and removing your objects to the list? ... >>> and made my client list a class with synchronization inside ...
    (microsoft.public.vc.atl)
  • Faster 32-bit mul for IAR 78K0 3.34B
    ... Browsed couple days ago throuh the IAR 78K0 C 3.34B libraries and was somehow disapointed to notice that the the 32 bit multiplication was a 'traditional' one, i.e. one that does not use the microcontroller's own multiply instruction. ... ; On the outer loop the 'result' and 'right' bytes are processed; at the same rate, so the hl pointer and pointer + fixed offset; can be used to access a byte from both of the variables. ...
    (comp.arch.embedded)
  • Re: [PATCH] fix/improve generic page table walker
    ... caused by the unconditional loop over all pgd/pud entries as specified ... Which means that the pgd and/or the ... pud pointer can get out-of-bounds causing all kinds of mayhem. ... and do nothing with the pointer if it is a lower level page table. ...
    (Linux-Kernel)
  • Re: FOR ... NEXT
    ... CELLS SWAP +! ... makes automating the loop straightforward. ... CSET-POINTER ... CSTART-LOOP (end-value start-value pointer) ...
    (comp.lang.forth)
  • Re: Threading problem
    ... Note that if you manage to delete the form while this loop is running, ... but if this is a pointer to free storage on the ... and close the form when the reference ... the thread instead of relying on the GetSafeHwnd() call. ...
    (microsoft.public.vc.mfc)