Re: malloc inside function (I know... I *did* search google first ;)



On Tue, 23 Aug 2005 16:36:59 -0400, Charles M. Reinke wrote:

....

>> /* Untested code follows */
>> long initialize_f(float *f) {
>> int i;
>> long n = 100;
>> f = malloc((size_t)n * sizeof *f);
>>
>> for(i=0; i<n; i++) {
>> f[i] = 0; /* or whatever initialization value you want */
>> } /* for i */
>>
>> return n;
>> } /* initialize_f */
>>
>> -Charles
>>
>>
>
> Of course, I made (at least) 2 errors--f must be passed by address, i.e.
> long initialize_f(float **f) {
> /* corrected code*/
> }
> and the return value of malloc should ALWAYS be checked for NULL, i.e.
> *f = malloc((size_t)n * sizeof **f);
> if(*f==NULL) {
> /* error handling code here */
> }
> else {
> /* initialization code here */
> }
> ...

Or IMO better still make the pointer the return value of the function

float *initialize_f(long *pn)
{
long i;
long n = 100;
float *f = malloc((size_t)n * sizeof *f);

if (f != NULL) {
for (i=0; i<n; i++) {
f[i] = 0; /* or whatever initialization value you want */
}

if (pn != NULL)
*pn = n;
}

return f;
}

Functions that allocate memory commonly return a pointer to the memory
allocated. That pointer is the primary return value from the function and
a null return value can be used to indicate failure as other functions
like malloc() do. The size value may or may not be needed depending on
context. The code allows a null pointer to be passed if it is not needed.
If it is never needed the pn parameter can be omitted completely from the
function definition, and then the float * return is certainly more natural
than using a float ** parameter and no return value.

Also some thought should be put into the function name. "initialize_f" is
a misleading name for a function that allocates the array, not just
initialises it.

Lawrence

.



Relevant Pages

  • Re: problems with free()
    ... No need to initialize mystr to NULL, ... you could just as easily put the malloc ... printf("Could not allocate memory"); ... array into the pointer object mystr. ...
    (comp.lang.c.moderated)
  • Re: Does malloc() reuse addresses?
    ... if i allocate some memory with malloc() and later free it (using ... allocate memort at the same starting address and will return the same ... In this example, if an instance is freed, and a pointer to it becomes ... If you are doing what you say with the instance array, ...
    (comp.lang.c)
  • Re: Are the following 2 code statements equivalent
    ... In the calloc() case, the ... is not allowed to allocate some smaller amount. ... You call memsetwithout checking the result of the malloc() call. ... If mallocfails, it returns a null pointer, and the call to memset ...
    (comp.lang.c)
  • Re: initialising array of unknown size (newbie)
    ... malloc()) doesn't have a problem with this line, and you are using a C++ ... My understanding is that before that particular line, resultset ... is a pointer to a memory block big enough to hold a float. ...
    (comp.lang.c)
  • freeing memory....partially
    ... i would like to allocate two structures making only one malloc call. ... These two pointer are later used by two threads in mutual exclusion so ... Remember that i cannot use realloc beacause thread2 cannot access ptr1. ...
    (comp.lang.c)