LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-21-2010, 05:56 AM   #1
man s
LQ Newbie
 
Registered: Dec 2009
Posts: 14

Rep: Reputation: 0
Local Variables anomaly!!


I recently came across a piece of code that caught my attention:

main(){

char *s;
char fun();

s=fun();
printf("%s",s);
}
char *fun(){
char buffer[30];
strcpy(buffer,"Hello");
return buffer;
}
Which gives an error: buffer is a local variable and it doesn't exist in main function......
But if I run a code like:

main(){
int i;
i=new_fun();
printf("%d",i);
}

int new_fun(){
int temp;
temp= 10;
return temp;
}

It runs just fine...
Why is the anomaly in both codes present..??
 
Old 03-21-2010, 06:08 AM   #2
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 96
The ANSI standard states that main() returns an integer. buffer is not integer.
 
Old 03-21-2010, 07:31 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,442

Rep: Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880
Since arrays are returned by reference, when you return an array you are only returning a reference to it. If the array was declared within the function then its memory would have been released by the time you use its reference outside the function.

I would also point out that your 2 versions of code are not similar as the second is not using arrays
 
Old 03-21-2010, 09:10 PM   #4
man s
LQ Newbie
 
Registered: Dec 2009
Posts: 14

Original Poster
Rep: Reputation: 0
Thanx grail...yeah I almost got it..But are integers by default returned by value?...doesn't it always involve the overhead of extra memory space..??
 
Old 03-22-2010, 01:22 AM   #5
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Code:
#include <stdio.h>

int
myfunc (int a)
{
  return a + 2;
}

int
main (int argc, char *argv[])
{
  printf ("myfunc(2)= %d\n", myfunc (2));
  return 0;
}
As you can guess, this function will return "4".

Parameter "a" was definitely "passed by value" ...
... and function "myfunc()" definitely returns an "int" ...
... but I wouldn't see the value 4 was "returned by value". I'd merely say "function 'myfunc ()' returned an int".

PS:
This applies equally to C and C++.

The big differences here are:
1) C++ absolutely *requires* you to specify the return value type (or specify "void" if there is *no* return value), classic "C" would default to "return int". You should *always* explicitly declare your return value, in *both* C and C++. Whether you get a compile error or not

2) C++ would allow either/both the parameter and/or return value to be a reference ("&"). C doesn't support references (at least not the last time I looked). Pointers: yes. References: no.

'Hope that helps .. PSM

PPS:
No, there's no "extra memory space". An integer return value is usually copied to a register (for example, "eax" for an Intel CPU).

Last edited by paulsm4; 03-22-2010 at 01:23 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ssh - using variables in call to start remote script from local script babag Linux - Networking 2 06-03-2008 04:50 PM
virtual address of local variables dale_chip Programming 1 06-28-2007 10:56 AM
Garbage Collection in C : Local variables question duryodhan Programming 13 12-04-2006 07:16 AM
Using DDD to watch Args and Local Variables jonty_11 Programming 3 03-15-2005 12:01 PM
Local vs Global variables wujee Programming 1 03-11-2005 11:43 PM


All times are GMT -5. The time now is 11:40 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration