libstd++ postfix operator hangs - gcc

Following program hangs. I know, several ways to fix it by changing the code.
// How to compile
// % g++ <filename>.cpp
#include <iostream>
#include <set>
using namespace std;
int main()
{
set<int> empty;
set<int>::iterator iter = empty.begin() ;
while (iter++ != empty.end())
{
cout << *iter << "\n";
}
return 0;
}
My questions are:
how to fix it or workaround this piece of code?
is it a bug in libstdc++ or gcc?
thank you in advance for the answers.

iter already points to the end of the set. Incrementing it further with iter++ is not allowed. The workaround is to write a loop that can deal with an empty range:
for (auto &it : empty)
for (; iter != empty.end(); ++iter)

Related

a double free error stemming from set::erase

I keep getting the runtime error double free or corruption (!prev). I've spent a while trying things out, and I think I can come up with smaller reproducible example.
This code produces a similar "double free" error. What's going on here? I thought set::erase increments the iterator.
#include <iostream>
#include <set>
#include <string>
int main() {
std::set<int> tmp = {1,2,3};
for(auto num = tmp.begin(); num != tmp.end(); ) {
if(true) {
std::cout << "removing...\n";
tmp.erase(num);
}
}
return 0;
}
I thought set::erase increments the iterator.
It does not increment the iterator. It returns the next iterator. After erasing, the iterator num is invalidated. The valid code looks like below.
for(auto num = tmp.begin(); num != tmp.end(); ) {
if(true) {
std::cout << "removing...\n";
num = tmp.erase(num);
}
}

C++ comma operator

I'm trying to run this code from C++ Primer plus
#include <iostream>
using namespace std;
int main() {
int i = 20, j= 2*i;
cout << "i = " << i << endl;
int cats = 17,240; //No, I don't want the number 17240
return 0;
}
Why I'm seeing this error expected unqualified-id before numeric constant int cats = 17,240; , I don't know, I need a short explanation. Thanks
int cats = 17,240; would be viewed by the compiler as int (cats = 17),240; due to operator precedence. And int 240; makes no sense, so a compiler diagnostic is issued.
Did you want 17240 cats? If so then drop the comma.

wrong result boost gmp float

I need to compute 5^64 with boost multiprecision library which should yield 542101086242752217003726400434970855712890625 but boost::multiprecision::pow() takes mpfloat and gives 542101086242752217003726392492611895881105408.
However If I loop and repeatedly multiply using mpint I get correct result.
Is it a bug ? or I am using boost::multiprecision::pow() in a wrong way ? or I there is an alternative of using boost::multiprecision::pow() ?
#include <iostream>
#include <string>
#include <boost/multiprecision/gmp.hpp>
typedef boost::multiprecision::mpz_int mpint;
typedef boost::multiprecision::number<boost::multiprecision::gmp_float<4> > mpfloat;
int main(){
mpfloat p = boost::multiprecision::pow(mpfloat(5), mpfloat(64));
std::cout << p.template convert_to<mpint>() << std::endl;
mpint res(1);
for(int i = 0; i < 64; ++i){
res = res * 5;
}
std::cout << res << std::endl;
}

Search Function returns wrong results

I have made a function for my program that reads from a text file, adds content to a vector and then search in that vector. The Problem is that even if the file is empty it shows that it found something, on the other side if i change return value to 0 it does not give results at all!
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;
vector<string> contacts;
//This function returns at what index the name is found
int searchContact(string contactToSearch)
{
string entry;
ifstream input;
input.open("contacts.txt");
while (input.good())
{
while (getline(input, entry))
{
contacts.push_back(entry);
}
input.close();
}
for(int i = 0; i < contacts.size(); i++)
{
if(contactToSearch == contacts[i])
{
//Found => Returning index rest of program can see index
return i;
}
}
return 1;
}
I have just refactored your code a little. Further improvements are possible, but to begin with
1) You dont need a while for input.good()
2) You were trying to return 0 and 1 which are indeed valid positions where the string could have been present in the vector
All these aside, I still think your code might not properly populated the array The reasons for this maybe :- case sensitive comparison, reading incorrect file, binary file.. etc..
Here is a refactored code that you could use
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;
void readContacts(const string &fileName inputFileName, vector<string> &contacts){
string entry;
ifstream input;
input.open(inputFileName);
if (input.good())
{
while (getline(input, entry))
contacts.push_back(entry);
input.close();
}
}
int searchContact(const string &contactToSearch, vector<string> &contacts)
{
for (int i = 0; i < contacts.size(); i++)
{
if (contactToSearch == contacts[i])
return i;
}
return -1;
}
int main(){
vector<string> contacts;
// This needs to be filled in with the contact name u want to search
string contactToSearch;
readContacts("contacts.txt", contacts);
int index = searchContact(contactToSearch, contacts)
if (index != -1)
cout << "Found Contact " << contactToSearch" at location " << index << endl;
else
cout << "Could Not find contact " << contactToSearch << endl;
}

Giving up ownership of a memory without releasing it by shared_ptr

Is there a way I can make the shared pointer point to a different memory location without releasing the memory.pointed by it currently
Please consider the code:
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <iostream>
int
main()
{
int *p = new int();
*p = 10;
int *q = new int();
*q = 20;
boost::shared_ptr<int> ps(p);
// This leads to a compiler error
ps = boost::make_shared<int>(q);
std::cout << *p << std::endl;
std::cout << *q << std::endl;
return 0;
}
You can't.
Of course you can release and reattach, while changing the deleter to a no-op
To be honest, it looks like you'd just want
ps = boost::make_shared<int>(*q);
Prints (live on Coliru):
0
20

Resources