0

I want to initialize an array of pointers. This is my code. It works fine for smaller numbers i.e 9979, but when I try to initialize dynamically to 66071, it shows segmentation fault. Now I had in another program, initialized dynamically an array of integers to a size of 120007, which worked fine, i.e. no segmentation faults. Any idea why this error is happening for array of pointers? Could it be due to the structure size of "dictnode"?

struct dictnode{
    string word;
    int key;
    int code;
}; 
class LZW{
    dictnode **de_table; 
    int count_dec;
    int desize;

    public:
    LZW();
    //DECOMPRESSION
    void decompress();
    void storedictdec(string str1, string str2);
    void storedictdec1(string str1, string str2, int dec);
    bool checkdictdec(int n);
    void initialize_dec_dict();
    void printdictdec();
    int quadprobe(int k, int i);
};

LZW::LZW(){
    desize=66071;
    count_dec=0;
    de_table = new dictnode* [desize];

    for(int i=0; i<desize; i++){
        de_table[i]=NULL;
    }

    return;
}
7
  • 1
    Show the declaration for de_table please. Commented Oct 1, 2014 at 14:03
  • What type is de_table? Commented Oct 1, 2014 at 14:03
  • 1
    Is there some compelling reason to not use std::vector for the dynamic array? Additionally you probably want to use the initalizer list for your class en.cppreference.com/w/cpp/language/initializer_list Commented Oct 1, 2014 at 14:06
  • Which line gives you the seg fault? Commented Oct 1, 2014 at 14:07
  • 2
    @mwigdahl de_table is where de_data is:) Commented Oct 1, 2014 at 14:15

4 Answers 4

1

I don't get any errors with this simplified test code:

#include <string>

struct dictnode{
std::string word;
int key;
int code;
}; 

class LZW{
dictnode **de_table; 
size_t desize;
int count_dec;

public:
LZW();
~LZW();
};

LZW::LZW()
{
    desize=66071;
    count_dec=0;
    de_table = new dictnode* [desize];
    for(size_t i=0; i<desize; i++)
        de_table[i]=NULL;
}

LZW::~LZW()
{
    delete[] de_table;
}

int main()
{
    LZW *lzw = new LZW;
    delete lzw;
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

0

I tried with

#include <iostream>
using namespace std;

struct dictnode{
string word;
int key;
int code;

}; 

int main(int argc, char**argv) {
int desize=66071;
int count_dec=0;
dictnode** de_table = new dictnode* [desize];

for(int i=0; i<desize; i++){
    de_table[i]=NULL;
}

return 0;
}

Everything goes fine, valgrind returns:

==4036== Memcheck, a memory error detector
==4036== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==4036== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==4036== Command: ./a.out
==4036== 
==4036== 
==4036== HEAP SUMMARY:
==4036==     in use at exit: 528,568 bytes in 1 blocks
==4036==   total heap usage: 1 allocs, 0 frees, 528,568 bytes allocated
==4036== 
==4036== LEAK SUMMARY:
==4036==    definitely lost: 528,568 bytes in 1 blocks
==4036==    indirectly lost: 0 bytes in 0 blocks
==4036==      possibly lost: 0 bytes in 0 blocks
==4036==    still reachable: 0 bytes in 0 blocks
==4036==         suppressed: 0 bytes in 0 blocks
==4036== Rerun with --leak-check=full to see details of leaked memory
==4036== 
==4036== For counts of detected and suppressed errors, rerun with: -v
==4036== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Please, show us full scope of this code

2 Comments

While this is some nice work it doesn't really help answer the question (which is lacking a lot).
Umm...I just need to know why the initialization works with smaller integers as i mentioned, but not with a large integer.
0

Looks ok now, but create a destructor to delete de_table:

LZW::~LZW(){
    delete[] de_table;
    return;
}

1 Comment

Doesn't work. Still shows segmentation fault. It has something to with initialization of array of pointers.
0

You probably need to increase the amount of heap available to your program (this is controlled by the linker). A program doesn't automatically get access to all of the memory available in a computer.

What compiler are you using on what OS?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.