0

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?

11
  • 9
    This is the perfect time to learn how to debug your programs. More specifically, if you run your program in a debugger it will catch the crash, and allow you to walk up and down the function call stack, so you can locate exactly where it happens in your code. You can also examine variables and their values to make sure they are okay. Commented May 6, 2018 at 20:03
  • 3
    If, after examining in a debugger, you still can't figure it out, then you need to create a Minimal, Complete, and Verifiable Example and show us, and tell us where in your code the crash happens. Commented May 6, 2018 at 20:04
  • 3
    So you think the problem is in main() but do not show the code of that? Please make a minimal reproducible example. Commented May 6, 2018 at 20:12
  • 1
    Don't the compiler shout warnings at you? The variable p is not of the correct type. And you pass the wrong type as first argument to set as well (which is the probable cause of your crash). Listen to your compiler. Commented May 6, 2018 at 20:31
  • 1
    Then you should enable more warnings. The compiler is definitely able to detect such things. Commented May 6, 2018 at 20:43

3 Answers 3

2
  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.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you,it solved some of my problems, thank you so much for taking the time in reading my wall text and responding.
1

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.

1 Comment

thanks for taking the time in explaining what was wrong,it helped me fix some of the problems.
1

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;
}

1 Comment

thank you for the time taken into fixing and replying,much appreciated.i managed to fix my program with everyone's suggestions and even the other functions work,but more ways of writing the same thing are always appreciated and welcomed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.