|
|
return an int from a function
Hi
I would like to have a function to process an input integer and return the
result. The following code will generate a memory leak. How should I write
this idea properly?
Thanks
int func(int num){
//process the num, counting the loop
int result;
return result;
}
main(){
int result;
result = func(7);
}
|
|
0
|
|
|
|
Reply
|
a177 (44)
|
7/10/2006 5:30:25 AM |
|
a wrote:
> Hi
> I would like to have a function to process an input integer and return the
> result. The following code will generate a memory leak. How should I write
> this idea properly?
> Thanks
>
> int func(int num){
> //process the num, counting the loop
> int result;
> return result;
> }
> main(){
> int result;
> result = func(7);
> }
Try some thing like this.
/*CODE BEGINS*/
#include <stdio.h>
int* func(int num){
//process the num, counting the loop
int *result;
result = malloc(sizeof (int));
//check for any error during allocation
//assuming this function will return a NULL in case of an error.
return result;
}
main(){
int *result;
result = func(7);
//perform some usual sanity checks here.
//_dont_ forget to free the allocated memroy here.
if (result != NULL){
free(result);
result = NULL;
}
}
/*CODE ENDS*/
|
|
0
|
|
|
|
Reply
|
haroon.shafiq (38)
|
7/10/2006 6:08:20 AM
|
|
>I would like to have a function to process an input integer and return the
>result.
>The following code will generate a memory leak.
Do you have proof of this? None of the code actually shown will
leak memory. I see no calls to malloc() or cealloc() or realloc()
at all.
Gordon L. Burditt
>How should I write
>this idea properly?
>Thanks
>
>int func(int num){
>//process the num, counting the loop
>int result;
>return result;
>}
>main(){
>int result;
>result = func(7);
>}
|
|
0
|
|
|
|
Reply
|
gordon13 (229)
|
7/10/2006 6:40:36 AM
|
|
"a" <a@mail.com> writes:
> Hi
> I would like to have a function to process an input integer and return the
> result. The following code will generate a memory leak. How should I write
> this idea properly?
> Thanks
>
> int func(int num){
> //process the num, counting the loop
> int result;
> return result;
> }
> main(){
> int result;
> result = func(7);
> }
There is no memory leak in the code you posted.
If you have code that leaks memory, show it to us. We're not mind
readers.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <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.
|
|
0
|
|
|
|
Reply
|
kst-u (21464)
|
7/10/2006 7:47:49 AM
|
|
a wrote:
>
> I would like to have a function to process an input integer and return the
> result. The following code will generate a memory leak.
How?
> How should I write this idea properly?
The way you have it below.
> int func(int num){
> //process the num, counting the loop
> int result;
> return result;
> }
> main(){
> int result;
> result = func(7);
> }
|
|
0
|
|
|
|
Reply
|
jjf (517)
|
7/10/2006 7:51:41 AM
|
|
a wrote:
> Hi
> I would like to have a function to process an input integer and return the
> result. The following code will generate a memory leak. How should I write
> this idea properly?
> Thanks
>
> int func(int num){
> //process the num, counting the loop
> int result;
> return result;
> }
This code will not cause a memory leak, except by provoking
undefined behaviour (you read the value of an uninitialised
variable).
If you have real code with a real memory leak, and you can't trim it
down any more, then show us that real code.
--
Chris "real, imaginary, complex - all flavours of code" Dollin
"Reaching out for mirrors hidden in the web." - Renaissance, /Running Hard/
|
|
0
|
|
|
|
Reply
|
chris.dollin (1683)
|
7/10/2006 8:19:09 AM
|
|
a wrote:
> Hi
> I would like to have a function to process an input integer and return the
> result. The following code will generate a memory leak.
Nothing in your posted code can possibly produce a memory leak.
> How should I write
> this idea properly?
> Thanks
>
> int func(int num){
> //process the num, counting the loop
> int result;
> return result;
> }
> main(){
main returns an int; you should say so.
> int result;
> result = func(7);
and you should return that int:
return 0;
> }
>
>
|
|
0
|
|
|
|
Reply
|
mambuhl (2201)
|
7/10/2006 9:28:41 AM
|
|
Haroon Shafiq wrote:
> a wrote:
> > I would like to have a function to process an input integer and return the
> > result. The following code will generate a memory leak.
oh no it doesn't
> > How should I write this idea properly?
> >
> > int func(int num){
> > //process the num, counting the loop
> > int result;
> > return result;
if you had returned a pointer to result then you would have a problem.
But C *copies* the return value, hence the fact that result has
disappeared after the funtion has exited doesn't matter.
> > }
> > main(){
> > int result;
> > result = func(7);
> > }
>
> Try some thing like this.
>
> /*CODE BEGINS*/
> #include <stdio.h>
you forgot stdlib
> int* func(int num){
> //process the num, counting the loop
> int *result;
> result = malloc(sizeof (int));
> //check for any error during allocation
> //assuming this function will return a NULL in case of an error.
> return result;
> }
> main(){
> int *result;
> result = func(7);
> //perform some usual sanity checks here.
> //_dont_ forget to free the allocated memroy here.
> if (result != NULL){
> free(result);
> result = NULL;
> }
> }
> /*CODE ENDS*/
this is just plain silly. Would you seriously malloc() a single
integer?
I'm sure we dream up situations where this might make sense but
normally it is just WRONG.
--
Nick Keighley
Let me get this straight: I'm going to run the Linux so I can run a
Java RTE so I can run a PDP emulator so I can run Unix V7 so I
can rebuild V6 from the Lions-commented source in a book legally
reprinted from an illegally photocopied Australian book based on
source code that came from New Jersey by way of Wales.
To quote Calvin and Hobbes, "The theological implications are
staggering."
|
|
0
|
|
|
|
Reply
|
nick_keighley_nospam (4574)
|
7/10/2006 10:15:19 AM
|
|
On 2006-07-10, a <a@mail.com> wrote:
> Hi
> I would like to have a function to process an input integer and return the
> result. The following code will generate a memory leak. How should I write
> this idea properly?
> Thanks
>
> int func(int num){
> //process the num, counting the loop
Syntax error in C89. Not only that, but it appears that you snipped
all of the relevant source.
> int result;
> return result;
> }
> main(){
Implicit int not allowed in C99. Therefore, this program is invalid C
no matter what standard you are using.
> int result;
> result = func(7);
> }
>
You need to use spaced indentation on Usenet. There are lots of free
tools out there to do so for you.
--
Andrew Poelstra <http://www.wpsoftware.net/projects/>
To email me, use "apoelstra" at the above domain.
"You people hate mathematics." -- James Harris
|
|
0
|
|
|
|
Reply
|
apoelstra (387)
|
7/10/2006 6:09:59 PM
|
|
a wrote:
> Hi
> I would like to have a function to process an input integer and return the
> result. The following code will generate a memory leak. How should I write
> this idea properly?
> Thanks
>
> int func(int num){
> //process the num, counting the loop
> int result;
> return result;
> }
> main(){
> int result;
> result = func(7);
> }
>
>
If your real code looks like this..
#include <stdio.h>
int func(int num) {
int result;
result = num * num;
return result;
}
int main(void) {
int result;
result = func(7);
printf("%d\n", result);
return 0;
}
It should work perfectly well. There is no case for a memory leak here.
If you want meaningful help, show us a minimal but compilable example of
your program which exhibits the problem.
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
|
|
0
|
|
|
|
Reply
|
joewwright (1737)
|
7/10/2006 11:36:38 PM
|
|
|
9 Replies
23 Views
(page loaded in 0.171 seconds)
|
|
|
|
|
|
|
|
|