Jump to content
  • Advertisement
Sign in to follow this  
Boooke

Pointertrouble

This topic is 2527 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
Not sure if you're learning how to use pointers, but as BeerNutts stated, you're not using them in the proper context or way.

What are you attempting to accomplish in your code from using pointers? I see no reason to allocate memory, or any reason to need the address of a variable, as you're simply using one function with no apparent need to pass a variable, ect...

Here's a small example I typed up. You create a Number, and a Pointer to that Number, then increase it by 10 in another Function.


#include <iostream>

void Add(int *Number)
{
*Number += 10;
}

int main()
{
int Number = 5;
int *Pointer_For_Number = &Number;
std::cout << "Number = " << Number;
std::cout << std::endl;
std::cout << "Adding Number By 10!!!";
std::cout << std::endl;
Add(Pointer_For_Number);
std::cout << "Number = " << Number;
std::cout << std::endl;
std::cout << "Adding Number By 10!!!";
std::cout << std::endl;
Add(Pointer_For_Number);
std::cout << "Number = " << Number;
std::cout << std::endl;
std::cin.get();
return 0;
}


I would also learn to pass by Reference!

Share this post


Link to post
Share on other sites
I agree with BeerNutts and Black Rock. You should also be aware that what you posted has a memory leak. I can't see a need for dynamic memory from what you wrote but if for some reason you do need to use Dynamic Memory you need to clean up the memory you allocated before you exit your program.

Immediately after you write the word [color=#0000cd]new in your program, you should figure out where you are going to [color=#0000cd]delete it and write that line in next.

Share this post


Link to post
Share on other sites
Just to follow up with JustColorado, make sure you never use that Pointer again after [color=#0000ff]delete unless you've assigned it to something again. Also a big mistake is to use [color=#0000ff]new twice with the same pointer. Always follow [color=#0000ff]new with[color=#0000FF] delete.

Share this post


Link to post
Share on other sites
Sorry for the late answer. Yes, I realized. I am going to use static variables instead. I haven't been working with pointers before, and I did not see how to actually solve the problem. I don't see why I would prefer pointers now. The arrays I need to reuse do not need to be resized anyways, which was my reason to use pointers. I guess I did not really think through that one. but thank you for the help and answers :-)

Regards,
Boooke

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!