I'm receiving sigtrap when I'm trying to reallocate a custom array.

I have customized the following structure:
1
2
3
4
5
6
7
8
9
10
11
12
typedef struct
{
    char *name, number[8];
    float *c;
} car;

typedef struct
{
    int na, n, nc;
    car *m;
    float *car;
} park;


Basically, the park (*p) is a list of cars that possess p->na possible spaces, p->n cars and p->nc characteristics for every car. p->car is the list of characteristics and for every car we have a name, a number (license plate number), and the address in the p->car array where the respective car's characteristics are located.
And I want to practice the realloc function.
Those may prove irrelevant, however, when I'm trying to add another car over the park limit my code begins like that:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int i=p->n, t=p->na, s=0, j;
    char c[100], d[8];
    car *aux;
    float *ax;
    while(fscanf(f, "%s%s", c, d)==2)
    {
        if(i>t)
            {
                aux=(car *)realloc(p->m, sizeof(car)*i);
                ax=(float *)realloc(p->car, sizeof(float)*i);
                if(!aux || !ax)
                {
                    printf("Additional allocation failed\n");
                    return NULL;
                }


The code continues but the issue lies there:
basically, when the number of cars is bigger than the number of spaces (p->n > p->na, or i>t) then the reallocation of the p->m array is attempted. Unfortunately, when I try that, at the
aux=(car *)realloc(p->m, sizeof(car)*i);
line (the 9th from the posted code),
I receive the following message (in the debugger)
At C:\Users\Test\Workspace\Cars\func.c:203
Program received signal SIGTRAP, Trace/breakpoint trap.
In ntdll!RtlpSetUserPreferredUILanguages () (C:\Windows\system32\ntdll.dll)
Debugger finished with status 0


I was told (from this site) that when realloc fails it returns NULL. However, after passing this line, the program simply crashes. Can you tell me what's wrong please?
Last edited on
Maybe 'p->m' or 'p->car' points to invalid memory (uninitialized)?
The thing is, that p->m and p->car already contain data. If I try to work with it in the initial parameters, they work without a hitch. I can display them, juggle with them, anything. The problem comes when I try to reallocate an extra space for an extra entry (assuming that the initial size is overloaded).

I was told that this is how to do it. Assign to back-up variable the result from the realloc function and then if it isn't NULL I give its value to the initial variable.

In conclusion, p->m and p->car are initialized and they already contain data. And I can add data to them until they are filled. However the realloc function (or something else for that matter) crashes the execution of my program. Can you please tell me what might be wrong?
so you mean that after line 15 you're doing something like this:
1
2
p->m=aux;
p->car=ax;
That'll be ok. After successful reallocation 'p->m' and 'p->car' are free'd and the pointer become invalid.

The original values must have been allocated with malloc (and alike).

Maybe the problem is fscanf(). Are you sure that the first string length never exceeds 99 and the second never exceeds 7? At least the second seems to be a very small value.

Furthermore are you sure that you access 'p->m' and 'p->car' within the limits?

the code otherwise seems to be ok so far
Yes, you're right. The else branch of (!aux || !ax) does that (it also raises the current number of cars and the total number of cars). The problem does not lie in fscanf. First of all, because I am very sure the strings do not exceed 99 and 7 characters (it's hard to think of a car name of 99 characters, the whole input file doesn't have that many characters, and the car number follows the same format, 7 characters).

Second of all, I followed the code. I entered debugging mode (in code::blocks) and clicked the step into button until I reached the printf line and there was absolutely no problem there. The problem arised when I reached the line with aux=(car *)realloc(p->m, sizeof(car)*i);. It didn't even pass to the next realloc function, it just froze there, the yellow cursor dissapears and every time I click the "step into button" it gives me text like that:
In ntdll!RtlpSetUserPreferredUILanguages () (C:\Windows\system32\ntdll.dll)
In ntdll!RtlInitUnicodeString () (C:\Windows\system32\ntdll.dll)
In ntdll!RtlTraceDatabaseValidate () (C:\Windows\system32\ntdll.dll)
In ntdll!RtlpSetUserPreferredUILanguages () (C:\Windows\system32\ntdll.dll)
In ntdll!RtlTraceDatabaseValidate () (C:\Windows\system32\ntdll.dll)
In ntdll!RtlGetExtendedFeaturesMask () (C:\Windows\system32\ntdll.dll)
In ntdll!RtlpSetUserPreferredUILanguages () (C:\Windows\system32\ntdll.dll)
In ntdll!RtlEqualSid () (C:\Windows\system32\ntdll.dll)
and so on.

I suppose I won't access p->m and p->car within the limits since I'm trying to reallocate limits to them.
Last edited on
Topic archived. No new replies allowed.