Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > where to allocate memory

Reply
Thread Tools

where to allocate memory

 
 
gc
Guest
Posts: n/a
 
      11-06-2003
I am writing a function that given nx1 vector a and a nx1 b solves a
system of equations f(a,c)=b for a nx1 c.

While writing the function:
1] Should I allocate the memory for c within the function and return
the allocated memory?
something that leads to
double *solve(const double *a,const double *b,int n)
{
double *c;
/*blah blah*/
c=malloc(n*sizeof(*c));
/*blah blah*/
return c;
}

or,

2] Should I allocate the memory outside the function and then pass it
as a parameter?

void solve(const double *a,const double *b,double *c,int n);


/****/
c=malloc(n*sizeof(*c));
solve(a,b,c,n);
 
Reply With Quote
 
 
 
 
Simon Biber
Guest
Posts: n/a
 
      11-06-2003
"gc" <> wrote:
> While writing the function:
> 1] Should I allocate the memory for c within the function and
> return the allocated memory?
> or,
> 2] Should I allocate the memory outside the function and then
> pass it as a parameter?


Both are valid solutions in C. I generally prefer the latter form
as it gives the caller more control over how they want to manage
the memory. (Perhaps using an automatic array rather than malloced
storage). I think it also looks cleaner to have the malloc and its
corresponding free in the same function.

double a = malloc(n * sizeof *c);
double b = malloc(n * sizeof *c);

double c = malloc(n * sizeof *c);
if(a && b && c)
{
/* set up a, b */
solve(a, b, c, n);
/* use c */
free(a);
free(b);
free(c);
}

Or:

double a[] = {1, 2 /* ... */};
double b[] = {1, 2 /* ... */};
double c[sizeof a / sizeof *a];
solve(a, b, c, sizeof a / sizeof *a);
/* use c */

--
Simon.


 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      11-06-2003
gc wrote:

> I am writing a function that given nx1 vector a and a nx1 b solves a
> system of equations f(a,c)=b for a nx1 c.
>
> While writing the function:
> 1] Should I allocate the memory for c within the function and return
> the allocated memory?


If you like.

> something that leads to
> double *solve(const double *a,const double *b,int n)
> {
> double *c;
> /*blah blah*/
> c=malloc(n*sizeof(*c));


if(c != NULL)
{

> /*blah blah*/


}

> return c;
> }
>
> or,
>
> 2] Should I allocate the memory outside the function and then pass it
> as a parameter?


If you like.

>
> void solve(const double *a,const double *b,double *c,int n);
>
>
> /****/
> c=malloc(n*sizeof(*c));
> solve(a,b,c,n);




It's a design decision that only you can make. But I would look at it this
way - if you're going to need many solutions to different problems to exist
at the same time, then probably you'll find it more convenient for the
function to allocate the memory. If, on the other hand, your typical usage
scenario is that you solve /one/ problem, then another, then another,
without the solutions needing to co-exist, then it may be handier to pass
in the memory. That way, you can re-use it if you wish. Alternatively, pass
in a pointer to the memory, and get solve() to resize it as appropriate.

--
Richard Heathfield :
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
 
Reply With Quote
 
Roose
Guest
Posts: n/a
 
      11-06-2003
If there's no good reason to allocate it in the function, then I prefer to
have the caller allocate. You might always remember to free, but others
might not. If you make them write the malloc, they won't forget.

Also, the caller allocating is slightly better from a code reuse point of
view. Your solver is now tied to malloc, whereas some people use different
memory management schemes. I have no idea what system you're on, but if you
use multiple heaps or anything like that, you want to have the caller
allocate.

But if you're just writing some small, quick app, there's probably no
difference.

"gc" <> wrote in message
news: om...
> I am writing a function that given nx1 vector a and a nx1 b solves a
> system of equations f(a,c)=b for a nx1 c.
>
> While writing the function:
> 1] Should I allocate the memory for c within the function and return
> the allocated memory?
> something that leads to
> double *solve(const double *a,const double *b,int n)
> {
> double *c;
> /*blah blah*/
> c=malloc(n*sizeof(*c));
> /*blah blah*/
> return c;
> }
>
> or,
>
> 2] Should I allocate the memory outside the function and then pass it
> as a parameter?
>
> void solve(const double *a,const double *b,double *c,int n);
>
>
> /****/
> c=malloc(n*sizeof(*c));
> solve(a,b,c,n);



 
Reply With Quote
 
rihad
Guest
Posts: n/a
 
      11-06-2003
On Thu, 6 Nov 2003 18:01:06 +1100, "Simon Biber" <> wrote:

>double a = malloc(n * sizeof *c);
>double b = malloc(n * sizeof *c);
>
>double c = malloc(n * sizeof *c);
>if(a && b && c)
>{
> /* set up a, b */
> solve(a, b, c, n);
> /* use c */
> free(a);
> free(b);
> free(c);
>}


What if a failed, you wouldn't be freeing the other guys? I still prefer the
"goto to the function trailer" approach.
 
Reply With Quote
 
rihad
Guest
Posts: n/a
 
      11-06-2003
On Thu, 06 Nov 2003 12:48:51 +0400, rihad <> wrote:

>On Thu, 6 Nov 2003 18:01:06 +1100, "Simon Biber" <> wrote:
>
>>double a = malloc(n * sizeof *c);
>>double b = malloc(n * sizeof *c);
>>
>>double c = malloc(n * sizeof *c);


"Oh, *pointers* are simple - it's *typing* that's difficult. "

