Segmentation fault using realloc on large arrays in C

user120920

I am trying to implement a dynamic array - if the array is full and you add another point it will double the size of the array. The size of the array is denoted by len and the space left in the array is denoted by reserved. If I append 5650 points it works fine, but as soon as I go 5700 or more it gives me a segmentation fault. Any ideas as to what could be causing this?

int point_array_append( point_array_t* pa, point_t* p )
{
    if(pa->reserved==0)
    {
        if(!(realloc(pa->points, sizeof(point_t)*(pa->len * 2))))
            return 1;
        pa->reserved=pa->len;
    }
    pa->len++;
    pa->reserved--;
    pa->points[(pa->len)-1] = *p;
    return 0;
}
shuttle87

realloc will resize the array (if it can) and then it will return the pointer to the new address of your data. If it fails it will return a null pointer, you should check for that (which you did, which is good to see!). Additionally it is important to note that realloc can move the memory to a different location if it needs to. In this code if such a move were to happen you would be in trouble because you only keep track of where the original pointer to the data is. So if realloc moved the data somewhere else you'd be writing to somewhere you shouldn't be which is undefined behavior and this could cause the segfault you are seeing. Chances are that up until 5650 points no move was done by realloc but more than that amount triggered a move to a different pointer.

The fix is to use the pointer returned by realloc and make sure that you check that this pointer is not null before you do anything with it.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

C Segmentation fault using strtok

분류에서Dev

A* Implementation in C, Segmentation fault

분류에서Dev

Segmentation Fault - GNU C

분류에서Dev

Strcpy Segmentation Fault C

분류에서Dev

Pointing to a variable using pointers in c-segmentation fault

분류에서Dev

Segmentation fault in recursive Binary Search Algorithm in C

분류에서Dev

merge sort segmentation fault c++

분류에서Dev

C segmentation fault 11 on recv() function

분류에서Dev

Segmentation fault when trying to clone a function (in C)

분류에서Dev

Segmentation fault when signing a message using OpenSSL, SWIG, and Perl

분류에서Dev

Can't find where is the segmentation fault in C program

분류에서Dev

Segmentation Fault in C causing code to run sometimes but not other times

분류에서Dev

Pthread_create causing segmentation fault (C++, Kubutnu 15)

분류에서Dev

c++ unsigned char array allocation - segmentation fault

분류에서Dev

Segmentation fault in Assembly and string

분류에서Dev

Access Violation (Segmentation Fault)

분류에서Dev

Malloc to struct; segmentation fault

분류에서Dev

Segmentation Fault in hsearch

분류에서Dev

Segmentation Fault? Why?

분류에서Dev

Strange segmentation fault in code

분류에서Dev

Segmentation Fault on return statement

분류에서Dev

glGenBuffers crashing with Segmentation fault

분류에서Dev

Struct causing segmentation fault

분류에서Dev

Resetting Variable : Segmentation fault

분류에서Dev

Segmentation fault in sorting algorithm

분류에서Dev

Segmentation Fault While Sorting - Malloc

분류에서Dev

Fractional Knapsack Algorithm segmentation fault

분류에서Dev

Segmentation fault on reboot Ubuntu 12.04

분류에서Dev

python Segmentation fault (core dumped)