linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Issue "address of local variable returned"
@ 2005-05-25  7:17 Amit Dang
  0 siblings, 0 replies; 4+ messages in thread
From: Amit Dang @ 2005-05-25  7:17 UTC (permalink / raw)
  To: linux-c-programming

Hi All,
    I am facing following issue

Following piece of code gives warning: "address of local variable `op'
returned"
#################################
#include<iostream>
using namespace std;

struct s {
    char op[24];
    int j;
};

char* fun()
{
    struct s op;
    strcpy(op.op, "898898898\0");
    return op.op;
}

int main()
{
    cout << fun() << endl;
    return 0;
}
#################################
But when the structure is change no warning is issued
Following is the new structure
#################################
struct s {
        int j;
        char op[24];
};
#################################

Obviously I know that "op.op" should not be returned.

Thanks in advance,
Amit Dang


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Issue "address of local variable returned"
@ 2005-05-25  7:27 Amit Dang
       [not found] ` <2ab8d39a05052610175e3d7018@mail.gmail.com>
  2005-05-27 16:09 ` Glynn Clements
  0 siblings, 2 replies; 4+ messages in thread
From: Amit Dang @ 2005-05-25  7:27 UTC (permalink / raw)
  To: linux-c-programming

Hi All,
    I am facing following issue
(g++ compiler  "g++ (GCC) 3.2.3 20030502 (Red Hat Linux 3.2.3-20)" is being
used for compilation.)

Following piece of code gives warning: "address of local variable `op'
returned"
#################################
#include<iostream>
using namespace std;

struct s {
    char op[24];
    int j;
};

char* fun()
{
    struct s op;
    strcpy(op.op, "898898898\0");
    return op.op;
}

int main()
{
    cout << fun() << endl;
    return 0;
}
#################################
But when the structure is change no warning is issued
Following is the new structure
#################################
struct s {
        int j;
        char op[24];
};
#################################

Obviously I know that "op.op" should not be returned.

g++ compiler  "g++ (GCC) 3.2.3 20030502 (Red Hat Linux 3.2.3-20)" is being
used for compilation.

Thanks in advance,
Amit Dang


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Issue "address of local variable returned"
       [not found] ` <2ab8d39a05052610175e3d7018@mail.gmail.com>
@ 2005-05-27  3:54   ` Amit Dang
  0 siblings, 0 replies; 4+ messages in thread
From: Amit Dang @ 2005-05-27  3:54 UTC (permalink / raw)
  To: krzaq; +Cc: linux-c-programming

Hi Karol,
    Thanks a lot for your reply. I am aware that memory for local variable
is allocated on stack and so its address should not be returned.
My problem is compiler warning. For two different structs, compiler warning
change.

If following struct is used, compiler flashes a warning
struct s {
     char op[24];
     int j;
 };

but if int is moved before char no warning is flashed by the compiler.
struct s {
         int j;
         char op[24];
 };

For details, refer text of original mail attached below.
Regards,
Amit Dang

----- Original Message ----- 
From: "krzaq" <krzakers@gmail.com>
To: "Amit Dang" <amit_dang@intersolutions.stpn.soft.net>
Sent: Thursday, May 26, 2005 10:47 PM
Subject: Re: Issue "address of local variable returned"


> On 5/25/05, Amit Dang <amit_dang@intersolutions.stpn.soft.net> wrote:
> > Hi All,
> >     I am facing following issue
> > (g++ compiler  "g++ (GCC) 3.2.3 20030502 (Red Hat Linux 3.2.3-20)" is
being
> > used for compilation.)
> >
> > Following piece of code gives warning: "address of local variable `op'
> > returned"
> > #################################
> > #include<iostream>
> > using namespace std;
> >
> > struct s {
> >     char op[24];
> >     int j;
> > };
> >
> > char* fun()
> > {
> >     struct s op;
> >     strcpy(op.op, "898898898\0");
> >     return op.op;
> ^^----------------- char* "op" is valid in the visibility context
> of struct "op", which is the body of function fun().
> Struct "op" is created on stack and gets "deleted" after returning
> from function fun().
> Try something like this:
>
> struct s* op;
> op = (struct s*) malloc(sizeof(struct s));
> strcpy(op->op, "898898898\0");
> return op->op;
>
> > }
> >
> > int main()
> > {
> >     cout << fun() << endl;
> >     return 0;
> > }
> > #################################
> > But when the structure is change no warning is issued
> > Following is the new structure
> > #################################
> > struct s {
> >         int j;
> >         char op[24];
> > };
> > #################################
> >
> > Obviously I know that "op.op" should not be returned.
> >
> > g++ compiler  "g++ (GCC) 3.2.3 20030502 (Red Hat Linux 3.2.3-20)" is
being
> > used for compilation.
> >
> > Thanks in advance,
> > Amit Dang
> >
> > -
> > To unsubscribe from this list: send the line "unsubscribe
linux-c-programming" in
> > the body of a message to majordomo@vger.kernel.org
> > More majordomo info at  http://vger.kernel.org/majordomo-info.html
> >
>
>
> -- 
> Regards
> Karol Krzak


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Issue "address of local variable returned"
  2005-05-25  7:27 Amit Dang
       [not found] ` <2ab8d39a05052610175e3d7018@mail.gmail.com>
@ 2005-05-27 16:09 ` Glynn Clements
  1 sibling, 0 replies; 4+ messages in thread
From: Glynn Clements @ 2005-05-27 16:09 UTC (permalink / raw)
  To: Amit Dang; +Cc: linux-c-programming


Amit Dang wrote:

>     I am facing following issue
> (g++ compiler  "g++ (GCC) 3.2.3 20030502 (Red Hat Linux 3.2.3-20)" is being
> used for compilation.)
> 
> Following piece of code gives warning: "address of local variable `op'
> returned"

> #include<iostream>
> using namespace std;
> 
> struct s {
>     char op[24];
>     int j;
> };
> 
> char* fun()
> {
>     struct s op;
>     strcpy(op.op, "898898898\0");
>     return op.op;
> }
> 
> int main()
> {
>     cout << fun() << endl;
>     return 0;
> }

> But when the structure is change no warning is issued
> Following is the new structure

> struct s {
>         int j;
>         char op[24];
> };

> Obviously I know that "op.op" should not be returned.
> 
> g++ compiler  "g++ (GCC) 3.2.3 20030502 (Red Hat Linux 3.2.3-20)" is being
> used for compilation.

Consider filing a bug report or enhancement request with the GCC
developers.

Ultimately, a compiler can't warn about every possible problem with a
program. Although I can't see any reason why it would be hard to catch
the second case.

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2005-05-27 16:09 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-05-25  7:17 Issue "address of local variable returned" Amit Dang
  -- strict thread matches above, loose matches on Subject: below --
2005-05-25  7:27 Amit Dang
     [not found] ` <2ab8d39a05052610175e3d7018@mail.gmail.com>
2005-05-27  3:54   ` Amit Dang
2005-05-27 16:09 ` Glynn Clements

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).