Segmentation Fault

Thread Starter

kurtruk

Joined Aug 26, 2012
140
Let me post the code first of all.

main.cpp:
Rich (BB code):
#include <iostream>
#include "Font.h"
#include "DisplayFunctions.h"
using namespace std;

DisplayFunctions dfObj;

int main()
{
    dfObj.getMsg();
    dfObj.createByteMap();
    return 0;
}
Font.h:
Rich (BB code):
#ifndef FONT_H
#define FONT_H

class Font
{
    public:
       /*Set Up font to ASCII character*/
      short int ASCII [127][8] = {
            {  0,   0,   0,   0,   0,   0,   0,   0},          //     0
            {  0,   0,   0,   0,   0,   0,   0,   0},          //     1
            {  0,   0,   0,   0,   0,   0,   0,   0},          //     2
            {  0,   0,   0,   0,   0,   0,   0,   0},          //     3
            {  0,   0,   0,   0,   0,   0,   0,   0},          //     4
            {  0,   0,   0,   0,   0,   0,   0,   0},          //     5
            {  0,   0,   0,   0,   0,   0,   0,   0},          //     6
            {  0,   0,   0,   0,   0,   0,   0,   0},          //     7
            {  0,   0,   0,   0,   0,   0,   0,   0},          //     8
            {  0, 256, 256, 256, 256, 256, 256, 256},          //     9
            { 60,  66, 169, 133, 133, 169,  66,  60},          //  ☺  10
            {  0,   8,   8,   8,  42,  28,   8, 256},          //  →  11
            {  0,   8,  28,  42,   8,   8,   8, 256},          //  ←  12
            {  0,  16,  32, 127,  32,  16, 256, 256},          //  ↑  13
            {  0,   4,   2, 127,   2,   4, 256, 256},          //  ↓  14
            {  0,   4,   2,  49,   1,  49,   2,   4},          //  (: 15
            {  0,  17,  41,  41,  69,  69, 256, 256},          //  ≤  16
            {  0,  69,  69,  41,  41,  17, 256, 256},          //  ≥  17
            {  0,  29,  39,  32,  39,  29, 256, 256},          //  Ω  18
            {  0,  63,   2,   2,   4,  62, 256, 256},          //  µ  19
            {  0,  21,  22,  28,  52,  84, 256, 256},          //  ≠  20
            {  0,  16,  15,  24,   8,  31,   8, 256},          //  π  21
            {  0,  18,  36,  36,  18,  36, 256, 256},          //  ≈  22
            {  0,   6,   1,   6,  56, 192, 256, 256},          //  √  23
            {  0,   8,   8,  42,   8,   8, 256, 256},          //  ÷  24
            {  0, 249,   0, 249, 256, 256, 256, 256},          //  ‼  25
            {  0,  24,  36,  36,  24,  36, 256, 256},          //  α  26
            {  0,   3,   7, 254,  64,  48, 256, 256},          //  ♪  27
            {  0,   3,  31,  32,  64, 140, 252, 256},          //  ♫  28
            {  0,   6,   1,   6, 248, 128, 128, 256},          //  √  29
            {  0, 255, 255, 255, 255, 255, 256, 256},          //  █  30
            {  0,  34,  34, 250,  34,  34, 256, 256},          //  ±  31
            {  0,   0,   0,   0,   0, 256, 256, 256},          //     32
            {  0, 249, 256, 256, 256, 256, 256, 256},          //  !  33
            {  0, 224,   0, 224,   0, 256, 256, 256},          //  "  34
            {  0,  20, 127,  20, 127,  20, 256, 256},          //  #  35
            {  0,  38,  82, 255,  74,  36, 256, 256},          //  $  36
            {  0, 193, 198,  24,  99, 131, 256, 256},          //  %  37
            {  0,  54,  73,  85,  34,   5, 256, 256},          //  &  38
            {  0, 224,   0, 256, 256, 256, 256, 256},          //  '  39
            {  0,  60,  66, 129, 256, 256, 256, 256},          //  (  40
            {  0, 129,  66,  60, 256, 256, 256, 256},          //  )  41
            {  0,  80,  32, 248,  32,  80, 256, 256},          //  *  42
            {  0,   8,   8,  62,   8,   8, 256, 256},          //  +  43
            {  0,   1,   6, 256, 256, 256, 256, 256},          //  ,  44
            {  0,   8,   8,   8,   8,   8, 256, 256},          //  -  45
            {  0,   3,   3, 256, 256, 256, 256, 256},          //  .  46
            {  0,   1,   6,  24,  96, 128, 256, 256},          //  /  47
            {  0, 126, 129, 129, 129, 126, 256, 256},          //  0  48
            {  0,  33,  65, 255,   1,   1, 256, 256},          //  1  49
            {  0,  97, 131, 133, 137, 145,  97, 256},          //  2  50
            {  0, 129, 137, 137, 137, 118, 256, 256},          //  3  51
            {  0, 248,   8,   8,   8, 255, 256, 256},          //  4  52
            {  0, 226, 145, 145, 145, 146, 256, 256},          //  5  53
            {  0,  62,  81, 145, 145,  14, 256, 256},          //  6  54
            {  0, 129, 134, 152, 160, 192, 256, 256},          //  7  55
            {  0, 118, 137, 137, 137, 118, 256, 256},          //  8  56
            {  0, 112, 137, 137, 138, 124, 256, 256},          //  9  57
            {  0,  54,  54, 256, 256, 256, 256, 256},          //  :  58
            {  0,  49,  54, 256, 256, 256, 256, 256},          //  ;  59
            {  0,   8,  20,  20,  34,  34, 256, 256},          //  <  60
            {  0,  20,  20,  20,  20, 256, 256, 256},          //  =  61
            {  0,  34,  34,  20,  20,   8, 256, 256},          //  >  62
            {  0,  64, 128, 133, 136, 112, 256, 256},          //  ?  63
            {  0,  60,  66, 153, 165, 189, 133, 121},          //  @  64
            {  0,  63,  72, 136,  72,  63, 256, 256},          //  A  65
            {  0, 255, 137, 137, 137, 118, 256, 256},          //  B  66
            {  0, 126, 129, 129, 129,  66, 256, 256},          //  C  67
            {  0, 255, 129, 129,  66,  60, 256, 256},          //  D  68
            {  0, 255, 137, 137, 137, 129, 256, 256},          //  E  69
            {  0, 255, 136, 136, 136, 128, 256, 256},          //  F  70
            {  0, 126, 129, 129, 137,  78, 256, 256},          //  G  71
            {  0, 255,   8,   8,   8, 255, 256, 256},          //  H  72
            {  0, 129, 129, 255, 129, 129, 256, 256},          //  I  73
            {  0,   6,   1,   1, 129, 254, 256, 256},          //  J  74
            {  0, 255,   8,  52,  66, 129, 256, 256},          //  K  75
            {  0, 255,   1,   1,   1,   1, 256, 256},          //  L  76
            {  0, 255,  64,  32,  16,  32,  64, 255},          //  M  77
            {  0, 255,  64,  48,  12,   2, 255, 256},          //  N  78
            {  0, 126, 129, 129, 129, 129, 126, 256},          //  O  79
            {  0, 255, 136, 136, 136, 112, 256, 256},          //  P  80
            {  0, 126, 129, 129, 133, 130, 125, 256},          //  Q  81
            {  0, 255, 136, 140, 138, 113, 256, 256},          //  R  82
            {  0, 114, 137, 137, 137,  79, 256, 256},          //  S  83
            {  0, 128, 128, 255, 128, 128, 256, 256},          //  T  84
            {  0, 254,   1,   1,   1,   1, 254, 256},          //  U  85
            {  0, 248,   6,   1,   6, 248, 256, 256},          //  V  86
            {  0, 254,   1,   3,  28,   3,   1, 254},          //  W  87
            {  0, 195,  36,  24,  24,  36, 195, 256},          //  X  88
            {  0, 240,   8,   7,   8, 240, 256, 256},          //  Y  89
            {  0, 131, 133, 137, 145, 161, 193, 256},          //  Z  90
            {  0, 255, 129, 129, 256, 256, 256, 256},          //  [  91
            {  0, 128,  96,  24,   6,   1, 256, 256},          //  \  92
            {  0, 129, 129, 255, 256, 256, 256, 256},          //  ]  93
            {  0,  32,  64, 128,  64,  32, 256, 256},          //  ^  94
            {  0,   1,   1,   1,   1,   1, 256, 256},          //  _  95
            {  0, 128,  64,  32, 256, 256, 256, 256},          //  `  96
            {  0,   2,  21,  21,  21,  15, 256, 256},          //  a  97
            {  0, 255,  17,  17,  17,  14, 256, 256},          //  b  98
            {  0,  14,  17,  17,  17, 256, 256, 256},          //  c  99
            {  0,  14,  17,  17,  17, 255, 256, 256},          //  d  100
            {  0,  14,  21,  21,  21,  12, 256, 256},          //  e  101
            {  0,   8, 127, 136, 128,  64, 256, 256},          //  f  102
            {  0,  24,  37,  37,  37,  62, 256, 256},          //  g  103
            {  0, 255,   8,  16,  16,  15, 256, 256},          //  h  104
            {  0,  95, 256, 256, 256, 256, 256, 256},          //  i  105
            {  0,   2,   1,   1,  94, 256, 256, 256},          //  j  106
            {  0, 255,   8,  20,  34,   1, 256, 256},          //  k  107
            {  0, 129, 255,   1, 256, 256, 256, 256},          //  l  108
            {  0,  31,  16,  12,  16,  15, 256, 256},          //  m  109
            {  0,  31,   8,  16,  16,  15, 256, 256},          //  n  110
            {  0,  14,  17,  17,  17,  14, 256, 256},          //  o  111
            {  0,  31,  20,  20,  20,   8, 256, 256},          //  p  112
            {  0,   8,  20,  20,  12,  31, 256, 256},          //  q  113
            {  0,  31,   8,  16,  16,   8, 256, 256},          //  r  114
            {  0,   9,  21,  21,  21,  18, 256, 256},          //  s  115
            {  0,  16, 254,  17,   2, 256, 256, 256},          //  t  116
            {  0,  31,   1,   1,   2,  31, 256, 256},          //  u  117
            {  0,  28,   2,   1,   2,  28, 256, 256},          //  v  118
            {  0,  30,   1,   6,   1,  30, 256, 256},          //  w  119
            {  0,  17,  10,   4,  10,  17, 256, 256},          //  x  120
            {  0,  24,   5,   5,   5,  30, 256, 256},          //  y  121
            {  0,  19,  21,  21,  25, 256, 256, 256},          //  z  122
            {  0,  24, 126, 129, 256, 256, 256, 256},          //  {  123
            {  0, 255, 256, 256, 256, 256, 256, 256},          //  |  124
            {  0, 129, 126,  24, 256, 256, 256, 256},          //  } 125
            {  0,   8,  16,  16,   8,  16, 256, 256},          //  ~  126
        };
};

