Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > malloc inside function (I know... I *did* search google first ;)

Reply
Thread Tools

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

 
 
spasmous@yahoo.com
Guest
Posts: n/a
 
      08-23-2005
Wow, you boys are hardcore on here! Rock on.

So I think I understand the problem better now, f needs to be passed as
a pointer to a pointer. Thanks

 
Reply With Quote
 
 
 
 
James Daughtry
Guest
Posts: n/a
 
      08-23-2005
> main()
This is a confusing definition for main, and it encourages undefined
behavior by making newcomers think that main returns nothing when in
fact it returns int. Be explicit because eventually the C99 standard
will become widely implemented and this definition will fail to
compile.

int main(void)
{
/* Your code here */
return 0;
}

> void initialize_f(float * f)
> {
> long n = 100;
> f = (float*)malloc(n*sizeof(float)*);
>
> f[1] = ... // etc. fill f array here
> }


This is all kinds of wrong. Because f is a copy of the original
pointer, you lose the memory you assign to it when the function
returns. You also cast malloc, thus hiding the error of not including
stdlib.h. Also, since the size of the array is a local variable, and
initialize_f returns void, how does the calling function know how much
memory was allocated? You also never declare initialize_f before using
it in main. How about this instead:

#include <stdlib.h>

int initialize_f(float **f)
{
int n = 100;

*f = malloc(n * sizeof(float));

/* Fill the array */

return n;
}

int main(void)
{
float *f;
int n = initialize_f(&f);

/* Use f for processing */

free(f);

return 0;
}

Of course, it looks to me like you could more easily keep the
allocation and release in main by having a function that returns n,
allocating the memory in main using that n, then calling initialize_f
to fill it with whatever values you're using. It's easier to keep your
head about you if you avoid allocating memory in one function and
releasing it elsewhere.

 
Reply With Quote
 
 
 
 
Roman Mashak
Guest
Posts: n/a
 
      08-24-2005
Hello, Michael!
You wrote on Tue, 23 Aug 2005 22:47:33 +0200:

[skip]
MM> return 0;
MM> }

MM> size_t initialize_f (float **fptr)
MM> {
MM> size_t i, n = 100;
Tiny question: what's the reason of declaring function's return type as
'size_t' ?
MM> *fptr = malloc(n * sizeof **fptr);

With best regards, Roman Mashak. E-mail: http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      08-24-2005
Roman Mashak wrote:
> Hello, Michael!
> You wrote on Tue, 23 Aug 2005 22:47:33 +0200:
>
> [skip]
> MM> return 0;
> MM> }
>
> MM> size_t initialize_f (float **fptr)
> MM> {
> MM> size_t i, n = 100;
> Tiny question: what's the reason of declaring function's return type as
> 'size_t' ?
> MM> *fptr = malloc(n * sizeof **fptr);
>
> With best regards, Roman Mashak. E-mail: (E-Mail Removed)


You snipped the answer given by me: If f is used to access an "array"
instead of, say the head of a linked list, it is often advantageous
to pass along the information how large the storage pointed to by f
is. There are other situations where you want to hide exactly this
information from the user but they are comparatively rare. A return
value is often a better solution to overcome the difficulty of
communicating the size than using file scope identifiers with static
or external linkage.
If the information is not needed right now: fine. Then just use the
return value to detect success/failure in the calling functions.
I just hate the concept of working with an object used as array
without knowing the size -- then often implicit assumptions are
used like "n is known to be >= 3". This is crap as the base for
the assumption may subtly change or someone made a simple mistake.

Personally, I use void return value mostly for functions which
cannot fail and have/gather no information the caller does not
have at its fingertips but which he is allowed to have.


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      08-24-2005
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

 
Reply With Quote
 
spasmous@yahoo.com
Guest
Posts: n/a
 
      08-24-2005
Thanks Lawrence, at last some good advice here! I much prefer that way.
It's funny, the cranks here got so worked up over coding style they
forgot about substance

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      08-24-2005
(E-Mail Removed) wrote:

> Thanks Lawrence, at last some good advice here! I much prefer that
> way. It's funny, the cranks here got so worked up over coding style
> they forgot about substance


See any of a dozen recent messages about quoting some of the previous
message and how to do it from Google.



Brian
 
Reply With Quote
 
spasmous@yahoo.com
Guest
Posts: n/a
 
      08-24-2005
Do you like top posting as well? I do


Default User wrote:
> (E-Mail Removed) wrote:
>
> > Thanks Lawrence, at last some good advice here! I much prefer that
> > way. It's funny, the cranks here got so worked up over coding style
> > they forgot about substance

>
> See any of a dozen recent messages about quoting some of the previous
> message and how to do it from Google.
>
>
>
> Brian


 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      08-24-2005
(E-Mail Removed) wrote:

> Do you like top posting as well? I do



Sure, it identifies the idiots.

*plonk*


Brian

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-24-2005
(E-Mail Removed) writes:
> Thanks Lawrence, at last some good advice here! I much prefer that way.
> It's funny, the cranks here got so worked up over coding style they
> forgot about substance


First, please provide context (and in reference to your later
followup, please don't top-post).

Second, there's no reason we can't both answer the substance of your
question and offer suggestions about coding style. I suggested using
a function in a followup I posted yesterday.

Note, however, that if the function is expected both to allocate and
initialize the array *and* to determine its size, you'll want to have
it return both the allocated size and a pointer to the array to the
caller. That's more difficult to do through a function result. You
could return a struct, but that might not fit as well into the context
in which the function is called.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Defining a function inside a function. Whats this feature ? How touse inside a class ? Sur Ruby 4 01-08-2008 02:50 PM
Malloc/Free - freeing memory allocated by malloc Peter C Programming 34 10-22-2004 10:23 AM
free'ing malloc'd structure with malloc'd members John C Programming 13 08-02-2004 11:45 AM
Re: free'ing malloc'd structure with malloc'd members ravi C Programming 0 07-30-2004 12:42 PM
How can I use malloc-ed memory inside a local function Simon Nickerson C Programming 8 09-09-2003 05:44 PM



Advertisments