- //Nicholas Riseden
-
//CSCI 3300
-
//Assignment 4 Version 2
-
-
#include "tree.h"
-
#include "pqueue.h"
-
#include <string>
-
#include <fstream>
-
#include <iostream>
-
#include <stdio.h>
-
#include "binary1.h"
-
-
using namespace std;
-
-
/*****************************************************************
-
* buildTree *
-
******************************************************************
-
* buildTree takes the array built from getFrequency and builds a *
-
* tree that holds a character and its priority using the insert *
-
* function. *
-
* *
-
* For example, if the file reads "AAb" it creates a node for the *
-
* character 'A' and stores its priority 2 for that node. *
-
*****************************************************************/
-
Node* buildTree(int arrayOfInts[])
-
{
-
PriorityQueue q;
-
for(int x = 0; x <= 255; x++)
-
{
-
if(arrayOfInts[x]!=0)
-
{
-
Node* leaf = new Node((char)x);
-
insert(q, leaf, arrayOfInts[x]);
-
}
-
}
-
-
Node* t1,*t2;
-
int p1, p2;
-
remove(q, t1, p1);
-
-
while(isEmpty(q)==false)
-
{
-
remove(q,t2, p2);
-
Node* t3 = new Node(t1, t2);
-
insert(q, t3, (p1 +p2));
-
remove(q, t1, p1);
-
}
-
-
return t1;
-
}
-
-
/*****************************************************************
-
* getFrequency *
-
******************************************************************
-
* getFrequency takes an array of 256 integers. It opens a *
-
* file and counts the amount of character *
-
* frequencies that occur, storing that count into the position *
-
* in the array that correpsonds with the character's *
-
* Ascii value. *
-
* *
-
* For example, if the file reads "AAb" it stores the number 2 in *
-
* position 64 in the array since capital A is a 65 on the Ascii *
-
* chart and arrays are zero based. *
-
*****************************************************************/
-
-
int* getFrequency(char* filename, int arrayOfInts[])
-
{
-
ifstream in;
-
in.open(filename);
-
for(int i = 0; i < 256; i++)
-
{
-
arrayOfInts[i] = 0;
-
}
-
-
-
-
int c = in.get();
-
-
while(c!=EOF)
-
{
-
arrayOfInts[c]++;
-
c = in.get();
-
}
-
-
int count = 0;
-
cout<<"\nThe character frequencies are: \n\n";
-
int k = 0;
-
while(k < 256)
-
{
-
-
if(k>0)
-
{
-
-
if(k == 10)
-
{
-
cout<<"\\n "<<arrayOfInts[k]<<"\n";
-
}
-
else if(k!=10)
-
{
-
cout<<char(k)<< " "<<arrayOfInts[k]<<"\n";
-
}
-
}
-
-
-
count++;
-
cout<<"count is "<<count<<"\n";//count reaches 256, then segmentation fault is printed
-
k++;
-
}
-
-
in.close();
-
cout<<"This is sparta\n";
-
return arrayOfInts;
-
cout<< "This is over\n";
-
-
}
-
-
//comments to describe this function
-
void writeTree(BFILE* f, Node* t)
-
{
-
if(t->kind!=leaf)
-
{
-
writeBit(f, 0);
-
writeTree(f, t->left);
-
writeTree(f, t->right);
-
}
-
else
-
{
-
writeBit(f, 1);
-
writeByte(f, t->ch);
-
}
-
-
-
}
-
//buildcode
-
void buildCode(Node* n, string codeArray[], string pref)
-
{
-
if(n->kind == leaf)
-
{
-
codeArray[int(n->ch)] = pref;
-
}
-
else if(n->kind == nonleaf)
-
{
-
buildCode(n->left, codeArray, pref + "0");
-
buildCode(n->right, codeArray, pref + "1");
-
}
-
-
}
-
//writecode(f, str) writes string str to BFILE* f, one bit
-
//at a time.
-
void writeCode(BFILE* f, string str)
-
{
-
int y = str.length();
-
for(int x = 0; x < y; x++)
-
{
-
writeBit(f, str[x]);
-
}
-
-
}
-
-
//function description
-
void writeCodedFile(char* filename, string codearray[], BFILE* f)
-
{
-
ifstream in;
-
in.open(filename);
-
int c = cin.get();
-
while(c != EOF)
-
{
-
writeCode(f, codearray[c]);
-
c = cin.get();
-
}
-
cout<<"Test E";
-
in.close();
-
}
-
-
-
int main(int argc, char* argv[])
-
{
-
string codeArray[255];
-
int arrayOfInts[255];
-
int* frequencyArray = getFrequency(argv[1], arrayOfInts);//problem is here
-
cout<<"I am about to do buildTree.";//this never gets printed
-
Node* n = buildTree(frequencyArray);
-
cout<<"I am done buildTree.";//this never gets printed
-
buildCode(n, codeArray, "");
-
-
BFILE* f = openBinaryFileWrite(argv[2]);
-
-
writeTree(f,n);
-
-
writeCodedFile(argv[1], codeArray,f);
-
-
closeBinaryFileWrite(f);
-
-
return 0;
-
-
}
Dec 8 '09
18 3000
Could you debug this and show me the line where the program stopped?
This is my current code
See attached for output
It is doing exactly what I want, just printing the segmentation fault at the end. It is not getting to any other functions before or after it because of the segmentation fault -
-
/*****************************************************************
-
* getFrequency *
-
******************************************************************
-
* getFrequency takes an array of 256 integers. It opens a *
-
* file and counts the amount of character *
-
* frequencies that occur, storing that count into the position *
-
* in the array that correpsonds with the character's *
-
* Ascii value. *
-
* *
-
* For example, if the file reads "AAb" it stores the number 2 in *
-
* position 64 in the array since capital A is a 65 on the Ascii *
-
* chart and arrays are zero based. *
-
*****************************************************************/
-
-
int* getFrequency(char* filename, int arrayOfInts[])
-
{
-
ifstream in;
-
in.open(filename);
-
for(int i = 0; i <= 256; i++)
-
{
-
arrayOfInts[i] = 0;
-
}
-
-
int c = in.get();
-
while(c!=EOF)
-
{
-
arrayOfInts[c]++;
-
c = in.get();
-
}
-
-
cout<<"\nThe character frequencies are: \n\n";
-
for(int count = 0; count<=256; count++)
-
{
-
while(arrayOfInts[count]!=0)
-
{
-
if(count == 10)
-
{
-
cout<<"\\n "<<arrayOfInts[count]<<"\n";
-
count++;
-
}
-
else
-
{
-
cout<<char(count)<< " "<<arrayOfInts[count]<<"\n";
-
count++;
-
}
-
}
-
-
}
-
-
in.close();
-
-
return arrayOfInts;
-
-
-
}
The image is too small to see. Actually, even if i can see it clearly, i can't tell you what the error is, because there is not enough information.
According to your code, i saw that the upper bound of loop is 256, while the array size is 256. Correct it to 255.
Also, change the print loop as the following code, run it and show me last output lines (3 lines at most). -
cout<<"\nThe character frequencies are: \n\n";
-
for(int count = 0; count<=255; count++)
-
{
-
cout << count << " ";
-
if (count == 10)
-
cout << "\\n ";
-
else
-
cout << char(count) << " ";
-
-
cout << arrayOfInts[count]<<"\n";
-
}
-
The last three lines are as follows:
254 \ufffd 0
255 \ufffd 0
Segmentation fault
nar0122@tecs-cs-sshpol3:~/Desktop/3300/assn4/programOne>
I have run your code successfully, and the output is different from yours.
I guess the error is somewhere outside this function.
Also, could you send me you input file?
You should use debugging tools to find the error more easily.
Frinavale 9,735
Recognized Expert Moderator Expert
I see what you're doing now.
You're trying to do too much with one variable.
You need one variable to keep track of how many times you have iterated through the array (how many times you've looped).
You should be using another variable to print the number of characters found in the array at that iteration.
If there is 0 for a specific character then don't do anything ...just loop to the next index.
This is a much cleaner approach that I hope will fix your problem. -
int* getFrequency(char* filename, int arrayOfInts[])
-
{
-
ifstream in;
-
for(int i = 0; i < 256; i++)
-
{
-
arrayOfInts[i] = 0;
-
}
-
-
in.open(filename);
-
int c = in.get();
-
while(c!=EOF)
-
{
-
if(c>0 && c<256)
-
{
-
arrayOfInts[c]++;
-
}
-
c = in.get();
-
}
-
in.close();
-
-
cout<<"\nThe character frequencies are: \n\n";
-
int numberOfCharacters = 0;
-
for(int i = 0; i<256; i++)
-
{ numberOfCharacters = arrayOfInts[i];
-
if(i==10}
-
{ cout << "\\n ";}
-
if(numberOfCharacters > 0)
-
{
-
cout<<char(i)<< " "<<numberOfCharacters<<"\n";
-
}
-
}
-
-
return arrayOfInts;
-
}
-Frinny
PS. Could you please post code snippets in code tags.
I did not get the segmentation fault after modifying my program; however, I got some pretty crazy errors. I think it is saying I have a memory leak somewhere.
Does this mean anything to anyone?
*** glibc detected *** ./huffman: free(): invalid pointer: 0x00007fffd5592 6f8 ***
Frinavale 9,735
Recognized Expert Moderator Expert
Nar0122, does that mean your original problem is fixed?
If so, please start a new thread for your new problem and indicate which post helped you to solve the problem you needed help with in this thread.
-Frinny
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Jay donnell |
last post by:
I have a short multi-threaded script that checks web images to make
sure they are still there. I get a segmentation fault everytime I run
it and I can't figure out why. Writing threaded scripts is new to me so
I may be doing something wrong that should be obvious :(
google messes up the python code so here is a link to it.
http://kracomp.com/~jay/py.txt
This is the output of the script.
|
by: fudmore |
last post by:
Hello Everybody.
I have a Segmentation fault problem. The code section at the bottom
keeps throwing a Segmentation fault when it enters the IF block for the
second time.
const int WORDS_PER_LINE = 4;
when counter == 7 is when the string Concatenation fails within the IF
block.
|
by: Polar |
last post by:
Hi!
i'm a newbie in C language and i'm writing my first simple codes.
In one of these, my purpose is to append the ascii value of an interger
(example 101 --> e) at the end of a string to obtain a new (longer)
string. Example:
string: languag
letter: e
|
by: damian birchler |
last post by:
If I run the following I get a segmentation fault:
#define NAMELEN 15
#define NPERS 10
typedef struct pers {
char name;
int money;
} pers_t;
|
by: I_have_nothing |
last post by:
Hi!
I am new in C. I try to use dynamical allocation fuction malloc( ) and
realloc( ).
I found something strange.
After several calling realloc( ), the malloc( ) will give me a
Segmentation fault.
If I just call realloc( ) once before calling malloc( ), it is OK.
Why?
I am trying to read some double-typed items from infile and save them
| |
by: Fra-it |
last post by:
Hi everybody,
I'm trying to make the following code running properly, but I can't get
rid of the "SEGMENTATION FAULT" error message when executing.
Reading some messages posted earlier, I understood that a segmentation
fault can occur whenever I declare a pointer and I leave it
un-initialized.
So I thought the problem here is with the (const char *)s in the stuct
flightData (please note that I get the same fault declaring as char *
the...
|
by: Paminu |
last post by:
I have a wierd problem.
In my main function I print "test" as the first thing. But if I run the call
to node_alloc AFTER the printf call I get a segmentation fault and test is
not printed!
#include <stdlib.h>
#include <stdio.h>
typedef struct _node_t {
|
by: pycraze |
last post by:
I would like to ask a question. How do one handle the exception due to
Segmentation fault due to Python ? Our bit operations and arithmetic
manipulations are written in C and to some of our testcases we
experiance Segmentation fault from the python libraries.
If i know how to handle the exception for Segmentation fault , it will
help me complete the run on any testcase , even if i experiance Seg
Fault due to any one or many functions in...
|
by: madunix |
last post by:
My Server is suffering bad lag (High Utlization)
I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2
Web visitors retrieve data from the web by php calls through oci
cobnnection from 10g release2
PHP is configured with the following parameters
'./configure' '--prefix=/opt/oracle/php'
'--with-apxs=/opt/oracle/apache/bin/apxs'
'--with-config-file-path=/opt/oracle/apache/conf' '--enable-safe-mode'
'--enable-session'...
|
by: DanielJohnson |
last post by:
int main()
{
printf("\n Hello World");
main;
return 0;
}
This program terminate just after one loop while the second program
goes on infinitely untill segmentation fault (core dumped) on gcc. The
only difference is that in first I only call "main" and in second call
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |