-4

Net is a structure which contain

typedef struct net{
    int numele;
    struct net **e;
} net;

The following code raises the error:

realloc(): invalid pointer

The realloc is causing problems whenever it is accessed and gives the above error. I don't understand why. If you think there is no problem in this part of the code, let me know because the whole code is 800 line long so i don't think I can post it here.

void add(net *n, net *ne) {
    if(n->numele==0) {
        n->e = (net **)malloc(sizeof(net *));
        n->e[0] = ne;
        n->numele = 1;
    } else {
        n->e = (net **)realloc(n->e, (1 + n->numele)*sizeof(net *));
        n->e[n->numele] = ne;
        n->numele = n->numele + 1;
    }
}

The nets n and ne are already initialized somewhere else in the code, so you can assume that they will not be null.

Karthik Nayak
  • 731
  • 3
  • 14
Prakhar Ganesh
  • 117
  • 2
  • 8

1 Answers1

0

Code for realloc() seems to be OK. But if net *n is not initialized properly in first call of add(), ie numele != 0 && e != NULL. In such cases e might have some junk values and realloc() tries with invalid address. So that error can happen.

jjm
  • 431
  • 3
  • 19