Problems whith argc and argv

So Im working on Ubuntu and I was trying to make this program (sorry for using spanish variables ).
It gives you the biggest number of an int array and here is the code:

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <ctime>
#include <cstdlib>
#include <cctype>
#include <random>  
#include <chrono>  

using namespace std;
	
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

class GeneradorAleatorioEnteros
{  
private:
	
   mt19937 generador_mersenne;    // Mersenne twister
   uniform_int_distribution<int>  distribucion_uniforme;
   
   /************************************************************************/
   long long Nanosec(){
      return (chrono::high_resolution_clock::now().
              time_since_epoch().count());
   }
   /************************************************************************/ 

public:
	
   /************************************************************************/	
   GeneradorAleatorioEnteros()
      :GeneradorAleatorioEnteros(0, 1){
   }
   
   /************************************************************************/  
   GeneradorAleatorioEnteros(int min, int max) {
      const int A_DESCARTAR = 70000;
      // ACM TOMS Volume 32 Issue 1, March 2006
      
      auto semilla = Nanosec();
      generador_mersenne.seed(semilla);
      generador_mersenne.discard(A_DESCARTAR);
      distribucion_uniforme = uniform_int_distribution<int> (min, max);
   }
   
   /************************************************************************/
   int Siguiente(){
      return (distribucion_uniforme(generador_mersenne));
   }
   /************************************************************************/
};

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

int * PosMayor (int *pv, int dcha = 99, int izda = 0){

	
	int * p = pv + izda;
	int *pos_max;
	int max = 0;
	// 	Bucle en el que avanzo valores mientras sean dígitos.

	for (;p < pv + dcha; p++){
 	
		if (*p > max){

			max = *p;
			pos_max = p;			
		
		}

	}

	return (pos_max);
}

void RellenaEntero (int * pv, int dcha = 99, int izda = 0){
	
	int limite_superior = 500;
	int limite_inferior = 0;
	int * p = pv;
	GeneradorAleatorioEnteros generador_aleatorio(limite_inferior, limite_superior);
	
	
	while (p < pv + dcha){
		*p = generador_aleatorio.Siguiente();
		p++;
	}
}

int main(int argc, char ** argv){
	
	const int TOPE = 100;
	int vp[TOPE];	
	int * max;	
	int total, pos_max, pos_min;

	

	if (argc == 1){

		RellenaEntero (vp);
		max = PosMayor(vp);

	}		

	if (argc == 2){

		total = atoi(argv [1]);
		RellenaEntero (vp, total);
		max = PosMayor(vp, total);

	}

	if (argc == 3){

		total = atoi(argv[1]);
		pos_min = atoi(argv[2]);
		RellenaEntero (vp, total);
		max = PosMayor(vp, total, pos_min);

	}
	
	if (argc == 4){
	
		total = atoi(argv[1]);
		pos_min = atoi(argv[2]);
		pos_max = atoi(argv[3]);
		RellenaEntero (vp, total);
		max = PosMayor(vp,pos_max,pos_min);

	}		
	
	if (argc > 4)
		cout << "\tNumero maximo de argumetos sobrepasado";


	cout << "\tEl maximo es: " << *max;

}


The thing is that I don know if the error comes frome the use on "argc" and "argv" because this is my first program using it, thanks.
It compiles well but when I execute, here is the error:

alberto@alberto-HP-Pavilion-Notebook:~/Escritorio/MP$ bin/I_PosMayor 22
Violación de segmento (`core' generado)

Violation of the segment ('core' generated)

Thanks and sorry for my english
Last edited on
In main(), vp has 100 elements. However, the user input can cause RellenaEntero() to breach that limit.

It might be better to do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main(int argc, char ** argv) {
	int * vp = nullptr;	
	int * max;	
	int total, pos_max, pos_min;

	if (argc == 1) {
		const int TOPE = 100;
		vp = new int[TOPE];
		RellenaEntero (vp);
		max = PosMayor(vp);
	}
	else if (argc == 2) {
		int total = atoi(argv [1]);
		vp = new int[total];
		RellenaEntero (vp, total);
		max = PosMayor(vp, total);
	}
//... 
Last edited on
Thanks. Despite your answer is usefull I don think that could be the cause of my error.
I mean, without arg:

alberto@alberto-HP-Pavilion-Notebook:~/Escritorio/MP$ bin/I_PosMayor El maximo es: 489

With 1 or mor args:

alberto@alberto-HP-Pavilion-Notebook:~/Escritorio/MP$ bin/I_PosMayor 222 22 222 22 22
Violación de segmento (`core' generado)
alberto@alberto-HP-Pavilion-Notebook:~/Escritorio/MP$ bin/I_PosMayor 222 22 222 22
Violación de segmento (`core' generado)
alberto@alberto-HP-Pavilion-Notebook:~/Escritorio/MP$ bin/I_PosMayor 222 22 22
Violación de segmento (`core' generado)

Thanks.

I can't reproduce your crash.
In your function PosMayor your pos_max isn't initialized.
1
2
3
4
5
if (*p > max) 
{
      max = *p;
      pos_max = p;
}

If the condition isn't true returning a pointer which might point into nirvana.

BTW. You use the new <random> library and then all this old pointer stuff.
Why don't you use a vector?
alberto@alberto-HP-Pavilion-Notebook:~/Escritorio/MP$ bin/I_PosMayor 222 22 222 22 22

Here argc==6. Remember that argc includes the program name itself, which is stored in argv[0]. The program prints the message at line 138, but the output is buffered so you don't see it yet. Then the program crashes at line 141 because max is uninitialized.

alberto@alberto-HP-Pavilion-Notebook:~/Escritorio/MP$ bin/I_PosMayor 222 22 222 22

argc == 5 so the behavior is the same as above.

alberto@alberto-HP-Pavilion-Notebook:~/Escritorio/MP$ bin/I_PosMayor 222 22 22

argc == 4. Lines 129-131 set total=222, pos_min=22 and pos_max=22. Line 132 calls RellenaEntero (vp, 222), which tries to store 222 values in an array that has space for just 100. The program crashes.
Last edited on
Topic archived. No new replies allowed.