ok... now I use 2 functions for the allocation of memori:
-
void resize(int *array, int *d, int dim)
-
{ if(dim>MAXDIM){printf("raggiunta dim max"); exit(2);}
-
-
array=realloc(array,(dim)*sizeof(int));
-
if(array==NULL) {printf("errore: impossibile allocare altra memoria"); exit(2);}
-
if(d==NULL){printf("errore: '*' su puntatore a NULL"); exit(2);}
-
*d=dim;
-
-
}
-
-
elemento inizializza (int dim)
-
{int i;elemento a;
-
if(dim>MAXDIM){printf("raggiunta dim max"); exit(2);}
-
a.array=malloc(dim*sizeof(int));
-
if(a.array==NULL) {printf("errore: impossibile allocare altra memoria"); exit(2);}
-
a.array[0]=1;
-
for(i=1;i<dim;i++)a.array[i]=0;
-
a.d=dim;
-
a.succ=NULL;
-
a.n=1;
-
return a;
-
}
-
And remember the type elemento:
-
typedef struct el {int *array; int n; int d; struct el *succ;} elemento;
-
when I use this in the function:
-
elemento DecToBin(elemento a)
-
{elemento decimale,tmp1,resto, binario,zero;
-
int i,j,tmp;
-
-
zero=inizializza(2);
-
zero.array[1]=0;
-
zero.n++;
-
-
decimale=inizializza(a.d);
-
decimale.n=a.n;
-
binario=inizializza(2);
-
resto=inizializza(2);
-
tmp1=decimale;
-
-
for(i=0;i<decimale.n;i++) decimale.array[i]=a.array[i];
-
while(maggiore(decimale,zero)==0)
-
{decimale=DivSemplice(tmp1,1,2);
-
binario.array[binario.n]=tmp1.array[tmp1.n-1]%2;
-
tmp1=decimale;
-
decimale=tmp1;
-
binario.n++;
-
if(binario.n==binario.d) resize(binario.array,&(binario.d), 2*binario.n);
-
-
}
-
-
for(i=1, j=binario.n-1;i<j;i++,j--) {tmp=binario.array[j];binario.array[j]=binario.array[i];binario.array[i]=tmp;}
-
if(decimale.array[0]==1)binario.array[0]=1;
-
else binario.array[0]=-1;
-
I get:
*** glibc detected *** /home/richard/progettino/calc: realloc(): invalid next size: 0x088c90e0 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7f46604]
/lib/tls/i686/cmov/libc.so.6[0xb7f4a1b1]
/lib/tls/i686/cmov/libc.so.6(realloc+0x106)[0xb7f4aee6]
/home/richard/progettino/calc[0x8048c2f]
/home/richard/progettino/calc[0x804b8b5]
/home/richard/progettino/calc[0x8048831]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe5)[0xb7eed775]
/home/richard/progettino/calc[0x80484a1]
======= Memory map: ========
08048000-0804d000 r-xp 00000000 08:01 5105080 /home/richard/progettino/calc
0804d000-0804e000 r--p 00004000 08:01 5105080 /home/richard/progettino/calc
0804e000-0804f000 rw-p 00005000 08:01 5105080 /home/richard/progettino/calc
088c9000-088ea000 rw-p 088c9000 00:00 0 [heap]
b7d00000-b7d21000 rw-p b7d00000 00:00 0
b7d21000-b7e00000 ---p b7d21000 00:00 0
b7ed6000-b7ed7000 rw-p b7ed6000 00:00 0
b7ed7000-b8033000 r-xp 00000000 08:01 2786520 /lib/tls/i686/cmov/libc-2.9.so
b8033000-b8034000 ---p 0015c000 08:01 2786520 /lib/tls/i686/cmov/libc-2.9.so
b8034000-b8036000 r--p 0015c000 08:01 2786520 /lib/tls/i686/cmov/libc-2.9.so
b8036000-b8037000 rw-p 0015e000 08:01 2786520 /lib/tls/i686/cmov/libc-2.9.so
b8037000-b803a000 rw-p b8037000 00:00 0
b803c000-b8049000 r-xp 00000000 08:01 2768961 /lib/libgcc_s.so.1
b8049000-b804a000 r--p 0000c000 08:01 2768961 /lib/libgcc_s.so.1
b804a000-b804b000 rw-p 0000d000 08:01 2768961 /lib/libgcc_s.so.1
b804b000-b804f000 rw-p b804b000 00:00 0
b804f000-b8050000 r-xp b804f000 00:00 0 [vdso]
b8050000-b806c000 r-xp 00000000 08:01 2768919 /lib/ld-2.9.so
b806c000-b806d000 r--p 0001b000 08:01 2768919 /lib/ld-2.9.so
b806d000-b806e000 rw-p 0001c000 08:01 2768919 /lib/ld-2.9.so
bfd58000-bfd6d000 rw-p bffeb000 00:00 0 [stack]
Program received signal SIGABRT, Aborted.
0xb804f430 in __kernel_vsyscall ()
the program is compiled with gcc -Wall --pedantic and for the run time I used gdb... Please save me:(