473,609 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compiler warning when using vector of pointers

Hi all,

I am getting a warning compiling the following code using Borland C++
Builder 6, but I dont think I am doing anything wrong. When using g++ I
get no warnings at all with a g++ -Wall -ansi -pedantic.

Now I usually assume the compiler is correct but in this case I am not sure.

push 1 gives the warning the other 2 go through fine.

The warning text is:-
"C:\temp>bc c32 a.cpp
Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland a.cpp:
Warning W8030 a.cpp 24: Temporary used for parameter '__x' in call to
'_STL::vector<A *,_STL::allocat or<A *> >::push_back( A * const &)' in
function main()
Turbo Incremental Link 5.66 Copyright (c) 1997-2002 Borland"

I cant see why a temporary is being created, what am I missing

TIA
--------------

#include <vector>

class A
{
public:
virtual ~A()=0;
private:
};

A::~A()
{
}

class BA : public A
{
public:
private:
};

int main()
{
std::vector<A *> fred;

fred.push_back( new BA()); // push 1 Gives warning

A *ptr=new BA();
fred.push_back( ptr); // push 2
fred.push_back( ptr=new BA()); // push 3

return 0;
}
Jul 22 '05 #1
5 2494
Adrian wrote:
Hi all,

I am getting a warning compiling the following code using Borland C++
Builder 6, but I dont think I am doing anything wrong. When using g++ I
get no warnings at all with a g++ -Wall -ansi -pedantic.

Now I usually assume the compiler is correct but in this case I am not
sure.

push 1 gives the warning the other 2 go through fine.

The warning text is:-
"C:\temp>bc c32 a.cpp
Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland a.cpp:
Warning W8030 a.cpp 24: Temporary used for parameter '__x' in call to
'_STL::vector<A *,_STL::allocat or<A *> >::push_back( A * const &)' in
function main()
Turbo Incremental Link 5.66 Copyright (c) 1997-2002 Borland"

I cant see why a temporary is being created, what am I missing

TIA
--------------

#include <vector>

class A
{
public:
virtual ~A()=0;
private:
};

A::~A()
{
}

class BA : public A
{
public:
private:
};

int main()
{
std::vector<A *> fred;

fred.push_back( new BA()); // push 1 Gives warning
The return value from new is a temporary. Where else would the value be
stored? So push_back gets a reference to that temporary.
A *ptr=new BA();
fred.push_back( ptr); // push 2
Here, you use a variable. push_back gets a reference to ptr.
fred.push_back( ptr=new BA()); // push 3
Here, the return value from new is assigned to ptr, and push_back gets a
reference to ptr.
return 0;
}


