Jump to content
  • Advertisement
Sign in to follow this  
Boooke

Pointertrouble

This topic is 2522 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I am currently trying to read and analyze a text (or multiple, one at a time). The purpose is to read a text, find the lettercount, calculate the probability of each of the letters. I use pointers to remove unused variables from completed readings and to create new ones for the next reading.

The code is as following


// Huffman Encoder v1.0 - Begun 06/01/2012, updated 06/01/2012
// Drag and drop a text file to be encoded and encode it.
// Mikkel S. Bugge
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
// Check to see if text has been dragged onto
if(argc < 2)
{
cout << "Drag file onto .exe file to encode" << endl;
cin.get();
}
else
{
int read = 1; // Number of read files
while(read != argc)
{
double **d_arLetters;
double **d_arLettersProb;
string line; // Text
d_arLetters = new double *[256]; // Letters and usecount
d_arLettersProb = new double *[256]; // Probability
// Reset value and prob of letters
for(int j = 0;j < 256;j++)
{
d_arLetters[j] = 0;
d_arLettersProb[j] = 0;
}
float * f_letterCount = 0; // Pool (Number) of letters
for(int i=1;i<argc;i++)
{
cout << "Encoding file \"" << argv << "\"\n" << endl;
ifstream fi(argv);

while(getline(fi,line)) // Read line till end of file;
{
f_letterCount += line.length(); // Problem

for(int k=0; k < *f_letterCount; k++) // Problem!?
{
d_arLetters[int(line[k])]++; // Add letter to letterpool.
}
// Calculate proability
for(int k = 0; k < 256; k++)
{
*d_arLettersProb[k] = *d_arLetters[k] / *f_letterCount;
}
cout << line << endl;
}
cout << "Probability of e: " << *d_arLettersProb[101];
}
read++;
}

}
cin.get();
return 0;
}


The output given is generally to see if its working. The problem is, that crashes around the for loop with the comment "Problem!?". Its a long time since I have worked with pointers, so I cant remember much of it. Some of the code may be rubbish, if not all of it. Though, its not finished yet. I hope someone can help me fix the error.

Regards,
Boooke

Share this post


Link to post
Share on other sites
Advertisement
Most probably f_letterCount isn't a valid pointer, because you initialise it to zero and don't set it to anything later. I think you want to make f_letterCount a normal float variable, and get rid of the * in the line commented "Problem!?".

Share this post


Link to post
Share on other sites
Hey again. Thank you for the reply. I have already tried your suggestion (Which i did first, actually). It gives me a problem later on, when I try to use it later on in the line,


...
*d_arLettersProb[k] = *d_arLetters[k] / *f_letterCount;
...


I can try show the error later on, but right now, I dont have the code around.

Regards,
Boooke

Share this post


Link to post
Share on other sites
Alright, you seem to be using too many pointers to things. Get rid of one * from the lines
double **d_arLetters;
double **d_arLettersProb;
and get rid of all the other occurences of * in the body of the main function

Share this post


Link to post
Share on other sites
You should be using static arrays (e.g. double letterProbability[256]) rather than pointers to dynamically allocated arrays.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!