Re: Dynamically allocating memory for handles
From: Bert (maatjesharing_at_gmx.de)
Date: 07/09/04
- Next message: Barry Schwarz: "Re: passing pointers [C]"
- Previous message: Chris Newton: "Re: Default constructors, passing argument"
- In reply to: Robert W Hand: "Re: Dynamically allocating memory for handles"
- Next in thread: Robert W Hand: "Re: Dynamically allocating memory for handles"
- Reply: Robert W Hand: "Re: Dynamically allocating memory for handles"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Barry Schwarz: "Re: passing pointers [C]"
- Previous message: Chris Newton: "Re: Default constructors, passing argument"
- In reply to: Robert W Hand: "Re: Dynamically allocating memory for handles"
- Next in thread: Robert W Hand: "Re: Dynamically allocating memory for handles"
- Reply: Robert W Hand: "Re: Dynamically allocating memory for handles"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|