473,396 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Compilation and runtime error with vc++ Version 6.0

I narrowed down the source of the error. The stand-alone program
listed
below reproduces it. The compilation finishes but the warning is
too serious to ignore:

cl -GX prog.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for
80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

prog.cpp
prog.cpp(18) : warning C4172: returning address of local variable or
temporary
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

/out:prog.exe
prog.obj

The line it refers to is in Matrix class definition:

const_reference operator()(size_type m, size_type n) const
{return array[m*ncol + n];}

The typedefs involved are
typedef double value_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef size_t size_type;
and array is of valarray type.

The array is a member of the Matrix class, so an element of it
should not be a temporary or a local variable, it seems to me.
Is this diagnostic correct? (Please see bottom of message for the
rest of the code.)

When I ran the program, the matrix it printed was
1 4.04
1 4.04
and the correct answer, given by the g++ compiled version, is
275 -495
275 -495

If anybody still has vc++ Version 6.0, I would appreciate
confirmation or denial that the compiler does what I said
above, by compiling and running the single-file program
below. It may be an installation or library problem I am
having. Should the standard library used by vc++ be updated
separately or do the patches issued by MS cover all there is
to cover (I have applied SP5 for Visual Studio)?

Thanks,
Levent

#include <iostream>
#include <cmath>
#include <valarray>

class Matrix
{
public:
typedef double value_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef size_t size_type;
Matrix() :nrow(0), ncol(0) {}
Matrix(size_type m, size_type n) : nrow(m), ncol(n)
{array.resize(m*n, 0);}
size_type rowsize() const {return nrow;}
size_type colsize() const {return ncol;}

reference operator()(size_type m, size_type n) {return array[m*ncol
+ n];}
const_reference operator()(size_type m, size_type n) const {return
array[m*ncol + n];}
std::valarray<value_type> array;
Matrix & operator=(const Matrix & B)
{
if (&B != this)
{
nrow = B.rowsize();
ncol = B.colsize();
array.resize(nrow*ncol);
array = B.array;
}
return *this;
}

friend void print(const Matrix& A)
{
size_t m, n;
for (m = 0; m < A.nrow; ++m)
{
for (n = 0; n < A.ncol; ++n)
std::cerr << " " << A(m, n);
std::cerr << '\n';
}
}

private:
size_type nrow, ncol;
};
Matrix operator*(const Matrix & A, const Matrix & B)
{
size_t m = A.rowsize(), p = A.colsize(), n = B.colsize(), i, j, k;
Matrix C(m, n);
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
for (k = 0; k < p; ++k)
C(i, j) += A(i, k)*B(k, j);
return C;
}

int main ()
{
Matrix M(2, 2);
Matrix N(2, 2), P(2, 2);
M.array = 275;
N(0, 0) = 1;
N(0, 1) = -2;
N(1, 0) = 0;
N(1, 1) = 1.0/5.0;
P = M*N;
print(P);
return 0;
}
Jul 19 '05 #1
1 3598
On 11 Nov 2003 17:48:06 -0800, lk**@earthlink.net (Count Dracula)
wrote:
I narrowed down the source of the error. The stand-alone program
listed
below reproduces it. The compilation finishes but the warning is
too serious to ignore:

cl -GX prog.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for
80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

prog.cpp
prog.cpp(18) : warning C4172: returning address of local variable or
temporary
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

/out:prog.exe
prog.obj

The line it refers to is in Matrix class definition:

const_reference operator()(size_type m, size_type n) const
{return array[m*ncol + n];}
Change that to:

value_type operator()(size_type m, size_type n) const
{return array[m*ncol + n];}
The typedefs involved are
typedef double value_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef size_t size_type;
and array is of valarray type.

The array is a member of the Matrix class, so an element of it
should not be a temporary or a local variable, it seems to me.


However, valarray::operator[] const returns a temporary object by
value. This is considered a defect in the standard:
http://std.dkuug.dk/jtc1/sc22/wg21/d...ctive.html#389

The change above is the only reasonable portable solution until the
defect goes through.

Tom
Jul 19 '05 #2

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

Similar topics

0
by: Jason Jefferies | last post by:
I'm currently converting a number of applications written using the MFC in vc++ version 6 into a workable .NET solution. Just trying to build the apps in version 7 without worrying about...
1
by: Paljo | last post by:
Trying to compile VC++ 6 project in VC++ 7 and getting a compiling error, error C2955: '_com_ptr_t' : use of class template requires template argument list Any thoughts? Thanks
6
by: Joachim | last post by:
I made some project changes (which seems it doesn't help if I undo) which have created compilation error: " Server Error in '/PCSWebApp1' Application....
5
by: Raterus | last post by:
I'm just throwing this error out for my sanity, I've seen posts about this, but never solutions. I'm using VS.NET 2003, Framework 1.1, and I'm getting a random error about every 1 out of 10 times...
2
by: VB Programmer | last post by:
I created a ASP.NET app locally and it works great. Uploaded it to my server and I get this error when trying to view a page. Any ideas? Server Error in '/earlyalert3' Application....
2
by: Kevin R. | last post by:
I have been ignoring this problem for a few weeks now, but it's becoming a bit annoying not to mention unproductive. Here it goes: I compile my project with no errors. Then after I debug/run it,...
0
by: z f | last post by:
i compile and test my vb.net asp.net web app. once in an hour or so i get a compilation error that only when i do IISRESET goes away. nothing related to my code. i compile and test on win XP...
1
by: Arsalan Ahmad | last post by:
Hi all, I am trying to compile some source files using makefile. While compiling I am getting errors as shown below. Any idea how can I solve this problem. I believe I need to add some...
7
by: Norman Diamond | last post by:
A project depends on VC runtime from Visual Studio 2005 SP1, and DotNet Framework 2. Options are set in the setup project properties, so if these two dependencies are not already installed then...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
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,...

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.