By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,408 Members | 945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,408 IT Pros & Developers. It's quick & easy.

return an int from a function

P: n/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);
}
Jul 10 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a

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*/

Jul 10 '06 #2

P: n/a
>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);
}
Jul 10 '06 #3

P: n/a
"a" <a@mail.comwrites:
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) ks***@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.
Jul 10 '06 #4

P: n/a
jjf

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);
}
Jul 10 '06 #5

P: n/a
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/

Jul 10 '06 #6

P: n/a
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;
}

Jul 10 '06 #7

P: n/a
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."

Jul 10 '06 #8

P: n/a
On 2006-07-10, a <a@mail.comwrote:
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
Jul 10 '06 #9

P: n/a
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 ---
Jul 10 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.