Code crashes on c, most likely something to do with malloc? - c

this is quite basic but I've been trying to solve it for a few hours now with no success. This code is supposed to read 3 ints from user input and then, points according to the ints (n = how many, d = dimension, k is unrelated to this part) and create an array of them. For some reason it crashes on the second loop, where it fails to create the second point, but im not sure why. I think it might has something to do with malloc but I'm totally lost, would appreciate any help.
I inserted the input :
5 3 2
1.2 3.4 0.1
2 times before it crashed.
Code is below:
int main(){
double* pdata;
int n,d,k;
scanf("%d %d %d",&n,&d,&k );
SPPoint* parr [n];
for(int i=0; i<n; i++)
{
double darr [d];
for(int j = 0; j < d-1; j++)
{
scanf(" %lf", &darr[j]);
}
scanf(" %lf", &darr[d-1]);
pdata = darr;
parr[i] = spPointCreate(pdata, d, i);
}
}
This is the code for the spPointCreate function:
struct sp_point_t{
double* data;
int dim;
int index;
};
SPPoint* spPointCreate(double* data, int dim, int index){
SPPoint* point = malloc(sizeof(SPPoint*));
if(point == NULL)
{
return NULL;
}
point->data = (double*) malloc(sizeof(data));
for( int i=0 ; i<dim ; i++)
{
point->data[i] = data[i];
}
point->dim = dim;
point->index = index;
return point;
}

SPPoint* point = malloc(sizeof(SPPoint*));
should be: struct SPPoint* point = malloc(sizeof(*point));
point->data = (double*) malloc(sizeof(data));
should be point->data = malloc(dim * sizeof(*point->data));
since you want to allocate dim doubles for your point.

