views:

150

answers:

3

Why are we allowed to run this code:

int* FunctionB(int x)
{
    int temp =30;

    //more code

    return &temp;
}

It seems to me that I am not returning what I said I would. Why is it that a memory address can be returned if I declared the return type to be a pointer. Isn't a pointer something that points to a memory address, not actually a memory address?


class Image : public BMP
{
    public:
    void invertcolors();
    void flipleft();
    void adjustbrightness(int r,int g,int b);

    private:
};

Upon compilation of the previous code I get this error:

image.h:3: error: expected class-name before ‘{’ token

but I thought I was using the proper syntax to declare a sub class. Is there something wrong with what I have written?

+4  A: 

In the first case, a pointer is a memory address, which points to some data, so your syntax is correct. In this case, however, you're returning the address of a local variable, so it's undefined what it'll be pointing to after the function call ends.

In the second case, the compiler is complaining because it doesn't know what BMP is. You may have forgotten to #include the header file for the BMP class.

Jesse Beder
I wish it would be certain that it pointed to garbage, then it probably would be noticed problem is that it might look like the code actually behaves nicely until the contents of that adress is changed. Then suddenly things starts going wrong. Good there is something known as warnings.
daramarak
@daramark, yeah, I like using garbage to mean undefined, but I edited to clarify a bit.
Jesse Beder
+6  A: 

As far as your first question is concerned

Why are we allowed to run this code:

Nothing can stop you from returning the address of a local variable but that's dangerous(mark my words). Infact return address or reference of a local variable invokes Undefined Behaviour (that means anything can happen).

Also have a look at this.

Why is it that a memory address can be returned if I declared the return type to be a pointer?

That means you have to study the basics on pointers. In computer science, a pointer is a programming language data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address.

..but I thought I was using the proper syntax to declare a sub class. Is there something wrong with what I have written?

Have you defined BMP or have you included the header which contains its defnition?

Prasoon Saurav
+3  A: 

In C and C++ languages the term address and the term pointer are [almost] precise synonyms. In practical, less formal language the term address is most ofent used to refer to rvalue pointer values, while the term pointer can be seen used for both rvalue and lvalue pointer values. In your first example, you are returning an rvalue, so the terms "address" and "pointer" can be used interchangeably. You are returning a pointer to an int object. You are returning an address of an int object. Both mean the same thing. Of course, returning address of a local object makes no sense, aside form a deliberate attempt to cause undefined behavior.

As for the seconf question, the compiler simply doesn't know what BMP is. You didn't declare it. Moreover, a class must be defined before you can use it as a base in another class declaration.

AndreyT