About stl list::splice - g++

I am facing a problem with splicing the list with itself. Note that I have gone through splice() on std::list and iterator invalidation
There the question was about two different lists. But my question is about the same list.
mylist.splice(mylist.end(), mylist, ++mylist.begin());
It seems that gcc 3.x is invalidating the moved iterator. So I suppose it is deallocating and allocating the node again. This does not make sense for the same list. SGI does tell that this version of splice should not invalidate any iterators. Is this a bug with gcc 3.x, if it is there any workaround?
In the mean time I was going through the stl_list.h file. But stuck with the transfer() function, I could not find a definition for these.
struct _List_node_base
{
_List_node_base* _M_next; ///< Self-explanatory
_List_node_base* _M_prev; ///< Self-explanatory
static void
swap(_List_node_base& __x, _List_node_base& __y);
void
transfer(_List_node_base * const __first,
_List_node_base * const __last);
void
reverse();
void
hook(_List_node_base * const __position);
void
unhook();
};
Do you have any idea where can I look for these function definitions?

This functions are in the libstdc++ sources, not the headers. In 3.4 it's in libstdc++-v3/src/list.cc
http://gcc.gnu.org/viewcvs/branches/gcc-3_4-branch/libstdc%2B%2B-v3/src/list.cc?view=markup
Have you tried compiling with -D_GLIBCXX_DEBUG ? That will enable the Debug Mode and tell you if you're using invalid iterators or anything else that causes the problem.
I just tried this simple test with GCC 3.4, with and without debug mode, and it worked fine:
#include <list>
#include <iostream>
#include <string>
int main()
{
std::list<std::string> l;
l.push_back("1");
l.push_back("2");
l.push_back("3");
l.push_back("4");
l.push_back("5");
l.push_back("6");
l.splice(l.end(), l, ++l.begin());
for (std::list<std::string>::iterator i = l.begin(), e = l.end(); i != e; ++i)
std::cout << *i << ' ';
std::cout << std::endl;
}
Modifying it further and debugging it I see that no element is destroyed and reallocated when doing the splice, so I suspect the bug is in your program. It's hard to know, as you haven't actually said what the problem is.

Related

Boost asio crashes

