Hi,

I am getting the following error when I am trying to initialize a 2 dimensional vector with 0's.
I am not to figure out the reason. Any help is appreciated.

std::vector<int> truth_table;
        std::vector<vector<int> > truth_table_col;

        double no_of_twos = 7;
        double base_two = 2;
        
        cout << "No of 2's = " << no_of_twos << "\n";
        double combi = pow(base_two,no_of_twos)-1;

        for(int hfh=0; hfh<=(int)combi; hfh++)
        {
                for(int ghg=0; ghg<=(int)no_of_twos; ghg++)
                {
                        truth_table.push_back(0);
                }
                truth_table_col.push_back(truth_table);
                truth_table.clear();
        }

Program received signal SIGSEGV, Segmentation fault.
0xb7433aed in _int_malloc () from /lib/tls/libc.so.6

Thanks

Dani AI

Generated

Brief follow-up and practical tips.

A crash inside glibc's _int_malloc normally means heap corruption or an attempt to allocate an unexpectedly large block — not a simple logic error in push_back. Undefined behavior (out‑of‑bounds writes, wrong loop bounds, mixing integer/double counts, or a wrong type/name lookup) can quietly succeed on one toolchain (VC++) and blow up when the allocator detects corruption on another. and were right to check the types and loop bounds; the allocator fault is the symptom, not the root cause.

Safer, concrete practices to avoid this class of bugs:

  • Use integer types for counts (prefer size_t or unsigned) instead of double. For powers of two use integer shifts for exact results rather than pow. For small n:

    unsigned no_of_twos = 7;
    size_t combos = (1u << no_of_twos) - 1;
  • Compute loop limits once (outside the loop) and use < limit rather than <= where appropriate to avoid off‑by‑one errors.

  • Initialize 2‑D arrays with resize/assign or reserve capacity instead of repeatedly push_back/clear — this both documents intent and reduces reallocation/copy surface where a bug might hide.

  • Add simple bounds checks or asserts when converting floating counts to integers.

Debug checklist (fast): enable warnings (-Wall -Wextra), compile with debug symbols, run under AddressSanitizer or Valgrind to catch heap/UB quickly. See the vector and math docs and sanitizer guides for details: std::vector, std::pow, AddressSanitizer, Valgrind quick start.

Thanks to and for the pointers — follow those checks first and use the diagnostics above if the crash persists.

Recommended Answers

All 3 Replies

1. You forgot to add std:: prefix for vector in template parameter.
2. Barbaric construct: hfh<=(int)combi . Make cast before loop.
This code works with VC++ 2008. Yes, it's a bad code: for example, you can initialize 2D vector in a simple, clear and effective manner:

truth_table.resize(n,0);
truth_table_col.resize(m,truth_table);
truth_table.clear();

This works great for me, using VC++ 2008 Express

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

int main(int argc, char* argv[])
{
        std::vector<int> truth_table;
        std::vector<vector<int> > truth_table_col;

        double no_of_twos = 7;
        double base_two = 2;
        
        cout << "No of 2's = " << no_of_twos << "\n";
        double combi = pow(base_two,no_of_twos)-1;

        for(int hfh=0; hfh<=(int)combi; hfh++)
        {
                for(int ghg=0; ghg<=(int)no_of_twos; ghg++)
                {
                        truth_table.push_back(0);
                }
                truth_table_col.push_back(truth_table);
                truth_table.clear();
        }
        for(size_t i = 0; i < truth_table_col.size(); i++)
        {
            vector<int> &table = truth_table_col[i];
            for(size_t j = 0; j < table.size(); j++)
                cout << table[j] << " ";
            cout << "\n";
        }
}

Thanks.. the error is resolved......the suggestions are really helpful...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.