Skip to contentSkip to conversation

Issue "address of local variable returned"

DISCUSSION Amit Dang 2 participants 3 messages May 25, 2005 – May 27, 2005 1 message not in the archive

lore
Share & tools
lore

Amit Dang

author
#0 lore raw

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

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#2 lore raw

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" <[email protected]>
To: "Amit Dang" <[email protected]>
Sent: Thursday, May 26, 2005 10:47 PM
Subject: Re: Issue "address of local variable returned"

Quoted 4 lines · Amit Dang
Quoted 3 lines

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

being

Quoted 24 lines
Quoted 13 lines

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;
};
Quoted 5 lines

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:

Quoted 25 lines

struct s* op;
op = (struct s*) malloc(sizeof(struct s));
strcpy(op->op, "898898898\0");
return op->op;

Quoted 20 lines

}

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

Quoted 7 lines
Quoted 7 lines

used for compilation.

Thanks in advance,
Amit Dang

-
To unsubscribe from this list: send the line "unsubscribe

linux-c-programming" in

Quoted 8 lines
Quoted 3 lines

the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 2 lines
Regards
Karol Krzak
#3 lore raw
In reply to Amit Dang
Quoted 36 lines · Amit Dang

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];
};
Quoted 4 lines

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.

Signature · 1 line
Glynn Clements <[email protected]>