allocate memory for function 
Author Message
 allocate memory for function

Do I need to "memory allocate" and "free memory" for function which
arguments are used pointer type?

like as:

 Function(int *a, int *b)

I'm always get memory corrupt,but don't know what happen...

Thanks

Terry



Tue, 04 Feb 2003 03:00:00 GMT  
 allocate memory for function
Once upon a while in a tremendously nice article

Quote:

> Do I need to "memory allocate" and "free memory" for function which
> arguments are used pointer type?

> like as:

>  Function(int *a, int *b)

> I'm always get memory corrupt,but don't know what happen...

> Thanks

> Terry

Hello Terry,

In your program you allways have to allocate memory you want to
use before you use it. This means that if you wan to access memory
pointed to by a pointer passed to your function this memory
either needs to be allocated prior to the call to your function
or your function should allocate the memory it needs to use.
If the calling function gives you allocated memory (I think most
regulars don't like it the other way around) your function should
know about the size of that memory to guarantee that it does not
access memory outside of that buffer.
If you just want to access a simple integer the caller
would write:

...
int firstthingigivetofunction, secondthingigvetofunction

Function(&firstthingigivetofunction, &secondthingigvetofunction);
...

Please also note that your function should have a return type
or should be declared with void, C99 does not allow implicit
declarations anymore. (If I understand it right!)

        Z

--
LISP is worth learning for the profound enlightenment experience you
will have when you finally get it; that experience will make you a
better programmer for the rest of your days.         Eric S. Raymond



Tue, 04 Feb 2003 03:00:00 GMT  
 allocate memory for function

Quote:

>Do I need to "memory allocate" and "free memory" for function which
>arguments are used pointer type?

>like as:

> Function(int *a, int *b)

>I'm always get memory corrupt,but don't know what happen...

Er, say what? If you call such a function, then its arguments must point to
valid memory locations, yes. The variables passed in don't have to have
been dynamically allocated, though.

#include <stdlib.h>

void Add(int *a, int *b)
{
    *a += *b; /* add a to b and return the result in a */

Quote:
}

int main(void)
{
    int a = 1, b = 2;
    int *pa = calloc(1, sizeof *pa), *pb = calloc(1, sizeof *pb);
    int *pc, *pd;

    Add(&a, &b); /* okay */
    Add(pa, pb); /* okay */
    Add(pc, pd); /* bad */
    Add(&1, &2); /* bad */
    return 0;

Quote:
}

--
Hong Ooi                    | Centre for Maths and its Applications/

Ph: (02) 6267 4140          | Australian National University
                            | ACT 0200 Australia


Tue, 04 Feb 2003 03:00:00 GMT  
 allocate memory for function

Quote:

> Do I need to "memory allocate" and "free memory" for function which
> arguments are used pointer type?

> like as:

>  Function(int *a, int *b)

> I'm always get memory corrupt,but don't know what happen...

No idea; it depends completely on how you use this function. For
example, you could do this:

int x, y;
Function(&x, &y);

No need to allocate or free memory.
Or you could do this:

int *x, *y;
...
Function(x, y);
...

In this case, somewhere in the first ... you must give x and y valid
values, or your function won't be able to do anything useful with them.
You might allocate some memory for them using malloc() or a related
function; you might point them at existing ints; you might even give
them the value NULL, if that's appropriate for Function().
You only need to free this memory if you've allocated it using the
*alloc() functions.

There are so many ways to do this, and it would be impossible to say
what is the right way for you. If you've got a specific problem with a
program, maybe you can post the smallest complete program that has this
problem?  We can probably help you better then.

Richard



Tue, 04 Feb 2003 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. allocating memory with functions

2. Allocating memory for function argument

3. allocating memory in one function and freeing it with another function

4. Allocating memory inside a function

5. allocating memory and processing thru function.

6. Do the string functions allocate memory?

7. standard variadic functions allocate memory?

8. getline function that allocates memory on-the-fly

9. allocate memory in a function

10. C functions that allocate memory

11. Freeing memory allocated inside a function

12. How to access memory allocated in function

 

 
Powered by phpBB® Forum Software