473,414 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

return an int from a function

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
9 1386

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
>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
"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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: strout | last post by:
function F(e) { return function(){P(e)} } Can anybody tell me what the code is doing? If return another function all in a function I would do function F(e)
10
by: LaEisem | last post by:
On-the-job, I have "inherited" a lot of old C language software. A question or two about when "casting" of null pointer constants is needed has occurred during behind-the-scenes cleanup of some...
16
by: G Patel | last post by:
Hi, If I want to call functions that don't return int without declaring them, will there be any harm? I only want to assign the function(return value) to the type that it returns, so I don't...
15
by: Greenhorn | last post by:
Hi, when a function doesn't specify a return type ,value what value is returned. In the below programme, the function sample()is returning the value passed to 'k'. sample(int); main() { int...
23
by: Nascimento | last post by:
Hello, How to I do to return a string as a result of a function. I wrote the following function: char prt_tralha(int num) { int i; char tralha;
3
by: Thomas Scheiderich | last post by:
I am curious as to why ASP.NET returns values a different way from VB or VB.net (or can you use both). In my one book I have it returning using a return statement ...
12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
7
by: nafri | last post by:
hello all, I want to create a function that returns the first element of the Array that is input to it. However, the Input Array can be an Array of points, double, or anyother type, which means...
2
by: mosesdinakaran | last post by:
Hi everybody, Today I faced a problem where I am very confused and I could not solve it and I am posting here.... My question is Is is possible to return a value to a particular function ...
7
by: Terry Olsen | last post by:
How do I get this to work? It always returns False, even though I can see "This is True!" in the debug window. Do I have to invoke functions differently than subs? Private Delegate Function...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.