>>if(a && b && c)
>>{
>> /* set up a, b */
>> solve(a, b, c, n);
>> /* use c */
>> free(a);
>> free(b);
>> free(c);
>>}

>
>What if a failed, you wouldn't be freeing the other guys? I still prefer the
>"goto to the function trailer" approach.


Namely:
double a = malloc(n * sizeof *c);
double b = malloc(n * sizeof *c);

double c = malloc(n * sizeof *c);
if(!(a && b && c))
goto nomem;
/* set up a, b */
solve(a, b, c, n);
/* use c */

nomem:
free(a);
free(b);
free(c);
}

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      11-06-2003
gc wrote:
>
> I am writing a function that given nx1 vector a and a nx1 b solves a
> system of equations f(a,c)=b for a nx1 c.
>
> While writing the function:
> 1] Should I allocate the memory for c within the function and return
> the allocated memory?


> or,
>
> 2] Should I allocate the memory outside the function and then pass it
> as a parameter?


I prefer #2.
1 It's easier to remember to write a call to free,
if you've just written the call to malloc.
2 If the function is recursive, you save a lot of allocating.
3 The function is more versatile.
It's possible that sometime, you may get a chance to pass
the function an automatic array, instead.


--
pete
 
Reply With Quote
 
Al Bowers
Guest
Posts: n/a
 
      11-06-2003


rihad wrote:
> On Thu, 06 Nov 2003 12:48:51 +0400, rihad <> wrote:
>
>
>>On Thu, 6 Nov 2003 18:01:06 +1100, "Simon Biber" <> wrote:
>>
>>
>>>double a = malloc(n * sizeof *c);
>>>double b = malloc(n * sizeof *c);
>>>
>>>double c = malloc(n * sizeof *c);

>
>
> "Oh, *pointers* are simple - it's *typing* that's difficult. "
>
>
>>>if(a && b && c)
>>>{
>>> /* set up a, b */
>>> solve(a, b, c, n);
>>> /* use c */
>>> free(a);
>>> free(b);
>>> free(c);
>>>}

>>
>>What if a failed, you wouldn't be freeing the other guys? I still prefer the
>>"goto to the function trailer" approach.


Yes, that should be a concern which this code does not handle.
However, the code snippet above will not even compile for other
reasons.
>
>
> Namely:
> double a = malloc(n * sizeof *c);
> double b = malloc(n * sizeof *c);
>
> double c = malloc(n * sizeof *c);


Why do you repeat the flawed code?
a, b, c should all be a pointer type.
The variable c is being used before it has been declared.
Namely: A lot of errors in this short snippet of code.

> if(!(a && b && c))
> goto nomem;
> /* set up a, b */
> solve(a, b, c, n);
> /* use c */
>
> nomem:
> free(a);
> free(b);
> free(c);
> }
>


There is no need to do a goto here. Simply free a, b, and c
in the "trailer".

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

void solve(double *a, double *b, double *c, double n);

int main(void)
{
double n = 9000.00;
double *a = malloc(sizeof *a);
double *b = malloc(sizeof *b);
double *c = malloc(sizeof *c);

if(a && b && c)
{
solve(a, b, c, n);
printf("*a = %f\n*b = %f\n*c = %f\n",*a,*b,*c);
}
free(a);
free(b);
free(c);
return 0;
}

void solve(double *a, double *b, double *c, double n)
{
*a = log(n);
*b = log10(n);
*c = *a-*b;
printf("log(%.2f) = %f\n",n,*a);
printf("log10(%.2f) = %f\n",n,*b);
printf("log(%.2f) - log10(%.2f) = %f\n\n", *a, *b,
log(*a)-log(*b));
return;
}

--
Al Bowers
Tampa, Fl USA
mailto: (remove the x to send email)
http://www.geocities.com/abowers822/

 
Reply With Quote
 
Simon Biber
Guest
Posts: n/a
 
      11-06-2003
"rihad" <> wrote:
> On Thu, 06 Nov 2003 12:48:51 +0400, rihad <> wrote:
> >On Thu, 6 Nov 2003 18:01:06 +1100, "Simon Biber" <> wrote:
> >
> >>double a = malloc(n * sizeof *c);
> >>double b = malloc(n * sizeof *c);
> >>
> >>double c = malloc(n * sizeof *c);

>
> "Oh, *pointers* are simple - it's *typing* that's difficult. "


Indeed. Thanks, Richard

--
Simon.


 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      11-06-2003
(gc) wrote in message news:<. com>...

> I am writing a function that given nx1 vector a and a nx1 b solves a
> system of equations f(a,c)=b for a nx1 c.
>
> While writing the function:
> 1] Should I allocate the memory for c within the function and return
> the allocated memory?


<snip code>

> or,
>
> 2] Should I allocate the memory outside the function and then pass it
> as a parameter?


<snip>

you've had quite few answers by now. I'll only add I'd favour 2] by
the old design rule that every function should do only one thing. Have
a separate allocator.


--
Nick Keighley
 
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
Why does new allocate more memory than there is?? Alan Gifford C++ 4 10-27-2003 10:01 AM
allocate memory for a list array with templates OlgaM C++ 1 10-10-2003 03:55 AM
Re: FileChannel.map() gives 'cannot allocate memory' Roedy Green Java 3 08-14-2003 08:44 PM
Does this allocate memory? Curt C++ 37 07-26-2003 05:08 PM
iterators - do they allocate memory? sks_cpp C++ 7 07-04-2003 02:40 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57