Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > return an int from a function

Reply
Thread Tools

return an int from a function

 
 
a
Guest
Posts: n/a
 
      07-10-2006
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);
}


 
Reply With Quote
 
 
 
 
Haroon Shafiq
Guest
Posts: n/a
 
      07-10-2006

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

 
Reply With Quote
 
 
 
 
Gordon Burditt
Guest
Posts: n/a
 
      07-10-2006
>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);
>}

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-10-2006
"a" <> 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- <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.
 
Reply With Quote
 
jjf@bcs.org.uk
Guest
Posts: n/a
 
      07-10-2006

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


 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      07-10-2006
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/

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      07-10-2006
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;

> }
>
>

 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      07-10-2006
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."

 
Reply With Quote
 
Andrew Poelstra
Guest
Posts: n/a
 
      07-10-2006
On 2006-07-10, 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


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
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      07-10-2006
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 ---
 
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 "template<typename T> ... function<T(int)>" not match "int(&)(int)" implicitly? Steve Hicks C++ 2 09-28-2009 05:24 PM
Difference between int i, j; and int i; int j; arun C Programming 8 07-31-2006 05:11 AM
int a[10]; int* p=(int*)((&a)+1); But why p isn't equal to ((&a)+1)? aling C++ 8 10-20-2005 02:42 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 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