#endif // FONT_H
 

Thread Starter

kurtruk

Joined Aug 26, 2012
140
DisplayFunctions.h
Rich (BB code):
#ifndef DISPLAYFUNCTIONS_H
#define DISPLAYFUNCTIONS_H

class DisplayFunctions
{
    public:
        short int *rmsgByteMap;
        char msg [512];
        int numberOfCol;
        int characterArrayLength[];

        void getMsg();
        char createCharacterByteMap(char c);
        void createByteMap();
        void send();
};
#endif // DISPLAYFUNCTIONS_H
DisplayFunctions.cpp:
Rich (BB code):
#include "DisplayFunctions.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include<stdio.h>
#include<conio.h>
#include "Font.h"
using namespace std;

Font fontObj;

void DisplayFunctions::getMsg(){
    int characterLength =0;
    int msgLength=0;
    cin.getline(msg, 512);

    for(msgLength = 0; msg[msgLength] != '\0'; msgLength++);

    for(int character = 0; character < msgLength; character++){
        for(int i=0; i < 8; i++){
            if (fontObj.ASCII[(msg[character])] != 256){
                characterLength++;
            }
        }
        characterArrayLength[character] = characterLength;
        numberOfCol += characterLength;
        characterLength = 0;
    }
}

void DisplayFunctions::createByteMap(){
    rmsgByteMap=new short int[numberOfCol];
    int Character = 0;
    int charColIndex=0;

    for (int col=0;col<numberOfCol;col++){
        rmsgByteMap[col] = fontObj.ASCII[msg[Character]][charColIndex];
        charColIndex++;

        if(charColIndex == characterArrayLength[Character]){
            charColIndex = 0;
            Character++;
        }
    }
    cout << "rmsgByteMap = " << endl;
    cout << "{";
    for(int col=0;col<numberOfCol;col++){
        cout << rmsgByteMap[col] << ", ";
    }
    cout << "}" << endl;
}



