return an int from a function

A

a

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);
}
 
H

Haroon Shafiq

a said:
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*/
 
G

Gordon Burditt

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
 
K

Keith Thompson

a said:
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.
 
J

jjf

a said:
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.
 
C

Chris Dollin

a said:
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.
 
M

Martin Ambuhl

a said:
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;
 
N

Nick Keighley

Haroon said:

oh no it doesn't


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.
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."
 
A

Andrew Poelstra

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.
 
J

Joe Wright

a said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top