473,513 Members | 2,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Access Violation with segmentation errors"!

57 New Member
Hi,
I have just spent many hours debugging my code and now when I run it I get the following error:

"An access violation (Segmentation fault) raised in your program"

I have researched on this and found that it is usually the result of faulty pointers or memory that is not allocated. However I have allocated all of my memory corectly (I feel) and my pointers look to be correct.. Is there another reason my code could be causing this error? My code is below:

Expand|Select|Wrap|Line Numbers
  1. /**-----------------------------------------------------------------------------
  2.  *                                                                              
  3.  * Title: Matrix Check
  4.  *
  5.  * File: MatrixCheck.c
  6.  *
  7.  * Author: Andrew Bullen (30562147)
  8.  *
  9.  * Date: 1st October 2007
  10.  *
  11.  * Program Function: Reads in data on a boolean matrix (/s) and outputs
  12.  *         whether the file has parity, is corrupt or the coordinates of the bit
  13.  *         we can change to give it parity.
  14.  *
  15.  -----------------------------------------------------------------------------*/
  16.  
  17.  
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <malloc.h>
  22.  
  23.  
  24.  
  25. /*----------------------------------------------------------------------------*/
  26.  
  27. // Sets values in place of booleans.
  28. #define TRUE 1
  29. #define FALSE 0
  30.  
  31. // Numbers of uneven rows / columns in a matrix for the properties.
  32. #define PARITY 1
  33. #define CHANGE 2
  34. #define CORRUPT 3
  35. #define INIT 0
  36. #define FILENAMEL 100
  37.  
  38.  
  39.  
  40. /*----------------------------------------------------------------------------*/
  41.  
  42.  
  43. FILE *readMatrix(FILE *fp, int **matrixPointer, int rows, int columns, int end);
  44. int **setMem(int **matrixPointer, int rows, int columns);
  45. int testRows(int **matrixPointer, int rowLocation, int rows, int columns);
  46. int testColumns(int **matrixPointer, int columnsLocation, int rows, int columns);
  47. void outputResults(int rowUnevens, int columnUnevens, int columnLocation, int rowLocation);
  48. void freeMem(int **matrixPointer, int rows, int columns);
  49.  
  50.  
  51. /*----------------------------------------------------------------------------*/
  52.  
  53.  
  54. int main(int argc, char **argv)
  55. {
  56.     int matrixNumber = INIT;
  57.     int rows = INIT;
  58.     int columns = INIT;
  59.     int rowLocation = INIT;
  60.     int columnLocation = INIT;
  61.  
  62.     int end = FALSE;
  63.  
  64.     FILE *fp = NULL;
  65.  
  66.     int **matrixPointer = NULL;
  67.  
  68.     do
  69.     {
  70.            matrixNumber++;
  71.  
  72.            fp = readMatrix(fp, matrixPointer, rows, columns, end);
  73.  
  74.            if (end != TRUE)
  75.            {
  76.                 int rowUnevens = testRows(matrixPointer, rowLocation, rows, columns);
  77.                 int columnUnevens = testColumns(matrixPointer, columnLocation, rows, columns);
  78.  
  79.                 printf("%d) \n", matrixNumber);
  80.                 outputResults(rowUnevens, columnUnevens, columnLocation, rowLocation);
  81.  
  82.            }           
  83.            freeMem(matrixPointer, rows, columns);
  84.  
  85.     }while(!feof(fp));
  86.  
  87.     return 0;   
  88. }
  89.  
  90.  
  91.  
  92. /*----------------------------------------------------------------------------*/
  93.  
  94.  
  95.  
  96. FILE *readMatrix(FILE *fp, int **matrixPointer, int rows, int columns, int end)
  97. {
  98.      char fileName[FILENAMEL]; // Character array for file-name string.   
  99.      fp = fopen(fileName, "r"); // Opens file with given name in 'read-only'.
  100.      int index1 = INIT;
  101.      int index2 = INIT;
  102.  
  103.      if(!feof(fp))
  104.      {
  105.             end = FALSE;      
  106.  
  107.             /* Reads numbers from file for values of 'rows' and 'columns' on the first 
  108.                line of input. */
  109.                fscanf(fp, "%d", &rows);
  110.                fscanf(fp, "%d", &columns);
  111.  
  112.                 // Allocates the memory for the matrixPointer array.
  113.                 matrixPointer = setMem(matrixPointer, rows, columns);
  114.  
  115.                 for(index1 = INIT; index1 < rows; index1++)// Loops through rows.
  116.                 {
  117.                      for (index2 = INIT; index2 < columns; index2++) // Loops through columns.
  118.                      {
  119.                          /* Inputs value at that point of file and stores in appropriate
  120.                             point in the matrix array according to current index. */
  121.                         fscanf(fp, "%d", &matrixPointer[index1][index2]);
  122.                       }
  123.                 }
  124.      }
  125.      else
  126.      {
  127.          end = TRUE;
  128.      }
  129.  
  130.  return fp;   
  131. }
  132.  
  133.  
  134. /*----------------------------------------------------------------------------*/
  135.  
  136.  
  137.  
  138. int **setMem(int **matrixPointer, int rows, int columns)
  139. {
  140.     int index1 = INIT;
  141.  
  142.     // Allocate memory for each row.
  143.     *matrixPointer = (int *)calloc(rows, sizeof(int *));
  144.  
  145.     // Allocate memory for each columns within each row. 
  146.     for (index1 = INIT; index1 < rows; index1++)
  147.     {
  148.         *matrixPointer[index1] = (int)calloc(columns, sizeof(int));
  149.     }
  150.  
  151.     return matrixPointer;
  152. }
  153.  
  154.  
  155.  
  156. /*----------------------------------------------------------------------------*/
  157.  
  158.  
  159.  
  160. int testRows(int **matrixPointer, int rowLocation, int rows, int columns)
  161. {
  162.     int counterRows = INIT;
  163.     int unevens = INIT;
  164.     int index1 = INIT;
  165.     int index2 = INIT;
  166.  
  167.     for(index1 = INIT; index1 < rows; index1++)// Loops through rows.
  168.      {
  169.           counterRows = INIT; 
  170.  
  171.           for (index2 = INIT; index2 < columns; index2++) // Loops through columns.
  172.           {
  173.               //Adds values of pointer as a counter of 1s. 
  174.               counterRows += matrixPointer[index1][index2];
  175.           }
  176.           if ((counterRows % 2) != 0)// If counter is not evenly divisible by 2.
  177.           {
  178.               unevens++;  // Number of uneven rows count increases.   
  179.               rowLocation = index2; // Coordinate of changeBit is altered.        
  180.           }        
  181.      }
  182.  
  183.     return unevens;
  184. }
  185.  
  186.  
  187. /*----------------------------------------------------------------------------*/
  188.  
  189.  
  190. int testColumns(int **matrixPointer, int columnsLocation, int rows, int columns)
  191. {
  192.     int counterRows = INIT;
  193.     int unevens = INIT;
  194.     int index1 = INIT;
  195.     int index2 = INIT;
  196.     int columnLocation = INIT;
  197.  
  198.     for(index1 = INIT; index1 < rows; index1++)// Loops through rows.
  199.      {
  200.           counterRows = INIT; 
  201.  
  202.           for (index2 = INIT; index2 < columns; index2++) // Loops through columns.
  203.           {
  204.               //Adds values of pointer as a counter of 1s. 
  205.               counterRows += matrixPointer[index2][index1];
  206.           }
  207.           if ((counterRows % 2) != 0)// If counter is not evenly divisible by 2.
  208.           {
  209.               unevens++;  // Number of uneven rows count increases.
  210.               columnLocation = index2; // Coordinate of changeBit is altered.           
  211.           }        
  212.      }
  213.  
  214.     return unevens;
  215. }
  216.  
  217.  
  218.  
  219. /*----------------------------------------------------------------------------*/
  220.  
  221.  
  222.  
  223. void outputResults(int rowUnevens, int columnUnevens, int columnLocation, int rowLocation)
  224. {
  225.      // If there are no rows/columns with uneven numbers.
  226.      if((rowUnevens == PARITY) && (columnUnevens == PARITY))
  227.      {
  228.           printf("Matrix has parity property. \n\n");          
  229.      }        
  230.      else
  231.      {
  232.           /* If 1 row and column is uneven then it uses the coordinate for 
  233.              change-bit*/
  234.           if((rowUnevens == CHANGE) && (columnUnevens == CHANGE))
  235.           {
  236.                // Prints bit-change location to user from recorded values.
  237.                printf("Change bit (%d , %d). \n\n", rowLocation, columnLocation);       
  238.           }
  239.           else
  240.           {
  241.                printf("Matrix is corrupt. \n\n");
  242.           }
  243.      }             
  244.  
  245.      return;                 
  246. }
  247.  
  248.  
  249. /*----------------------------------------------------------------------------*/
  250.  
  251.  
  252. void freeMem(int **matrixPointer, int rows, int columns)
  253. {
  254.      int index1 = INIT;
  255.  
  256.      for (index1 = INIT; index1 < rows; index1++)// Loops through rows.
  257.      {
  258.          // Frees memory of each array in the matrix and sets to NULL.
  259.          free(matrixPointer[index1]);
  260.          matrixPointer[index1] = NULL; 
  261.      }
  262.      // Frees memory of the matrix.
  263.      free(matrixPointer);
  264.      matrixPointer = NULL;
  265.  
  266.      return;    
  267. }
  268.  
  269.  
  270. /*----------------------------------------------------------------------------*/
  271.  
  272.  
  273.  
when I debug it stops at the line:

if(!feof(fp))

if thats any help :-).

I will keep looking over it to try and find the problem but i definately NEED help pretty bad.
Oct 7 '07 #1
1 2610
sicarie
4,677 Recognized Expert Moderator Specialist
when I debug it stops at the line:

if(!feof(fp))
Did you check to make sure the open was successful before you tried to see if it was an EOF?
Oct 9 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2782
by: ATS | last post by:
ERR - WebClient.DownloadData returns "protocol violation" Please help, I'm getting a "protocol violation" from a site that is working perfectly fine when I issue WebClient.DownloadData as...
0
3519
by: ASP.Confused | last post by:
The old message looked a little stale, so I am re-posting it here. Anybody have any ideas of what I could do?!? The previous responses to this question are below. If you want to look at the...
4
1481
by: Toufik | last post by:
Hi, I've the folowing error "Concurency violation, The update command affected 0 records", when I execute the save command "DA.Update(DS, "tblName")" DA is a dataadapter linked to a...
1
2301
by: Yanping Zhang | last post by:
Here are more details about my codes, please help! The function declared in C: typedef void (WINAPI *PLEARNCALLBACKPROC) (unsigned int progress, unsigned int sigQuality, unsigned long...
3
9009
by: Scott McDermott | last post by:
I have an application that is making an HTTP request with HttpWebRequest.GetRequest. Unless I set 'httpWebRequest useUnsafeHeaderParsing="true"' in the web.config, I get a 'The server committed a...
2
2874
by: Curten | last post by:
Hi, I want to read data from a txt-file that looks like this: aaa 4 12.45 bbb 3 7.34 ccc 12 3.45 and store the data in an array of structures. The struct and array are defined...
23
3343
by: pbaldridge | last post by:
I'm just starting my first C++ class and have a very basic question. We are to write a program that is going to be using the pow10(int m) function and we are not to use the pow10 function from the...
6
2916
by: hadad.yaniv | last post by:
Hello, i am new to c++, i hav a vector of typed object: vector<Man*People; When i do a second pushback, even for the same object the program crash say: "An Access Violation (Segmentation...
3
3043
by: raghunadhs | last post by:
Hi all, i have developed a ".dll" in vc++, regarding to find out CRC32. and in my V.B 6.0 application i am calling that .dll. If i run my v.b application it is working means.. i am able to find...
0
7257
marktang
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,...
0
7157
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...
0
7521
tracyyun
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...
0
5682
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5084
isladogs
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...
0
4745
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...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.