473,511 Members | 16,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What's the scope of a variable defined in parameter list?

Is this safe?

foo(C2X(""));

C2X is a macro defined as
#define C2X(str) XStr(str).unicodeForm()

XStr is a kind of string processing class.
unicodeForm() returns pointer of its member string.

When the caller calls foo(C2X("")), a temporary variable XStr(str) is
created, and returns the pointer of its member string as foo's
parameter.
1) Inside of foo, is the pointer still valid?
2) After calling foo, is the pointer still valid?

On VC2003, 1) is valid and 2) is invalid, but I don't know if C++
standard says the same.

Jul 23 '07 #1
1 1373
Meal wrote:
Is this safe?

foo(C2X(""));
What do you mean by "safe"?
>
C2X is a macro defined as
#define C2X(str) XStr(str).unicodeForm()

XStr is a kind of string processing class.
unicodeForm() returns pointer of its member string.

When the caller calls foo(C2X("")), a temporary variable XStr(str) is
created, and returns the pointer of its member string as foo's
parameter.
1) Inside of foo, is the pointer still valid?
Yes.
2) After calling foo, is the pointer still valid?
Not for long.
On VC2003, 1) is valid and 2) is invalid, but I don't know if C++
standard says the same.
The temporary object created by 'XStr("")' has the lifetime the same as
the evaluation of the complete expression where it's created. In your
case, it's until 'foo' returns and the result of calling it is discarded.
The expression statement ends in the semicolon. After 'foo' returns
but before the next statment begins executing the temporary is destroyed.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 23 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

56
3685
by: Xah Lee | last post by:
What are OOP's Jargons and Complexities Xah Lee, 20050128 The Rise of Classes, Methods, Objects In computer languages, often a function definition looks like this: subroutine f (x1, x2, ...)...
18
2077
by: Razvan | last post by:
Hi! What is the purpose of such a function ? int function(void)
4
14867
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is...
165
6745
by: Dieter | last post by:
Hi. In the snippet of code below, I'm trying to understand why when the struct dirent ** namelist is declared with "file" scope, I don't have a problem freeing the allocated memory. But...
6
14152
by: Jody Gelowitz | last post by:
I have run into an issue with variable scope within an XSLT document that is translated in VS.NET 2.0. Under VS.NET 1.1 (XslTransform), this code works fine. However, when using VS.NET 2.0...
78
4879
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
8
4518
by: Erik de Castro Lopo | last post by:
Hi all, Consider the following code snippet: do { int r = rand () ; } while (r != 0) ; It seems the compiler I'm using (GCC) does realise that the
92
6118
by: Heinrich Pumpernickel | last post by:
what does this warning mean ? #include <stdio.h> int main() { long l = 100; printf("l is %li\n", l * 10L);
12
2240
by: Stephan Beal | last post by:
Hello, all! i'm working on a program where i'm making heavy use of (...) arguments and i have a question about them. It's all working fine and well, but i'm getting a warning from one compiler...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7355
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
7423
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
7510
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...
1
5066
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4737
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
3225
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
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
447
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.