Jul 22 '05 #2
>> int main()
{
std::vector<A *> fred;

fred.push_back( new BA()); // push 1 Gives warning


The return value from new is a temporary. Where else would the value be
stored? So push_back gets a reference to that temporary.


Are you sure, after all new is an operator returning a pointer.

What about this, does it give a warning?

fred.push_back( new A());

My guess is that the warning has something to do with the implicit
conversion that happens from BA* to A*.

But whatever, the warning is completely bogus and shouldn't be issued.
Compilers which give warnings on legitimate code are annoying because they
only encourage people to ignore compiler warning.

john
Jul 22 '05 #3
Rolf Magnus wrote:
The return value from new is a temporary. Where else would the value be
stored? So push_back gets a reference to that temporary.

Really, my copy of the C++ standard says its void *, please backup your
conclusion.

By the way
5.3.4.1 of the standard says
"If the entity is a non-array object, the new-expression returns a
pointer to the object created. If it is an array, the new-expression
returns a pointer to the initial element of the array."

If you dont know the answer dont answer my posts
TIA

Adrian
Jul 22 '05 #4
John Harrison wrote:
What about this, does it give a warning?

fred.push_back( new A()); Change A to a non abstract class, and doing a push_back on the above I
dont get a warning.
My guess is that the warning has something to do with the implicit
conversion that happens from BA* to A*.

But whatever, the warning is completely bogus and shouldn't be issued.
Compilers which give warnings on legitimate code are annoying because they
only encourage people to ignore compiler warning.

I am glad you agree with me and its the compiler, I didn't think I was
doing anything wrong.
Thanks John
Adrian
Jul 22 '05 #5
Adrian wrote in news:cm******** **@titan.btinte rnet.com in comp.lang.c++:
The return value from new is a temporary. Where else would the value be
stored? So push_back gets a reference to that temporary. Really, my copy of the C++ standard says its void *, please backup your
conclusion.


Either you don't have the real C++ Standard, or more likely you're
confusing the return from operator new () with the value of a new
expression.

By the way
5.3.4.1 of the standard says
"If the entity is a non-array object, the new-expression returns a
pointer to the object created. If it is an array, the new-expression
returns a pointer to the initial element of the array."

Indeed it return's a pointer of type "object *", *not* "void *",
its an rvalue, when bound to a "A * const &" it will be copied to
a temporary before a refrence to this temporary is passed to
std::vector<>:: push_back.
If you dont know the answer dont answer my posts


Whatever.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #6

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

Similar topics

5
11219
by: dis | last post by:
I've been going through my code and clearing away some of the compiler warnings that i'm generating and i've come across areas where i cast pointers to integer values. The Visual Studio compiler generates this warning ... "warning C4311: 'type cast' : pointer truncation from 'void *' to 'DWORD'" The warning is generated as the pointer is apparently 64 bits long (even though a quick sizeof() reports it to only be 4 bytes in length) and...
7
2669
by: Matthew Del Buono | last post by:
Don't try to solve the problem. I've found a way -- around or fixing it. I'm just curious as to whether this is Microsoft's problem in their compiler or if there's a standard saying this is to be true (not necessarily an internal compiler error, but still an error) This may just a bit OT, but I decided to post it here instead of Microsoft because my question is more directed towards standards... Of course, any other day I would have...
2
2973
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the Intel compiler it has stopped working. I think the singleton pattern static instance thing may be at the root of the problem, but I'm not sure. I had to add calls to instance() in regCreateFn, which gets the behaviour more in line with what I was...
7
3332
by: Paul Sheer | last post by:
I need to automatically search and replace all fixed size buffer strcpy's with strncpy's (or better yet, strlcpy's) as a security and stability audit. The code base is large and it is not feasable to manually perform these changes. I would like perhaps a C++ parser that can automatically detect use of a strcpy to a buffer of fixed size. For instance, struct x { char member;
1
3167
by: Hafeez | last post by:
I am having real trouble compiling this code http://www.cs.wisc.edu/~vganti/birchcode/codeHier/AttrProj.tgz The attachment shows errors when compiled using the current version of g++ in a i386-pc-solaris2.9. Seeing reference to gcc-2.7.2 in the Makefile of the code, I downloaded it and compiled in my home directory. Then changed the referenes of LIBDIR and INCLUDES to this installation .and ran with g++ for 2.7.2 then there are still...
29
2501
by: junky_fellow | last post by:
Consider the following piece of code: struct junk { int i_val; int i_val1; char c_val; }; int main(void) {
34
4854
by: Bob | last post by:
Hi, The compiler gives Warning 96 Variable 'cmdSource' is used before it has been assigned a value. A null reference exception could result at runtime. Dim cmdSource as SQlClient.SQLDataReader Try Set up the database read and do it. Catch ex as system.exception exception stuff here Finally
8
1337
by: Ramon F Herrera | last post by:
I have some code like this: RadioButtonGroup *empty_vector = new RadioButtonGroup(); ListOfRadioButton.push_back(*empty_vector); Coming from the Java camp, I keep on trying to write the above as follows: ListOfRadioButton.push_back(new RadioButtonGroup());
4
1837
by: Juha Nieminen | last post by:
Unknownmat wrote: VS2005 has a bug related to this. When you use size_t, it internally converts it to 'unsigned int'. In some situations it forgets that the type was actually size_t and only sees it's an 'unsigned int', so when you eg. assign a size_t value to such a "unsigned int which was a size_t but VS2005 has forgotten about it", it will give you a warning about possible data loss (because it only sees that a size_t is being...
0
8129
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8074
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8535
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8220
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8404
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5509
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4017
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2530
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 we have to send another system
1
1667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.