Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  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:


char *s;
char fun();

char *fun(){
char buffer[30];
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:

int 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
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
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,529

Rep: Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896
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
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

#include <stdio.h>

myfunc (int a)
  return a + 2;

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

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

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.


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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:57 PM.

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