It seems wrong to me

S

Stub

I read the code in a book and it seems not quite right to me. The possible
mistake seems in the dummy::getData(). It returns a local pointer, void*
data, back to its caller. Am I right about this?

class dummy
{
public:
void* getData();
void *first;
};


void* dummy::getData(){
void *data;
data = first;
return data;
}
 
J

John Carson

Stub said:
I read the code in a book and it seems not quite right to me. The
possible mistake seems in the dummy::getData(). It returns a local
pointer, void* data, back to its caller. Am I right about this?

class dummy
{
public:
void* getData();
void *first;
};


void* dummy::getData(){
void *data;
data = first;
return data;
}

There is no error. It would be simpler for getData to just return first, but
since data has the same value as first, returning data has the same effect.

Whether or not the pointer is a local variable is irrelevant, just as it is
irrelevant when a function returns an int. What matters is whether the
memory pointed to is "local memory" or "global memory".
 
D

Dirk Feytons

Stub said:
I read the code in a book and it seems not quite right to me. The possible
mistake seems in the dummy::getData(). It returns a local pointer, void*
data, back to its caller. Am I right about this?
No.

class dummy
{
public:
void* getData();
void *first;
};


void* dummy::getData(){
void *data;

This declares the variable named 'data' of type 'void*'. It's a variable
that can hold a void pointer. At this point, the pointer points to
god-knows-what. It is said to be uninitialized.
data = first;

Here the pointer gets a value. More specific, the pointer now points to
the same thing as 'first' points to.
return data;

So what is actually returned, is 'first'.
In fact, the implementation of getData() can be simplified to a simple
'return first'.
 
B

Bruno Desthuilliers

Stub said:
I read the code in a book and it seems not quite right to me. The possible
mistake seems in the dummy::getData(). It returns a local pointer, void*
data, back to its caller. Am I right about this?
class dummy
{
public:
void* getData();
void *first;
};


void* dummy::getData(){
void *data;
data = first;
return data;
}

#include <stdcaution> // I'm certainly not an expert in C++

You're right about the fact that it returns a local variable, but this
is not a problem by itself (well, this may be a problem for the caller
if dummy::first is not initialised before).

Returning a local variable is ok, the caller gets a copy of it. What
would be wrong would be to return *the address of* a local variable.

What seems more problematic to me is that
1/ the local variable 'data' in getData() is useless. This code is the
same as :

void* dummy::getData(){
void *data = first; // useless
return first;
}

which in turn would be better written :
void* dummy::getData(){
return first;
}

2/ since it's a pointer that is returned, and not a copy of the value
pointed, any change made to the value pointed will affect dummy::first

3/ this function by itself is actually of useless, since dummy::first is
public...

4/ ... - which is a Bad Thing(tm).

5/ void pointers are probably the worst way to write generic code in C++
anyway.

Well... I don't know which book this come from, but I suspect the paper
it's made of would be more useful elsewhere...

(gurus please correct me if I said some stupidities)

Bruno
 
R

Rolf Magnus

Stub said:
I read the code in a book and it seems not quite right to me. The
possible mistake seems in the dummy::getData(). It returns a local
pointer, void* data, back to its caller. Am I right about this?

You're right in that it returns a local pointer, but that's not a
problem. It seems you're confusing it with a pointer to a local
variable, which would be what you call "not quite right". In your
example, you return the pointer "by value", which means that the
pointer itself is copied and the caller of the function receives a copy
that points to the same address as 'data'.
The following example would be wrong:

class dummy
{
public:
int* getData();
int x;
};


int* dummy::getData(){
int data;
data = x;
return &data;
}

Now you return a pointer that contains the address of a local variable.
Since that variable ceases to exist before the caller can access it,
the returned pointer is invalid and must not be dereferenced.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top