简体   繁体   中英

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?

  1. In init function (vectors)[i] = NULL; should actually be (*vectors)[i] = NULL;
  2. 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 , ie 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM