I have two pointers and I want to fill the pointer somme
with the values contained in the pointer v
.
This is method:
somme[0]=v[0] + v[1];
somme[1]=v[2] + v[3];
somme[2]=v[4] + v[5];
...
The error occurs when it performs the distruggi_vec(somme);
function and not distruggi_vec(v);
.
do you have any ideas? Thank you for your time.
This is my c code:
#include <stdlib.h>
#include <stdint.h>
extern uint32_t *crea_vec(size_t n)
{
uint32_t *p;
p = malloc(n * sizeof(uint32_t));
for (size_t i = 0; i < n; ++i)
{
p[i] = i;
}
return p;
}
uint32_t *somme_2a2(uint32_t *vett, size_t size)
{
if (size % 2 != 0)
{
size = size - 1;
}
size_t j = 0;
for (size_t i = 0; i < size; ++i)
{
if (i >= 10) { goto a; }
j = i * 2;
vett[i] = vett[j] + vett[j + 1];
}
a:
size = size / 2;
vett = realloc(vett, size * sizeof(uint32_t));
return vett;
}
extern void distruggi_vec(uint32_t *p)
{
free(p);
}
int main(void)
{
size_t n = 20;
uint32_t *v = crea_vec(n);
uint32_t *somme = somme_2a2(v, n);
distruggi_vec(v);
distruggi_vec(somme);
return 0;
}
when I debug my code it gives me this problem:
Thank you all for your time, this is the final solution of my problem without errors:
Code:
`
`