Cherry Cool
Cherry Cool

Reputation: 69

Initializing an array of pointers

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;
}

Upvotes: 0

Views: 159

Answers (4)

Rob K
Rob K

Reputation: 8926

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?

Upvotes: 0

rcgldr
rcgldr

Reputation: 28818

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;
}

Upvotes: 1

rcgldr
rcgldr

Reputation: 28818

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

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

Upvotes: 0

froger
froger

Reputation: 11

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

Upvotes: 0

Related Questions