473,493 Members | 4,347 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

It seems wrong to me

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
4 2179
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1348
by: Quentin Crain | last post by:
Hello All: I am expecting that my FieldStorage object will contain any and all key/values from the QUERY_STRING regardless of the request method. But it seems that the QUERY_STRING is ignored...
6
4836
by: Harald Kirsch | last post by:
In the W3C document about CSS2, I read at http://www.w3.org/TR/REC-CSS2/visuren.html#choose-position the following: absolute The box's position (and possibly size) is specified with the...
7
3551
by: Peter Oliphant | last post by:
Using MakeTransparent one can supposedly turn a color used in a Bitmap to transparent. But, it looks to me like all it does it set these pixels to the color BackColor of the Control it's attached...
3
1264
by: Brian Hirt | last post by:
I've having a strange issue with pg_autovacuum. I have a table with about 4 million rows in 20,000 pages. autovacuum likes to vacuum and/or analyze it every 45 minutes or so, but it probably...
27
12989
by: ted benedict | last post by:
hi everybody, i hope this is the right place to discuss this weird behaviour. i am getting dynamically generated text or xml from the server side using xmlhttprequest. if the server side data is...
0
1046
by: Edwin Knoppert | last post by:
Seems ICallbackEventHandler is meant for the 2.0 beta i guess ??? VWD forces me to public void RaiseCallbackEvent(string eventArgument) And using a public string GetCallbackResult() to return the...
7
2048
by: Leszek L. | last post by:
Hello, I am new to this group; if my question is OT then please excuse me and tell me where to go. I am using MS Visual C++ with some of its graphics libraries. Now the compiler tells me that...
3
1190
by: cr113 | last post by:
I get the following error message when I try to run any ASP.NET application, even "hello world": "Unable to start debugging on the web server." It used to work on Windows 2000 but since I've...
2
2473
by: Tiruak | last post by:
Hi there. Thanks in advance for the people reading and trying to help. I'm very begginer using flash and action script, and I tryed to do this one navigation menu. Since I dont have experience...
0
7157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7192
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7364
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5452
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3087
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1397
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
282
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.