1

I've created a function that allows an element to be deleted from my malloc(ed) array (a list of struct triangle coordinates). That works fine. But when I tried to create one that adds an element (after position i) it just won't work...

For example, if I had an array of [0] [1] [2] [3] [4] [5] and wanted to add a new value after [3].

Here is my code: (this is a snippet of inside my while loop)

totaltri++;
addtriangle(trilist, totaltri, i);
i++;
secondtriangle(p, &t1, &t2, &t3, v, i);
i++;

My code deletetriangle(....):

for (int c = i; c < totaltri; c++)
{
     trilist[c] = trilist[c+1];
}

tmp = (triangle *) realloc(trilist, (totaltri-1)*sizeof(triangle));

trilist = tmp;

And my code addtriangle(....): Which doesn't seem to work..!

tmp = (triangle *) realloc(trilist, (totaltri+1)*sizeof(triangle));

for (int c = totaltri; c > i; c--)
{
    trilist[c-1] = trilist[c];
}
trilist = tmp;

I then proceed to assign values to the second triangle that has just been added in this code:

    int p1, p2, p3;
    p1  = whereisthepoint(p, *t1, v);
    p2  = whereisthepoint(p, *t2, v);
    p3  = whereisthepoint(p, *t3, v);

    triptr=&(trilist[i]);
    triptr->corner=(point *) malloc(3*sizeof(point));
    pt1ptr=&(triptr->corner[0]);
    pt2ptr=&(triptr->corner[1]);
    pt3ptr=&(triptr->corner[2]);

        pt3ptr->x=t3->x; etc etc
wohxela
  • 35
  • 6

3 Answers3

1
  • You have to assign the value of previous element to the next element. Your assignment is reversed.
  • trilist is invalidated via realloc() so you should use tmp for shifting.
  • You have to assign the new value to add that.
  • Casting results of malloc() family is considered as a bad practice.

addtriangle(....): should be like this:

tmp = realloc(trilist, (totaltri+1)*sizeof(triangle));

for (int c = totaltri; c > i; c--)
{
    tmp[c] = tmp[c-1]; /* correctly shift the elements */
}
tmp[i] = new_value; /* assign new value (replace new_value with proper thing) */
trilist = tmp;
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
1

Assume you have a pointer p of type T declared as such: T *p; and you also have an object that you want to add declared as T x;

Then just do like this:

T *p = malloc(sizeof *p * size);
T x;

...

// Reallocate to make room for one more element
T *tmp = realloc(p, sizeof *p * (size + 1));

// Error checking
if(!tmp) { /* Handle error */ }

p = tmp;

// Move the elements after the insertion spot
memmove(&p[index+1], &p[index], sizeof *p * (size - index));

// And insert
p[index] = x;

If you don't care about recovering on an allocation error, you don't need a temporary variable. Then you can just do:

p = realloc(p, sizeof *p * (size + 1));

if(!p) exit(EXIT_FAILURE);
klutt
  • 30,332
  • 17
  • 55
  • 95
0
tmp = (triangle *) realloc(trilist, (totaltri+1)*sizeof(triangle));

for (int c = totaltri; c > i; c--)
{
    trilist[c-1] = trilist[c];

in your code you modify (if realloc was successful) the invalid pointer. trilist may not point to the valid memory location. You should use tmp instead or assign trilist with tmp.

Universal function to add to an array.

void *addToArr(void *array, size_t arraysize, size_t pos, size_t elemsize, void *element)
{
    unsigned char *ucarray;
    array = realloc(array, (arraysize + 1) * elemsize);
    if(array)
    {
        ucarray = array;
        memmove(ucarray + (pos + 1) * elemsize, ucarray + pos * elemsize, (arraysize - pos) * elemsize);
        memcpy(ucarray + pos * elemsize, element, elemsize);
    }
    return array;
}
0___________
  • 60,014
  • 4
  • 34
  • 74