Code is mis-allocating in 2 places
// Bad
SPPoint* spPointCreate(double* data, int dim, int index){
SPPoint* point = malloc(sizeof(SPPoint*)); // allocate the size of a pointer
...
point->data = (double*) malloc(sizeof(data)); // allocate the size of a pointer
Instead avoid mis-coding the type and allocate to the the size of the de-referenced variable.
Also need to allocate N objects.
SPPoint* spPointCreate(double* data, int dim, int index){
size_t N = 1;
SPPoint* point = malloc(sizeof *point * N);// allocate the size of `*point` * N
...
assert(dim >= 0);
N = dim;
point->data = malloc(sizeof *(point->data) * N);
BTW, casting the result of malloc() not needed.
2nd allocation would benefit with a NULL check. More complicated as dim may be 0 and a malloc() return of NULL in that case is OK.
N = dim;
point->data = malloc(sizeof *(point->data) * N);
if (point->data == NULL && N > 0) {
free(point);
return NULL;
}

Related

Allocating dynamic memory using malloc in C

I'm new to C and have been trying to tackle this question. It's a continuation of the last thread I made. I made some progress but still have so much to learn and fix.
In short:
In this question a "vector" is a one dimensional array of integers. Therefore an array of vectors would be a two dimensional array that holds one dimensional arrays inside him.
I need to use these variables:
int** vectors- the 2D array
int size -an integer that represents how many vectors exist inside **vectors
int* sizes-a 1D array of integers that represents the length of the vectors
I need to write the following functions:
int init(int ***vectors, int **sizes, int size)
the function allocated memory to **vectors and *sizes with size and initializes vectors to be full of NULLs,and sizes to be full of zeros.
int set(int **vectors, int *sizes, int index, int *tmp, int tmp_size)
the function receives an array of nulls (**vectors)), frees the vector inside **vectors whose index is index and allocates memory for a new vector, whose length is tmp_size and places inside it *tmp's elements.
This is my code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int init(int*** vectors, int** sizes, int size)
{
int i, k,j;
*sizes = (int*)malloc(size * sizeof(int));
if (*sizes == NULL)
return 0;
for (j = 0; j < size; j++)
{
(*sizes)[j] = 0;
}
*vectors = (int**)malloc(size * sizeof(int*));
if (*vectors == NULL)
return 0;
for (i = 0; i < size; i++)
{
(vectors)[i] = NULL;
}
return 1;
}
int set(int **vectors, int *sizes, int index, int *tmp, int tmp_size)
{
if ((vectors)[index] != NULL)
{
free((vectors)[index]);
}
(vectors)[index] = (int*)malloc(tmp_size * sizeof(int));
if ((vectors)[index] == NULL)
return 0;
for (int b = 0; b < tmp_size; b++)
{
(vectors)[index][b] = tmp[b];
}
sizes[index] = tmp_size;
return 1;
}
int main()
{
int size, i, length, indexhere;
int** vectors = NULL;
int* sizes = NULL;
int* tmp = NULL;
int* p = &vectors;
int tempindex;
printf("\nPlease enter an amount of vectors:\n");
scanf("%d", &size);
init(p, &sizes, size);
printf("Enter index\n");
scanf("%d", &indexhere);
printf("Enter Length\n");
scanf("%d", &length);
tmp = (int*)malloc(length * sizeof(int));
printf("Enter elements:\n");
for (int g = 0; g < length; g++)
scanf("%d", &tmp[g]);
set(&vectors, sizes, indexhere, tmp, length);
system("pause");
return 0;
}
Could someone explain please why the program always crashes?
In init function (vectors)[i] = NULL; should actually be (*vectors)[i] = NULL;
When calling set function from main you should pass vectors instead of &vectors.
There also seems to be several pointer type mismatches in your code, so you should really pay attention to compiler's warnings. This is because C unfortunately allows implicit conversions between incompatible pointers, unlike C++ for example.
You call set like this
set(&vectors, sizes, indexhere, tmp, length);
but the first argument is declared as an int **. By passing &vector you're passing a pointer to vector, i.e. something of type int ***. This mismatch will lead to undefined behavior and probable crashes.
Here is a complete working example.
#include <stdio.h>
#include <stdlib.h>
void destroyVectors(int **vectors, int size)
{
for (int i = 0; i < size; i++)
{
free(vectors[i]);
}
}
int init(int*** vectors, int** sizes, int size)
{
int i, j;
*sizes = (int*)malloc(size * sizeof(int));
if (*sizes == NULL)
return 0;
for (j = 0; j < size; j++)
{
(*sizes)[j] = 0;
}
*vectors = (int**)malloc(size * sizeof(int*));
if (*vectors == NULL)
return 0;
for (i = 0; i < size; i++)
{
(*vectors)[i] = NULL;
}
return 1;
}
int set(int **vectors, int *sizes, int index, int *tmp, int tmp_size)
{
if ((vectors)[index] != NULL)
{
free((vectors)[index]);
}
(vectors)[index] = (int*)malloc(tmp_size * sizeof(int));
if ((vectors)[index] == NULL)
return 0;
for (int b = 0; b < tmp_size; b++)
{
(vectors)[index][b] = tmp[b];
}
sizes[index] = tmp_size;
return 1;
}
int main()
{
int size = 0, length = 0, indexhere = 0;
int** vectors = NULL;
int* sizes = NULL;
int* tmp = NULL;
printf("\nPlease enter an amount of vectors:\n");
scanf("%d", &size);
init(&vectors, &sizes, size);
printf("Enter index\n");
scanf("%d", &indexhere);
printf("Enter Length\n");
scanf("%d", &length);
tmp = (int*)malloc(length * sizeof(int));
printf("Enter elements:\n");
for (int g = 0; g < length; g++)
scanf("%d", &tmp[g]);
set(vectors, sizes, indexhere, tmp, length);
for(int i = 0; i < length; ++i)
printf("byte: %d\n", vectors[indexhere][i]);
printf("sizes index: %d\n", sizes[indexhere]);
free(tmp);
free(sizes);
destroyVectors(vectors, size);
return 0;
}

pointer of a pointer and memory allocation

If I have int **array and want to place a series of numbers in it (I don't know its size), 5 3 4 0 or 9 1 5 8 3 0 as an example. As far as I know I should be using malloc
So I did something like this
int **array;
int n = 1, inp = 0;
while(n){ // scan till the input is 0
scanf("%d", &n);
array = (int**)malloc(sizeof(int*)*(inp+1)); //since inp start at 0
array[inp] = &n; //is this even correct?
inp++;
}
My first question is: Will this method (the loop) upgrade/expand the size of the array or is what I am doing a waste of memory?
The second question is how can I print/edit the values of this array?
EDIT:
From your answers I have came up with the following.
int **array;
int n = 1, inp = 0;
array = (int**)malloc(sizeof(int*));
while(n){
scanf("%d", &n);
realloc( array, sizeof((int*)(inp+1)));
array[inp] = n;
inp++;
}
Is this the correct way to do it?
Note* I am aware that it does not have to be a pointer of a pointer, but I need it to be for something else later on.
You code is wrong for at least these reasons.
1) You keep doing malloc to array and thereby loose previously malloced blocks. The function to use is realloc when extending the size of dynamic memory.
2) You store the address of n instead of the value of n
Besides that it seems strange to use a double pointer. Why not do like:
int *array = NULL;
int n = 1, inp = 0;
while(n){ // scan till the input is 0
scanf("%d", &n);
array = realloc(array, sizeof(int)*(inp+1));
array[inp] = n;
inp++;
}
EDIT after OPs update
If you really want to use a double pointer (i.e. int **array;), you need to allocate memory in two levels.
That could look like:
int **array = malloc(sizeof *array);
*array = NULL;
int n = 1, inp = 0;
while(n){ // scan till the input is 0
scanf("%d", &n);
*array = realloc(*array, sizeof(int)*(inp+1));
(*array)[inp] = n;
inp++;
}
What you're doing in your code is allocating progressively larger areas of memory and saving the input value in the last position of each new area, while losing the pointers to the previously allocated areas. A commom and efficient solution for what you want (which is used in C++'s vectors, I believe) is to allocate some minimum amount of space, then check at each iteration if you are on the verge of exceeding it. In case you are, reallocate the area doubling the space. Something like this:
int i = 0; //iterator
int s = 1; //array size
int n; //input (use do-while so you don't have to worry about initial value)
//it doesn't have to be bidimensional for a single series
int * array = (int *) malloc(sizeof(int) * s);
do
{
if(i == s)
{
s *= 2;
array = (int *) realloc(array, sizeof(int) * s);
}
scanf("%d", &n);
array[i++] = n; //assign with the value, not with the address
}
while(n)
UPDATE: if you really need to use **int, do it like this:
int n, i = 0, s = 1;
int ** array = (int **) malloc(sizeof(int*) * s);
do
{
if(i == s)
{
s *= 2;
array = (int **) realloc(array, sizeof(int *) * s);
}
scanf("%d", &n);
array[i] = (int *) malloc(sizeof(int));
array[i][0] = n;
++i;
}
while(n)