I get a segmentation fault.

Stepping though the program reveals that it happens at the very end even after return 0;

I am unfamiliar with debugers. The "call stack" says 00457CDA std::basic_ostream<wchar_t, std::char_traits<wchar_t> >::flush()

Here's some more info that I scarcely know the meaning of:
Problem Event Name: APPCRASH
Application Name: MATRIX~1.EXE
Application Version: 0.0.0.0
Application Timestamp: 5307fff6
Fault Module Name: MATRIX~1.EXE
Fault Module Version: 0.0.0.0
Fault Module Timestamp: 5307fff6
Exception Code: c0000005
Exception Offset: 00057cda
OS Version: 6.1.7601.2.1.0.256.48
Locale ID: 1033
Additional Information 1: fdef
Additional Information 2: fdef00b82efcfeb2a8d9fda4b24bbd53
Additional Information 3: 965d
Additional Information 4: 965df9fcde076979e37092106956450b


It only happens when the message that the user enters is somewhat long.
So why am I getting a segmentation fault?
 

spinnaker

Joined Oct 29, 2009
7,830
1. You now know what causes that error now (in general).

2. You have the code.

3. You should have a debugger.

You have everything you need. Just step through the code and watch where your code is going out of bounds accessing memory.

More than likely it is characterArrayLength. You need to allocate memory. You can't do it like this characterArrayLength[];. That is an array with no memory allocated.


If you want to be a programmer then you need to become familiar with debuggers.
 

Thread Starter

kurtruk

Joined Aug 26, 2012
140
More than likely it is characterArrayLength. You need to allocate memory. You can't do it like this characterArrayLength[];. That is an array with no memory allocated.

Thanks SO MUCH, that worked.
 
Top