Program compiles but does not run

Hi guys, i have program which implements the longest common subsequence algorithm. But from some reason, it is impossible to run. I am using Win7 and Dev-C++. I also tried Visual studio, where during debugging an error occured- Unhandled exception at 0x74e743f9 in Untitled1.exe: 0xC0000005: Access violation reading location 0x00000000. I am pretty sure that source code is ok, I dont know if have problems in some variables or what. BTW, when I tried to run Hello world program, it worked fine... Thanks a lot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdlib.h>

char* LCS(char* a, char* b);

#define NEITHER       0
#define UP            1
#define LEFT          2
#define UP_AND_LEFT   3

int main(int argc, char* argv[]) {
	printf("%s\n", LCS(argv[1],argv[2]));
}

char* LCS(char* a, char* b) {
	int n = strlen(a);
	int m = strlen(b);

	int** S;
	int** R;

	int ii;
	int jj;

	int pos;
	char* lcs;

	S = (int **)malloc( (n+1) * sizeof(int) );
	R = (int **)malloc( (n+1) * sizeof(int) );
	for(ii = 0; ii <= n; ++ii) {
		S[ii] = (int*) malloc( (m+1) * sizeof(int) );
		R[ii] = (int*) malloc( (m+1) * sizeof(int) );
	}

	for(ii = 0; ii <= n; ++ii) {
		S[ii][0] = 0;
		R[ii][0] = UP;
	}
	for(jj = 0; jj <= m; ++jj) {
		S[0][jj] = 0;
		R[0][jj] = LEFT;
	}

	for(ii = 1; ii <= n; ++ii) {
		for(jj = 1; jj <= m; ++jj) { 

			if( a[ii-1] == b[jj-1] ) {
				S[ii][jj] = S[ii-1][jj-1] + 1;
				R[ii][jj] = UP_AND_LEFT;
			}

			else {
				S[ii][jj] = S[ii-1][jj-1] + 0;
				R[ii][jj] = NEITHER;
			}

			if( S[ii-1][jj] >= S[ii][jj] ) {	
				S[ii][jj] = S[ii-1][jj];
				R[ii][jj] = UP;
			}

			if( S[ii][jj-1] >= S[ii][jj] ) {
				S[ii][jj] = S[ii][jj-1];
				R[ii][jj] = LEFT;
			}
		}
	}

	ii = n; 
	jj = m;
	pos = S[ii][jj];
	lcs = (char *) malloc( (pos+1) * sizeof(char) );

	lcs[pos--] = (char)NULL;

	while( ii > 0 || jj > 0 ) {
		if( R[ii][jj] == UP_AND_LEFT ) {
			ii--;
			jj--;
			lcs[pos--] = a[ii];
		}
	
		else if( R[ii][jj] == UP ) {
			ii--;
		}

		else if( R[ii][jj] == LEFT ) {
			jj--;
		}
	}

	for(ii = 0; ii <= n; ++ii ) {
		free(S[ii]);
		free(R[ii]);
	}
	free(S);
	free(R);
	getchar();

	return lcs;
	
}
If you rnu this program without entering some command line parameters, here
int n = strlen(a);
a is a null pointer, because argv[1] is a null pointer.

Did you remember to include arguments?

When you call the excecutable, you have to do so from the console (not just clicking on the .exe) and you need to add at least 2 arguments for this program. If I do this by clicking on the exe I get the same error.

In this scenario, argv[1] and argv[2] do not exist! Your program is crashing at line 18 as it tries to find argv[1].
Last edited on
@Moschops
I've been Ninja'd again! You always do that to me.
Niiinnn..... JJAAAAAAAAAAAAR!
Shit, of course I forgot to include argumetns :/ thanks a lot guys, now it works....
Topic archived. No new replies allowed.