Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

__LINE__Var+1 instead of the line number

202 views
Skip to first unread message

Abde Sassi

unread,
Mar 11, 2003, 8:50:08 PM3/11/03
to
The following C program compiled with Visual C++ .Net
produces an incorrect output compared to other compilers.
Could you please tell me how to get this program to work
with Visual C++ and print the line number and not
__LINE__Var+...

#include <stdio.h>

#define STRINGIZE_HELPER(x) #x
#define STRINGIZE(x) STRINGIZE_HELPER(x)

#define LINE_STR STRINGIZE(__LINE__)

int main(int, char**)
{
printf("%s\n", LINE_STR);
}

The preprocessor output is (skipping stdio.h stuff):

<stdio stuff skipped>....
}
#line 443 "d:\\microsoft visual studio .net\\vc7
\\include\\stdio.h"


#pragma pack(pop)
#line 447 "d:\\microsoft visual studio .net\\vc7
\\include\\stdio.h"

#line 449 "d:\\microsoft visual studio .net\\vc7
\\include\\stdio.h"
#line
26 "c:\\ccm_wa\\onscup\\phoenix\\pxpl\\code\\test_preproce
ssor\\test_strcat.cpp"


int main(int, char**)
{
printf("%s\n", "34");
}


The output of that program when executed is:

(__LINE__Var+1)


What's wrong and how to get the line number instead of
__LINE__Var+1 ?


Thanks.


Jonathan Caves [MSFT]

unread,
Mar 12, 2003, 2:53:10 PM3/12/03
to

>Content-Class: urn:content-classes:message
>From: "Abde Sassi" <abdessat...@hp.com>
>Sender: "Abde Sassi" <abdessat...@hp.com>
>Subject: __LINE__Var+1 instead of the line number

>
>The following C program compiled with Visual C++ .Net
>produces an incorrect output compared to other compilers.
>Could you please tell me how to get this program to work
>with Visual C++ and print the line number and not
>__LINE__Var+...
>

<snip>

>
>
>What's wrong and how to get the line number instead of
>__LINE__Var+1 ?
>
>
>Thanks.

The problem here is that you have enabled Edit-and-Continue (/ZI) and for
this feature to work correctly the compiler needs to be able to adjust the
values of any uses of __LINE__ inside of a function if a user adds more
code while editing during a debug session. For example:

void f(S* pS;)
{
for (int i = 0; i < 10; i++) {
pS->m_data1 += i;
}

printf("Line = %d\n", __LINE__);
}

Imagine that when you run this code the program emits:

Line = 126

Then while debugging the user realizes that the bug they are trying to
track down is due to a field not
being updated: so they change the code to:

void f(S* pS;)
{
for (int i = 0; i < 10; i++) {
pS->m_data1 += i;
pS->m_data2 += i;
}

printf("Line = %d\n", __LINE__);
}

and continue debugging -- when the debugger adds in the changes the user
has made it needs to ensure that the usage of __LINE__ has the correct
value (127 instead of 126) so under Edit and Continue __LINE__ is
implemented as an offset from the first line of function. This is why if
you preprocess a file that uses __LINE__ and you have enabled Edit and
Continue you will see __LINE__Var+1 instead of just a number.

The workaround is to turn off Edit and Continue (/ZI) while preprocessing.

--
Jonathan Caves, Visual C++ Team
This posting is provided AS IS with no warranties, and confers no rights.

0 new messages