I have a program using cpprestsdk for http querying and websocketpp for subscribing a data stream. The program will crash immediately(it says Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)). But if I comment either of the http querying or subcribing data stream, the program won't crash.
#include <websocketpp/config/asio_client.hpp>
#include <websocketpp/client.hpp>
#include "json.hpp"
#include <iostream>
#include <ctime>
#include <iostream>
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <vector>
#include <string>
using std::string;
using namespace web;
using std::cout, std::endl;
using std::vector;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
typedef websocketpp::client<websocketpp::config::asio_tls_client> client;
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
void on_stream_data(websocketpp::connection_hdl hdl, message_ptr msg) {
}
class OrderBook {
public:
void initialize() {
web::http::client::http_client_config cfg;
std::string uri = string("https://fapi.binance.com/fapi/v1/depth?symbol=btcusdt&limit=1000");
web::http::client::http_client client(U(uri), cfg);
web::http::http_request request(web::http::methods::GET);
request.headers().add("Content-Type", "application/x-www-form-urlencoded");
web::http::http_response response = client.request(request).get();
}
int start_stream() {
client c;
std::string uri = string("wss://fstream.binance.com/ws/btcusdt#depth#100ms");
try {
c.set_access_channels(websocketpp::log::alevel::all);
c.clear_access_channels(websocketpp::log::alevel::frame_payload);
c.init_asio();
c.set_message_handler(bind(on_stream_data, ::_1, ::_2));
websocketpp::lib::error_code ec;
client::connection_ptr con = c.get_connection(uri, ec);
if (ec) {
std::cout << "could not create connection because: " << ec.message() << std::endl;
return 0;
}
c.connect(con);
c.run();
} catch (websocketpp::exception const &e) {
std::cout << e.what() << std::endl;
}
}
};
int main(int argc, char *argv[]) {
OrderBook ob;
ob.initialize(); // comment either of these two lines, the program won't crash, otherwise the program will crash once start
std::this_thread::sleep_for(std::chrono::milliseconds(10000000));
ob.start_stream(); // comment either of these two lines, the program won't crash, otherwise the program will crash once start
}
When I run this program in Clion debug mode, Clion show that the crash comes from function in /opt/homebrew/Cellar/boost/1.76.0/include/boost/asio/ssl/detail/impl/engine.ipp
int engine::do_connect(void*, std::size_t)
{
return ::SSL_connect(ssl_);
}
It says Exception: EXC_BAD_ACCESS (code=1, address=0xf000000000)
What's wrong with it? is it because I run two pieces of code using boost::asio, and something shouldn't be initialized twice?
I can compile this and run it fine.
My best bet is that you might be mixing versions, particularly boost versions. A common mode of failure is caused when ODR violations lead to Undefined Behaviour.
Note that these header-only libraries depend on a number of boost libraries that are not header-only (e.g. Boost System, Thread and/or Chrono). You need to compile against the same version as the libraries you link.
If you use distribution packaged versions of any library (cpprestsdk, websocketpp or whatever json library that is you're using) then you'd be safest also using the distribution packaged version of Boost.
I'd personally simplify the situation by just using Boost (Beast for HTTP/websocket, Json for, you guessed it).
Running it all on a test Ubuntu 18.04 the OS Boost 1.65 version, the start_stream sequence triggers this informative error:
[2022-05-22 13:42:11] [fatal] Required tls_init handler not present.
could not create connection because: Connection creation attempt failed
While being UBSAN/ASAN clean. Perhaps that error helps you, once you figure out the configuration problems that made your program crash.

Subtracting multiple polyhedra continuously from a polyhedron

I want to get the result of subtracting multiple (probably hundreds) polyhedra from a polyhedron. I found that the CGAL library's "3D Boolean Operations on Nef Polyhedra" package supports Boolean Operations between Polyhedra. I wanted to use this package to solve my problem, but I ran into a lot of trouble. While I knew the CGAL library was a powerful one, I was completely new to it and had no idea how to use it most effectively to solve my problem.
My goal is to use the CGAL library to implement one polyhedron minus multiple polyhedrons, and I'll go into more detail about this problem as well as my approach and the errors that the program produced. I'd appreciate it if you could tell me why the program is producing these errors and how I can efficiently use the CGAL library to implement one polyhedron minus multiple polyhedra.
The problem I want to solve with CGAL:
I used MATLAB to get some polyhedra: A,B1,B2,...,Bi,..., Bn(n probably several hundred). I want to get the result of A-B1-B2-...-Bn.
My approach to solve this problem with CGAL library:
In fact, only A is a 2-manifold, and Bi were both 3-dimensional surface with boundaries. In order to use CGAL library's "3D Boolean Operations on Nef Polyhedron" package, I turned these surfaces into closed polyhedrons. I saved A as a ".off" format file named "blank.off". Bi was converted to ".off" format, and all Bi were saved in one file named "sv.off". Each Bi is separated by a newline character. I use CGAL::OFF_to_nef_3() to read file "blank.off" into a Nef_polyhedron object nef1. Then I wrote a looping statement, in which I use CGAL::OFF_to_nef_3() to read file "sv.off" into a Nef_polyhedron object nef2 and do nef1-=nef2.
code is as follows:
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/Nef_polyhedron_3.h>
#include <CGAL/IO/Polyhedron_iostream.h>
#include <CGAL/draw_nef_3.h>
#include <CGAL/OFF_to_nef_3.h>
typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
typedef CGAL::Polyhedron_3<Kernel> Polyhedron;
typedef CGAL::Nef_polyhedron_3<Kernel> Nef_polyhedron;
#include<fstream>
#include<ctime>
int main() {
Polyhedron p1, p2, res;
int n = 0;
std::ifstream fin1("blank.off");
std::ifstream fin2("sv.off");
std::cout << "how many polyhedra in sv.off\n";
std::cin >> n;
//load nef2 and do bool operations
Nef_polyhedron nef1(p1);
Nef_polyhedron nef2(p2);
CGAL::OFF_to_nef_3(fin1, nef1);
fin1.close();
for (int i = 0; i < n;i++) {
nef2.clear();
CGAL::OFF_to_nef_3(fin2, nef2);
fin2.get();
nef1 -= nef2;
std::cout << "A-B" << i+1 << " have been calculated" << std::endl;
}
//convert nef2 to res.off
nef1.convert_to_polyhedron(res);
std::ofstream fout("res.off");
fout << res;
//draw
//CGAL::draw(nef1);
fin2.close();
return 0;
}
You can download blank.off and sv.off files from Github. Download link: blank.off and sv.off
Problems that occur while the program is running
On the fifth iteration(i==4) of the program, sometimes the IDE will stop running or crash without any exception thrown. Why does this problem arise? Is it because the memory usage is too high?
After 12 cycles(i==11), I want to convert nef1 to .off file to save current result. But it fails when the program reaches the sentence "nef1.convert_to_polyhedron(res)"since nef1.is_simple() returns false. I looked in the manual and I realized that this means that nef1 is no longer a 2-manifold. But what causes nef1 to no longer be a 2-manifold? Is there a function in the CGAL that can modify nef1 to make it a 2-manifold again?
This is not an error, but the computing speed is too slow. Is there another way to do it faster?
Other problems:
In fact, what I originally got using MATLAB were sets of points for the polyhedron A without boundary and the surface B with boundaries. In order to perform boolean operations on A and B. I wrote some programs with MATLAB to triangulate A and B, and convert B to closed polyhedron. I know the quality of triangulation mesh produced by the program is not high, which maybe the main reason why so many errors occur. Whether these can be done entirely with the CGAL library? How to do it?
The most important question is whether the CGAL library is suitable for performing hundreds of continuous Boolean operations on three-dimensional geometry?
Thank you very much for reading this question, I would appreciate it if you could help me.
After playing a little with your inputs, I can say that the most plausible cause for all of your problems is that your insputs are not very clean. If you manage to remove degenerated faces from your OFFs in sv.off, everything should run fine. Now, there is a way to make everything much faster : corefine_and_compute_difference.
If I adapt your code to use it, it looks like :
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/IO/Polyhedron_iostream.h>
#include <CGAL/Polygon_mesh_processing/corefinement.h>
#include <CGAL/draw_surface_mesh.h>
#include <CGAL/Polygon_mesh_processing/IO/polygon_mesh_io.h>
typedef CGAL::Exact_predicates_inexact_constructions_kernel Epic;
typedef CGAL::Surface_mesh<Epic::Point_3> Surface_mesh;
int main() {
std::cout << "how many polyhedra in sv.off\n";
std::cin >> n;
std::ifstream fin1("data/blank.off");
std::ifstream fin2("data/sv.off");
//load nef2 and do bool operations
Surface_mesh s1, s2, out;
CGAL::IO::read_OFF(fin1, s1);
fin1.close();
for (int i = 0; i < n;i++) {
s2.clear();
CGAL::IO::read_OFF(fin2, s2);
fin2.get();
std::ofstream fout("s2.off");
fout << s2;
fout.close();
CGAL::Polygon_mesh_processing::corefine_and_compute_difference(s1, s2, s1);
std::cout << "A-B" << i+1 << " have been calculated" << std::endl;
}
//convert nef2 to res.off
std::ofstream fout("res.off");
fout << s1;
fout.close();
//draw
CGAL::draw(s1);
fin2.close();
return 0;
}
But you have to be sure your sv meshes are "clean", they must not have degenerated faces, for example.
From the shape of the first sv mesh, I'd say you can use CGAL's Advancing Front Surface Reconstruction (example here) to get your meshes from your point sets, and they should be clean enough. (I tried with the first one and it worked well).

Using custom DLL with Lua 5.1

I'm trying to use a custom DLL from within Lua. I have a simple DLL, for example, like
extern "C"
{
static int function_1(lua_State* L)
{
std::cout << "[DLL]this is a custom function" << std::endl;
lua_pushnumber(L, 10);
return 1;
}
__declspec(dllexport) int __cdecl luaopen_myDLL(lua_State* L)
{
L = luaL_newstate();
luaL_openlibs(L);
std::cout << "[DLL] being initialized!" << std::endl;
lua_register(L, "fun1", function_1);
luaL_dofile(L, "./run.lua");
return 1;
}
}
written in VS and built as dll.
After running within Lua
package.loadlib("./myDLL.dll", "luaopen_myDLL")()
or
require("myDLL")
the DLL is loaded and runs like expected and also runs the specified run.lua that execute function_1 just fine.
The run.lua has nothing special in it, just something like
f = function_1()
print("[Lua] Function_1 says", f, "\n");
My current issues now are:
I cannot run function_1() from the initial Lua script calling the DLL. Trying to do that I get
attempt to call global 'function_1' (a nil value)
i must use L = luaL_newstate(); inside my C code. For some reason, it doesn't work with the passed lua_State*, which I think is the reason why I cannot call the registered functions from the LUA script loading my DLL. Before running luaL_newstate() my lua_State has a valid address which doesn't change after the newstate.
I could theoretically run any Lua script from within my C library executing the registered functions, but this seems more like a dirty workaround to me.
My question now is if I'm missing something essential?
p.s.: I'm using Lua 5.1
The code below should work. It may not work because of the following reasons:
your binary you use to run initial Lua script (that has require("myDLL") in it) has a different Lua version and/or does not use shared dll.
your Lua headers you use in your C++ code have different Lua version from original lua.exe
you link your project against different Lua version
you compile Lua again with your solution (you must use only headers and already provided .lib file with Lua distribution, if you want to use lua.exe)
To make your code available in Lua you must use Lua headers for proper Lua version and link against proper .lib file and use lua.exe that uses shared library (lua.dll, I guess).
static int function_1(lua_State* L)
{
std::cout << "[DLL]this is a custom function" << std::endl;
lua_pushnumber(L, 10);
return 1;
}
extern "C" int __declspec(dllexport) luaopen_quik(lua_State *L) {
std::cout << "[DLL] being initialized!" << std::endl;
lua_register(L, "fun1", function_1);
luaL_dofile(L, "./run.lua");
return 0;
}
P. S. please provide your solution files so I can help further because it is not an issue with the code. -- it's the linkage issue.

What does ":=" refer to?

I'm looking at the algorithm for breadth-first sorting of a binary search tree, and there is a symbol used that I cannot understand. Funny enough, Google returns zero results.
// levelorder()
// q = empty queue
// q.enqueue(root)
// while not q.empty do
// node := q.dequeue() //Referring to this
// visit(node)
// if node.left != null then
// q.enqueue(node.left)
// if node.right != null then
// q.enqueue(node.right)
What is the operation being used here? I'm quite confused by this line.
The code you posted is pseudo code, and is not intended to be valid C++.
In C++, the assignment operator is =.
In other languages such as Ada, BCPL, Cecil, Dylan, E, Eiffel, Maple, Mathematica, Modula-3, Pascal, Pliant, Sather, Simula, Smalltalk, SML, the assignment operator is :=.
GNU make also uses := for a way of assigning.
Since the code you posted is a comment, it is not intended to be valid C++.
Here is a closer representation of the code you posted in valid C++:
#include <iostream>
#include <string>
#include <queue>
//A node might look like this:
struct Node{
Node* left;
Node* right;
};
//somewhere you have a node and a root
Node* node = new Node;
Node* root = new Node;
//a visit function is called in the pseudo code you posted
void visit(Node* node){
// ... code ...
return;
}
//here is what valid C++ that is similar to the pseudo code:
void levelorder(){
//empty queue
std::queue<Node*> q;
//add the root to the queue
q.push(root);
do {
visit(node);
if (node->left != nullptr){
q.push(node->left);
}
if (node->left != nullptr){
q.push(node->right);
}
}while(!q.empty());
return;
}
//I just added this main function so the whole code snippet compiles successfully
int main(){}
:= is assignment in pseudocode.
Or ADA. But that's kinda like psuedocode anyway.
It's inside a comment (per // prefix)... it's not compilable code and doesn't mean anything in C++.
It's not a C++ operator. It is used in some languages such as Pascal to mean the same thing as what an assignment = does.
See: Assignment operator (Computer science)

how to iterate over PCB's to show information in a Linux Kernel Module?

I want to write a little Linux Kernel Module that can show me the PID of all running processes.
I have the following code:
/*
* procInfo.c My Kernel Module for process info
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
/*
* The init function, called when the module is loaded.
* Returns zero if successfully loaded, nonzero otherwise.
*/
static int mod_init(void)
{
printk(KERN_ALERT "ProcInfo sucessfully loaded.\n");
return 0;
}
/*
* The exit function, called when the module is removed.
*/
static void mod_exit(void)
{
printk(KERN_ALERT "ProcInfo sucessfully unloaded.\n");
}
void getProcInfo()
{
printk(KERN_INFO "The process is \"%s\" (pid %i)\n",
current->comm, current->pid);
}
module_init(mod_init);
module_exit(mod_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Rodrigo");
As you can see, i know i have to use the *struct task_struct* structure to get the PID and the Process name, but i am using current, and i know the existance of some double linked circular list that contains all PCB's, so the main question is:
what do i need to add to iterate over this linked lisk with p-next_task and p-prev_task so getProcInfo works?
Thanks!
The following macros from include/linux/sched.h may be useful:
#define next_task(p) \
list_entry_rcu((p)->tasks.next, struct task_struct, tasks)
#define for_each_process(p) \
for (p = &init_task ; (p = next_task(p)) != &init_task ; )
You probably need to hold the tasklist_lock before calling these macros; several examples of how to lock, iterate, and unlock, are in mm/oom_kill.c.
Actually, for newer kernels (2.6.18 and newer) the proper way to list tasks is by holding an rcu lock, because task list is now an RCU list. Also tasklist_lock is no more exported symbol - it means that when you are compiling a loadable kernel module, this symbol will not be visible for you.
example code to use
struct task_struct *task;
rcu_read_lock();
for_each_process(task) {
task_lock(task);
/* do something with your task :) */
task_unlock(task);
}
rcu_read_unlock();
Also documentation about RCU in linux kernel source directory can be helpful and you will find it in Documentation/RCU