Hi All,
I've this weird question about pointers. I would like to know how to
return the address of a local variable, safely!! Isn't that a
unrecommended procedure? Doesn't it have possibilities that the
variable could be deallocated from memory and the dereferencing of the
returned pointer somewhere else could crash the program?
If it's not safer to do such an operation, how do functions like
strtok for example work???
--
Spitfire 14 16116
Spitfire a écrit :
Hi All,
I've this weird question about pointers. I would like to know how to
return the address of a local variable, safely!! Isn't that a
unrecommended procedure? Doesn't it have possibilities that the
variable could be deallocated from memory and the dereferencing of the
returned pointer somewhere else could crash the program?
If it's not safer to do such an operation, how do functions like
strtok for example work???
--
Spitfire
You can't return the address of a local variable since when the
function where that variable leaves (exits) the local variable is
no longer valid and points to unreachable storage.
strtok use a static variable, not a local variable with automatic
storage class.
Spitfire wrote:
I've this weird question about pointers. I would like to know how to
return the address of a local variable,
void* get_local_address ()
{
int i;
return &i;
}
safely!!
it's perfectly (well almost) safe as long as you don't use it.
Isn't that a unrecommended procedure?
I just don't understand why you want to do this
Doesn't it have possibilities that the
variable could be deallocated from memory and the dereferencing of the
returned pointer somewhere else could crash the program?
so don't dereference it?
If it's not safer to do such an operation, how do functions like
strtok for example work???
now I'm confused. Why would stritok return the address of a local
variable? strtok() modifies the string you pass to it.
--
Nick Keighley
Spitfire said:
Hi All,
I've this weird question about pointers. I would like to know how to
return the address of a local variable, safely!!
int *foo(void)
{
static int i;
return &i;
}
Isn't that a unrecommended procedure?
It can be. That depends on what you're doing and how you do it.
Doesn't it have possibilities that the
variable could be deallocated from memory and the dereferencing of the
returned pointer somewhere else could crash the program?
Not if you're careful (see above).
If it's not safer to do such an operation, how do functions like
strtok for example work???
Carefully. For one thing, they don't return the addresses of local
variables.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www.
On Nov 23, 3:06 am, jacob navia <j...@jacob.remcomp.frwrote:
>You can't return the address of a local variable since when the
function where that variable leaves (exits) the local variable is
no longer valid and points to unreachable storage.
strtok use a static variable, not a local variable with automatic
storage class.
hmmm... that pretty much explains everything...
"Nick Keighley" <ni******************@hotmail.comwrites:
Spitfire wrote:
> I've this weird question about pointers. I would like to know how to return the address of a local variable,
void* get_local_address ()
{
int i;
return &i;
}
>safely!!
it's perfectly (well almost) safe as long as you don't use it.
And completely useless.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
On Nov 23, 3:11 am, "Nick Keighley" <nick_keighley_nos...@hotmail.com>
wrote:
safely!!it's perfectly (well almost) safe as long as you don't use it.
Can't understand your point here!?
Why would stritok return the address of a local
variable? strtok() modifies the string you pass to it.
I can understand that it modifies the string that I pass to it.
Nevertheless, if you observe the function "returns" the token after the
current call. Understood what I'm referring to? I somehow got that I'm
overlooking something trivial... however, couldn't catch what that was!
The reply from Jacob highlights what I'd overlooked.
Hi,
On Nov 23, 3:56 am, Richard Heathfield <r...@see.sig.invalidwrote:
return the address of a local variable, safely!! int *foo(void)
{
static int i;
return &i;
}
that's something I overlooked :)
>Carefully. For one thing, they don't return the addresses of local
variables.
the point is just to extend the lifetime, to prevent invalid references
right?
On Nov 23, 4:05 am, Keith Thompson <k...@mib.orgwrote:
it's perfectly (well almost) safe as long as you don't use it.
And completely useless.
the reason being?
--
Spitfire
Spitfire said:
Hi,
On Nov 23, 3:56 am, Richard Heathfield <r...@see.sig.invalidwrote:
>return the address of a local variable, safely!! int *foo(void) { static int i; return &i;
}
that's something I overlooked :)
>>Carefully. For one thing, they don't return the addresses of local variables.
the point is just to extend the lifetime, to prevent invalid references
right?
Yes, this is about lifetime, not scope. Basically, if the object *exists*,
it's okay to know where it lives. But if it doesn't exist, knowing where it
lives is more information than you can use.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www.
Spitfire said:
>
On Nov 23, 4:05 am, Keith Thompson <k...@mib.orgwrote:
it's perfectly (well almost) safe as long as you don't use it.
And completely useless.
the reason being?
If the object doesn't exist, where's the percentage in knowing where it used
to live?
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www.
On Nov 23, 4:20 am, Richard Heathfield <r...@see.sig.invalidwrote:
Yes, this is about lifetime, not scope. Basically, if the object *exists*,
it's okay to know where it lives. But if it doesn't exist, knowing where it
lives is more information than you can use.
thanks for the info
--
Spitfire
Spitfire wrote:
On Nov 23, 3:06 am, jacob navia <j...@jacob.remcomp.frwrote:
You can't return the address of a local variable since when the
function where that variable leaves (exits) the local variable is
no longer valid and points to unreachable storage.
strtok use a static variable, not a local variable with automatic
storage class.
hmmm... that pretty much explains everything...
no, not really. strtok() doesn't return a pointer to the static
variable
but a pointer to the argument string. The use of a static variable
is irrelevent.
char *tail (char* s)
{
return &s[1];
}
--
Nick Keighley
In article <x9******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
....
>Yes, this is about lifetime, not scope. Basically, if the object *exists*, it's okay to know where it lives. But if it doesn't exist, knowing where it lives is more information than you can use.
ITYM, "lived".
Groovy hepcat Spitfire was jivin' on 23 Nov 2006 02:07:22 -0800 in
comp.lang.c.
Re: returning address of a local variable's a cool scene! Dig it!
>On Nov 23, 3:11 am, "Nick Keighley" <nick_keighley_nos...@hotmail.com> wrote:
>safely!!it's perfectly (well almost) safe as long as you don't use it.
Can't understand your point here!?
>Why would stritok return the address of a local variable? strtok() modifies the string you pass to it.
I can understand that it modifies the string that I pass to it. Nevertheless, if you observe the function "returns" the token after the current call.
It returns a pointer to the first/next token in the string. However,
the string, and therefore the token, is in the buffer whose address is
passed to strtok().
Understood what I'm referring to?
Not really. Maybe you're refering to the address (of the next token)
stored in a static variable used by strtok()? In that case it's
strtok()'s internal pointer itself that's static, not the thing it
points at.
--
Dig the even newer still, yet more improved, sig! http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Olumide |
last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is
an absolutely fantastic book; however on page 175 (topic: operator
overlaoding), there the following code snippet:
inline...
|
by: octavio |
last post by:
Hello members of the comp.lang.c newsgroup. Please I need you help on
the following one.
Compiling the simple code I'm getting this error message.
Why ? Please what's the correct type of the fb...
|
by: Sourav |
last post by:
Suppose I have a code like this,
#include <stdio.h>
int *p;
void foo(int);
int main(void){
foo(3);
printf("%p %d\n",p,*p);
|
by: FireHead |
last post by:
Hello All,
Its hard to explain but here it goes:
char &free_malloc(char* object_copy){
Here ------------------------------------------------------------------
object_copy is parametre were...
|
by: rsa_net_newbie |
last post by:
Hi there,
I have a Managed C++ object (in a DLL) which has a method that is
defined like ...
Generic::List<String^>^ buildList(String^ inParm)
Now, when I compile it, I get "warning C4172:...
|
by: ali |
last post by:
Hi,
When I pass a pointer as an argument of a method, is it safe if I
change the data pointed by the argument and return it upon
completion ?
For example:
Object* someFunction(Object* ob)...
|
by: curious2007 |
last post by:
Hello Everyone,
I had this code working on DEV C++. But now, it does not work in Visual C++ Exress. Here is the code:
template <class V, class AI, class I, class S>
const V& AssocArray<V,...
|
by: hari |
last post by:
Hi all,
Is it legal to return a local variable from function.
Regards
Hari
|
by: pauldepstein |
last post by:
#include <iostream>
using namespace std;
double & GetWeeklyHours()
{
double h = 46.50;
double &hours = h;
return hours;
}...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |