logo
Tags down

shadow

access to iterator in lambda function of an algorithm cause me segmentation fault


By : Susana Diaz
Date : July 31 2020, 07:00 PM
wish of those help Honestly, your approach looks rather complicated to me. Most of its complexity is due to the fact that you have rows in your code but they are present only implicitly. Making stuff explicit in code not only helps to increase readability but also makes code easier to write.
Lets say you use std::array for rows, then your code could be as leightweight as this:
code :
#include <vector>
#include <array>
#include <algorithm>
#include <iostream>

int main() {
    using row_t = std::array<std::string,3>;
    std::vector<row_t> vec = { {"1","8","1"},{"2","3","2"},{"3","5","5"},{"2","5","6"}};

    std::sort(vec.begin(),vec.end(),[](const row_t& a, const row_t& b) { return a[2] < b[2]; });

    for (const auto& row : vec) {
        for (const auto& e : row) std::cout << e << " ";
        std::cout << '\n';
    }
}
1 8 1 
2 3 2 
3 5 5 
2 5 6 
template <int stride>
std::string& access_at(std::vector<std::string>& vec,size_t row,size_t col) { 
    return vec[ row * stride + col ]; 
}
template <int stride>
const std::string& access_at(const std::vector<std::string>& vec,size_t row,size_t col) { 
    return vec[ row * stride + col ]; 
}
for (size_t i=0;i < vec.size()/3;++i) {
    for (size_t j=0;j<3;++j) {
        std::cout << access_at<3>(vec,i,j) << " ";
    }
    std::cout << '\n';        
}
using index_t = std::vector<size_t>;
template <int stride>
index_t make_sorted_index(const std::vector<std::string>& values,size_t col) {
    index_t index(values.size() / stride);
    std::iota(index.begin(), index.end(), 0);
    std::sort(index.begin(), 
              index.end(), 
              [&values,&col](size_t a, size_t b) { 
                  return access_at<stride>(values,a,col) < access_at<stride>(values,b,col); 
              } 
    );
    return index;
}
for (size_t i=0;i < vec.size()/3;++i) {
    for (size_t j=0;j<3;++j) {
        std::cout << access_at<3>(vec,index[i],j) << " ";
    }
    std::cout << '\n';        
}
#include <vector>
#include <numeric>
#include <algorithm>
#include <iostream>

template <int stride>
std::string& access_at(std::vector<std::string>& vec,size_t row,size_t col) { return vec[ row * stride + col ]; }
template <int stride>
const std::string& access_at(const std::vector<std::string>& vec,size_t row,size_t col) { return vec[ row * stride + col ]; }

using index_t = std::vector<size_t>;
template <int stride>
index_t make_sorted_index(const std::vector<std::string>& values,size_t col) {
    index_t index(values.size() / stride);
    std::iota(index.begin(), index.end(), 0);
    std::sort(index.begin(), 
              index.end(), 
              [&values,&col](size_t a, size_t b) { return access_at<stride>(values,a,col) < access_at<stride>(values,b,col); } 
    );
    return index;
}


int main() {
    std::vector<std::string> vec = { "1","8","1","2","3","2","3","5","5","2","5","6"};
    for (size_t i=0;i < vec.size()/3;++i) {
        for (size_t j=0;j<3;++j) {
            std::cout << access_at<3>(vec,i,j) << " ";
        }
        std::cout << '\n';        
    }
    std::cout << '\n';
    auto index = make_sorted_index<3>(vec,1);
    for (size_t i=0;i < vec.size()/3;++i) {
        for (size_t j=0;j<3;++j) {
            std::cout << access_at<3>(vec,index[i],j) << " ";
        }
        std::cout << '\n';        
    }
}
1 8 1 
2 3 2 
3 5 5 
2 5 6 

2 3 2 
3 5 5 
2 5 6 
1 8 1 


Share : facebook icon twitter icon

std::unique_ptr in lambda function causing segmentation fault


By : Narcís Nogué Bonet
Date : March 29 2020, 07:55 AM
I hope this helps . Don't name members and method arguments the same thing. However, if you insist on doing so, you should be able to change your lambda capture to [this] instead of [&] to fix the problem.
As a commenter said:

Using an iterator in a recursive function results in a segmentation fault


By : nsnk
Date : March 29 2020, 07:55 AM
may help you . ++it is incrementing the pointer (which makes it invalid) not the iterator. You probably want ++*it.
But that might also take you beyond the end of the list.

Lambda function, segmentation fault prevent


By : Pol Alcarria
Date : March 29 2020, 07:55 AM
wish of those help No suggestion seemed to work for me. I tried std::shared_ptr, std::weak_ptr and QSharedPointer. Seems like the model the item is added to somehow removes the item anyway?! However, I ended up using a QStack where I pushed all the item pointers on the stack and once restored or removed the Items were poped off the stack. Since the time is always the same, this approach works perfekt for me, altough is is not very well implemented.

Segmentation fault when storing lambda as std::function


By : Mohammed AL Harbi
Date : March 29 2020, 07:55 AM
it should still fix some issue You need to be careful with function types, and it should work fine if you use the correct one;
code :
typedef std::function<Vec(const Vec&)> Function; 
// const Vec& -> Vec
std::vector<double> the_lambda(const std::vector<double>& x)
{
    return x;
}

// Vec -> const Vec&
const std::vector<double>& the_function(std::vector<double> x)
{
    return the_lambda(x);
}


int main()
{
    std::vector<double> v = {1, 2};
    std::vector<double> lv = the_lambda(v);    // OK.
    std::vector<double> fv = the_function(v);  // Undefined.
}

Segmentation fault with std::function and lambda parameters


By : Manj
Date : March 29 2020, 07:55 AM
this one helps. Can you please explain why this code crashes? I would expect output of "a", but I get segmentation fault. , The
code :
[](const MyStruct& in) {return in.a;}
[](const MyStruct& in) -> auto {return in.a;}
[](const MyStruct& in) -> const auto& {return in.a;}
Related Posts Related Posts :
  • The rationale behind std::assignable_from possible implementation
  • Dynamic memory allocation output
  • Variable name after struct definition
  • C++ static instance of enum shared between two source files
  • Why does my pointer to a char only get the first character in it?
  • Storing a specific address into a pointer in C++
  • Difference between *x++ and *y += 1
  • How to do array
  • Is converting an integer to a pointer always well defined?
  • Is there a way to code data directly to the hard drive (similar to how one can do with RAM)?
  • Transform range-based for normal loop
  • Need help on the below code , what is the problem with the Below Code
  • How to manage Unicode strings easily in C++
  • Command line client to resolve win64 stack trace (with access to a symbol server)
  • How to replace C++ templates to be C compatible without changing the rest of the code?
  • Logical Private VS Physical Private
  • Why is this segfaulting?
  • How can I join some few numbers into one, for example 1,7,3 = 173?
  • How to implement Front() method to return the first element of a templated doubly linked list C++?
  • condition_variable, references and threads: Who Owns The Lock?
  • C++ new/delete complexity guarantee by the standard
  • Using hash directly in methods from unordered_map instead of the user defined object producing the hash
  • Getting Windows Version from Preprocessor (C++ Win32)
  • Standard Deviation c++ Formula advice
  • Why is one Visual Studio # include file of two not found?
  • C++ read 2 lines in a while loop to a map?
  • Size of array of vectors of different sizes cpp
  • Linking OpenCV to Qt Creator
  • Creating array with const int size parameter in a function throws error in Visual Studio C++: expression did not evaluat
  • How can I store all data the user inputs and then show them if they want to see all of them
  • Use depth texture in compute shader
  • error: conflicting declaration using universal references
  • Using std::optional instead of own structure
  • Is it necessary to make pointer Null when there is no use of it at the end in c++
  • How are std::array's compared in C++?
  • Herb Sutter's article on making virtual functions private
  • Interaction between copy-and-swap idiom and move operations
  • Problems using overloaded extraction operator in C++
  • segmentation fault on reading certain files, seems to be unrelated to filesize
  • Protected members of base classes inaccessible to friends of derived classes
  • Constructing smart pointer from stack allocated raw pointer
  • Array Index Overload Bug
  • Why I'm getting " void value not ignored as it ought to be"?
  • Need help converting a string to a char in c++ ---- ERROR A value of type "const char *" cannot be used to ini
  • What means put preprocessor in std::string?
  • Is it possible to compare an array elements with a hard-coded initializer list without loop?
  • Complexity of an specific algorithm with 3 for?
  • Character converting funtion std::isupper() & std::islower() C++17
  • Hide method taking template parameter reference in cpp file
  • Disable the std::initializer_list constructor
  • What does "static_cast<double>(123)" mean in C++?
  • How to use libraries (*.a files) in C++ when using cmake
  • Use class name for conditional macro expansion
  • C++ variable names (can declare main as variable however same is not true for other functions name)
  • error: ‘nullptr’ was not declared in this scope
  • Why is the z coordinate flipped after multiplying with a matrix in GLSL - OpenGL
  • QML use C ++ function with parameters
  • How can I convert a character from a string into a integer variable using C++
  • Can range-v3 contains work on initializer list?
  • How would I calculate the runtime of this function?
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org