# Thread: Pointer becomes NULL inside loop

1. ## Pointer becomes NULL inside loop

Hi all,

I'm having a problem with a pointer struct to pointer passed in a function...
We'll, better show first.

Code:
```void defCompraBuscaLocal ( int * vCOMP, Instancia * I, Solucao * S, VetDeListas * K, int prod )
{
Noh * p = 0;
int i=0, aux=0, aux_DEM=0, qtmcd=0, qtdCOMP=0;

aux_DEM = I->DEM[prod];
qtmcd = I->merc;

for(i=0; i<qtmcd; i++) {
assert(S->binMcd != 0);
vCOMP[i] = 0;
}

p = (K->VL[prod])->cab;
while ( p != 0 )
{
if ( S->binMcd[p->dado] > -1 )
{
if ( aux_DEM > 0 )
{
aux = detMinimo(aux_DEM, I->DISPB[prod*qtmcd+p->dado]);
vCOMP[p->dado] = aux;
aux_DEM = aux_DEM - aux;
}
else
{
qtdCOMP = somaElemVetor(vCOMP,qtmcd);
vCOMP[0] = qtdCOMP;
return;
}
}	/*if ( S->binMcd[p->dado] == 1 )*/
p = p->prox;
}	/*while ( p != 0 )*/

qtdCOMP = somaElemVetor(vCOMP,qtmcd);
vCOMP[0] = qtdCOMP;

return;
}```
I hope this amount of code will suffice to explain my point: The assertion fails after some loops in the 9th line
Code:
```for(i=0; i<qtmcd; i++) {
assert(S->binMcd != 0);
vCOMP[i] = 0;
}```
The struct S is

Code:
```typedef struct Solucao
{
int fo_total;
int fo_comp;
int fo_transp;
_Bool fac;
int * binMcd;
Lista * sol;
int * COMP;
} Solucao;```
Running GDB I get

Code:
```Breakpoint 1, defCompraBuscaLocal (vCOMP=0x61e010, I=0x609910, S=0x61e030, K=0x6096d0, prod=6)
at heur_const_atpp.c:148
148		Noh * p = 0;
(gdb) break 155
Ponto de parada 2 at 0x403b94: file heur_const_atpp.c, line 155.
(gdb) continue
Continuing.

Breakpoint 2, defCompraBuscaLocal (vCOMP=0x61e010, I=0x609910, S=0x61e030, K=0x6096d0, prod=6)
at heur_const_atpp.c:155
155		for(i=0; i<qtmcd; i++) {
(gdb) print S->binMcd
\$1 = (int *) 0x6207a0
(gdb) print S->COMP
\$2 = (int *) 0x61e080
(gdb) print *S->binMcd
\$3 = 13
(gdb) next
156		  assert(S->binMcd != 0);
(gdb)
157			vCOMP[i] = 0;
(gdb)
155		for(i=0; i<qtmcd; i++) {
(gdb)
156		  assert(S->binMcd != 0);
(gdb)
157			vCOMP[i] = 0;
(gdb)
155		for(i=0; i<qtmcd; i++) {
(gdb)
156		  assert(S->binMcd != 0);
(gdb)
157			vCOMP[i] = 0;
(gdb) continue
Continuing.
heur_const_atpp: heur_const_atpp.c:156: defCompraBuscaLocal: Assertion `S->binMcd != 0' failed.
Rodando ../Clase5-SymCapTPP/CapEuclideo.50.50.1.1.tpp
Program received signal SIGABRT, Aborted.
0x00007ffff780aa75 in *__GI_raise (sig=<value optimized out>) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
64	../nptl/sysdeps/unix/sysv/linux/raise.c: Arquivo ou diretório não encontrado.
in ../nptl/sysdeps/unix/sysv/linux/raise.c```
Translation of the last part 'Arquivo ou diretório não encontrado'='file or directory not found'.

I did some checking and found out that S is not NULL after the assertion, but S->COMP and S->binMcd are.

If anyone can point a direction I would be very thankful.

Thanks a lot,
Abel

2. Thank you for the effort, and sorry to waste your time. I found what happened (though I haven't found where it happen yet).

The vector vCOMP was pointing to right before where S where pointing to. When I did the for, it set all vector in S to 0, hence NULLifying them.

Since the vector vCOMP was allocated before, I don't know what is going wrong. But knowing the error is very useful.

Thank you everyone, this thread can be closed.

Abel

Popular pages Recent additions