I recently started immersing myself in this world of programming. Clearly, I'm taking my first steps. Now, I was trying to make a program that solves systems of equations, but in data entry I already have a problem whose cause I cannot identify. I would be quite happy to find an answer.
The idea of data entry is to identify how many equations the system has and, subsequently, collect each one in a string (array) of characters. Then another function takes care of evaluating each character in the string and deciding if it is a coefficient, a variable, or a sign. It then stores this data in a structure that is created for each equation. The function that gives me problems is the one that collects the character string, because when I execute the program it receives up to the sixth character and ends due to some error:
enter the code here
typedef struct ecua
{ /*cabecera del programa*/
int coefi[8];
char varia[8]; /*estructura para cada ecuacion*/
char sig[8];
}ecuacion;
void entrada(char**ecu);
int con1;
int con2;
int n;
int main() /* en la funcion main, declaro y defino punteros para trabajar con memoria dinamica*/
{
printf("Por favor, indique cuantas ecuaciones hay en el sistema a solucionar\n");
scanf("%d",&con1);
ecuacion *pun;
char *ec;
pun=(struct ecua*)malloc(sizeof(int)*8+sizeof(char)*16);
for(con2=1;con2<=con1;con2++)
{
if(con2>1)
{
pun=(struct ecua*)realloc(pun,(sizeof(int)*8+sizeof(char)*16));
} /*asigna memoria para la clasificación que se hará posteriormente en otra función*/
entrada(&ec);
}
return 0;
}
void entrada(char**ecu) /*no se cual es el error en esta funcion :( */
{
n=0;
*ecu=(char*)malloc(sizeof(char));
printf("introduzca la ecuacion %d. Para terminar, pulsa enter\n",con2);
*ecu[0]=getche();
while(*ecu[n]!=(13)) /*Mientras el caracter no sea enter,*/
{
n++;
*ecu=(char*)realloc(*ecu,sizeof(char));
*ecu[n]=getche();
}
return;
}
Let's take a closer look at the function
entrada
:There are two obvious flaws:
1.- That is not the correct way to use the realloc function , because, in each iteration, you pass the same size (in this case a byte), therefore, you are not resizing the array!
The second parameter of realloc expects a new size in bytes . At first the array consumes 1 byte, then on resizing you must pass 2 bytes to it, then on the third iteration you must pass 3 bytes and so on.
So to the second parameter you should pass this:
A plus one is added to
n
because you have to leave an extra space for the null character, since it indicates the end of the string.2.- This statement causes a segmentation fault :
The above statement is equivalent to:
Now, the question is, what if
n = 1
? We would simply be accessing a memory address that does not belong to the program and this causes the OS to kill the process. This is all because this expression is used to access X position of an array of pointers , but this is not the case, since the double pointer (ecu
) can only access one pointer.3. You don't need the getche function ! It is not standard! Use the getchar function which is standard.
The getchar function reads one character at a time from the keyboard buffer.
The function
entrada
would look like this if we follow the mentioned corrections:How to use:
Another problem is the code you have in the function
main
, you have complicated yourself too much and the way you calculate the size in bytes I still don't understand why you did it that way.All you have to do is create an array of
N
element structures. So yes itN = 2
is because you will have two equations.In code it would look like this:
Observation:
1.- Do not abuse global variables. Look at this thread .
2.- Don't forget to free the memory, otherwise there will be a memory leak.