By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,620 Members | 1,302 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,620 IT Pros & Developers. It's quick & easy.

It seems wrong to me

P: n/a
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;
}
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Stub" <st**@asof.com> wrote in message
news:Xl***********************@bgtnsc04-news.ops.worldnet.att.net
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".
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 19 '05 #2

P: n/a
Stub wrote:
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

Jul 19 '05 #3

P: n/a
Stub wrote:
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'.
}

--
Dirk

(PGP keyID: 0x448BC5DD - http://www.gnupg.org - http://www.pgp.com)

Jul 19 '05 #4

P: n/a
Stub wrote:
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.

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.