Multiplication Table issue

I am trying to write a C function to store a multiplication table from the range 0-x. The function I have written has a bug in it where it only stores either the first 2 or 3 rows of the table and the function is also never returning a value and I am very confused, any help is appreciated.
void makeTable (int x) {
int** table = malloc(x * sizeof(int*));
int i;
int q;
int* ptr;
for(i = 0; i <= x; i++){
ptr = calloc(x, sizeof(int));
for(q = 0; q <= x; q++){
ptr[q] = (i * q);
}
table[i] = ptr;
}
return table;
}
Here you access memory that you have not allocated. That is undefined behavior.
Correct would be to consider x elements not x+1.
for(q = 0; q < x; q++){
ptr[q] = (i * q);
Same goes with the outer loop also.
for(i = 0; i < x; i++){
Also few other things - returning from a function which is declared void (Undefined behavior). Not checking the return value of malloc and calloc is problematic.
So correct way to write the function would be
int** makeTable (int x) {
int** table = malloc(x * sizeof *table);
if(!table)
{
fprintf(stderr,"Error in mem alloc");
exit(1);
}
for(size_t i = 0; i < x; i++){
int * ptr = calloc(x, sizeof *ptr);
if(!ptr)
{
fprintf(stderr,"Error in mem alloc");
exit(1);
}
for(size_t q = 0; q < x; q++){
ptr[q] = (i * q);
}
table[i] = ptr;
}
return table;
}
Also somewhere in this program you should add the logic to free all these dynamically allocated memory using free.

Increase dynamic array size with a function; free error, invalid next size (fast)

I need to create a dynamic array and put five number, after there is another input which is how much I have to increase the array size.
When all input are ended I am going to increase the array size and put numbers inside.
everything works but free return me this error: "free(): invalid next size (fast): 0x00000..."
I think there are errors inside "extend" function
void extend(int *V, int n, int s)
{
int *pt;
pt = realloc(V, n*sizeof(int));
int i;
pt[5]=s;
for(i=6;i<(5+n);i++)
pt[i] = pt[i-1]*2;
}
int main()
{
int *v;
int n,i, sum;
sum = 0;
v = malloc(sizeof(int)*5);
for(i=0;i<5;i++)
{
scanf("%d", &v[i]);
sum+=v[i];
}
scanf("%d", &n);
extend(v,n,sum);
for(i=0;i<(5+n);i++)
printf("%d\n", v[i]);
free(v);
return 0;
}
The function does not change the original variable v. It should be defined at least like
void extend(int **V, int n, int s)
{
int *pt;
pt = realloc(*V, n*sizeof(int));
if ( pt )
{
int i;
pt[5]=s;
for(i=6; i < n; i++)
pt[i] = pt[i-1]*2;
*V = pt;
}
}
Or
int extend(int **V, int n, int s)
{
int *pt;
pt = realloc(*V, n*sizeof(int));
int success = pt != NULL;
if ( success )
{
int i;
pt[5]=s;
for(i=6; i < n; i++)
pt[i] = pt[i-1]*2;
*V = pt;
}
return success;
}
And the function should be called lije
extend( &v,n,sum);
Take into account that the function is unsafe because in general n can be less than or equal to 5. And this statement
for(i=6;i<(5+n);i++)
^^^^
can result in undefined behavior.
Or the function shall allocate n + 5 elements. For example
pt = realloc(*V, ( n + 5 )*sizeof(int));
^^^^^^^^
Also the using of this magic number 5 is unclear and does not make sense.
Well, in function extend:
You are allocating pt to a memory block of n entries
You are attempting to access pt at indexes beyond n-1
More specifically, with:
pt = realloc(V, n*sizeof(int));
for (i=6; i<(5+n); i++)
pt[i] = ...
You are performing illegal memory-access operations on:
pt[n]
pt[n+1]
pt[n+2]
pt[n+3]
pt[n+4]
The realloc returns a pointer to the new memory block but you do not return that to the caller. Instead v in main, is the same v that was before the call but now invalid and which causes your error no doubt, easiest is to return pt instead:
int* extend(int *V, int n, int s)
{
int *pt;
pt = realloc(V, n*sizeof(int));
int i;
pt[5]=s;
for(i=6;i<(5+n);i++)
pt[i] = pt[i-1]*2;
return pt;
}
...
v = extend(v,n,sum);
another thing that is not correct is your initialization of the new elements, n should contain the number of new elements plus any old since you want the allocated the block to be bigger. better to give old and new number of elements to it:
int* extend(int *V, int oldNumber, int newNumber, int s)
{
int *pt = realloc(V, newNumber*sizeof(int));
int i;
if (pt==NULL)
{
fprintf(stderr,"Out of memory\n");
return V;
}
pt[oldNumber]=s;
// now you can initialize the rest of the array
for(i=oldNumber+1;i<newNumber;i++)
pt[i] = pt[i-1]*2;
return pt;
}
...
v = extend(v,5,n,sum);
Grow an array like this
int capacity = 10; // don't start off as tiny
ENTRY *array = malloc(capacity * sizeof(ENTRY));
int N = 0; //number of entries.
To add
int addentry(ENTRY *entry)
{
ENTRY *temp;
if(N >= capacity)
{
temp = realloc(array, (capacity + capacity/2) * sizeof(ENTRY));
if(!temp)
goto out_of_memory;
array = temp;
capacity = capacity + capacity/2;
}
array[N] = *entry;
N++;
return 0;
out_of_memory:
//always a problem, maybe terminate program here
// as it is, just shunt up
return -1;
}
You need the temp because realloc returns 0 on failure but keeps
the argument intact, so you need to keep array to destroy it
gracefully. Grow by about 1.5, doubling is too aggressive and
reallocating on every call is too expensive.

Freeing malloc of unknown size

I'm trying to free the malloc that is generated with a not fixed number of arrays.
char ** get_moves(){
// some code
char **moves = malloc(sizeof(char *) * k); // 'k', could ranges between 1~9
if (!moves){
return NULL;
}
for(int i = 0; i < k; i++){
moves[i] = malloc(82);
if (!moves[i]) {
free (moves);
return NULL;
}
// more code
return moves;
}
int main(){
//some code
char **res = get_moves(some_input);
//more code
for (int i = 0; i < (sizeof(res)/sizeof(res[0)); i ++){
free(res[i]);
}
free(res);
}
In one of the inputs to get_move, res should have 2 arrays but the sizeof(res)/sizeof(res[0) gives me just 1.
How is the proper way to handle this?
The only way is to keep track of the element count of the array, if you don't want to pass it to every function when passing the array, you can combine both pieces of information in a struct, like here
#include <stdlib.h>
struct ArrayOfStrings
{
int count;
char **data;
};
struct ArrayOfStrings get_moves()
{
struct ArrayOfStrings result;
char **moves;
// some code
result.count = 0;
result.data = malloc(sizeof(char *) * k); // 'k', could ranges between 1~9
if (result.data == NULL)
return result;
result.count = k;
moves = result.data;
for (int i = 0; i < k; i++)
{
moves[i] = malloc(82);
if (moves[i] == NULL)
{
/* also free succesfully allocated ones */
for (int j = i - 1 ; j >= 0 ; --j)
free(moves[j]);
free(moves);
}
result.count = 0;
result.data = NULL;
return result;
}
// more code
return result;
}
int main(){
//some code
struct ArrayOfStrings res = get_moves(some_input);
//more code
for (int i = 0; i < res.count ; i ++)
free(res.data[i]);
free(res.data);
return 0; // you should return from main.
}
sizeof is not for the length of an object's content but for the size of a data type, it is computed at compile time.
So in your case
sizeof(res) / sizeof(res[0]) == sizeof(char **) / sizeof(char *) == 1
since sizeof(char **) == sizeof(char *) it's just the size of a pointer.
sizeof(res)
Returns the sizeof(double-pointer);
So if you intend to get the number of pointers stored then you might not get this by doing what you are doing.
You need to do something like
for(i=0;i<k;i++) /* As I see you are allocating k no of pointer Keep track of it*/
free(res[i]);
free(res);
res is in fact not an array of arrays of char type. Instead it is a pointer to pointer to char type. sizeof(res) will give you the size of char**. You need to keep track of the number of allocations.
Since the maximum number of arrays to allocate is small (9), you can simplify your code by allocating the maximum number. Fill the unused elements with NULL:
#define MAX_K 9
char **moves = malloc(sizeof(char *) * MAX_K);
for(int i = 0; i < k; i++){
...
}
for(int i = k; i < MAX_K; i++){
moves[i] = NULL;
}
To deallocate, just ignore the NULL pointers:
for (int i = 0; i < MAX_K; i ++){
if (res[i])
free(res[i]);
}
free(res);

Resources