简体   繁体   中英

memory alocation with 2D Array

I am facing a question in which I am required to create a function that gets a 2D array and it's size and it should return another 2D array which is basically the same one but half rows size and half columns size and each group of arrays depending on the size of the original matrix will be pasted next to each other, example:

https://imgur.com/a/ctRUopc

image of the faulty output i am getting: https://imgur.com/a/85q8ipe

it keeps giving me trash value after the second matrix paste for some reason and i dont know why:/

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
long long Power(long long C1, long long C2)
{
    int Digit = 0, i = 0;
    long long Flag, DigitCount=0, Multiplier = 1;
    Flag = C2;
    while (Flag != 0)
    {
        Digit = Flag % 10;
        DigitCount++;
        Flag = Flag / 10;
    }
    while (i < DigitCount)
    {
        Multiplier = Multiplier * 10;
        i++;
    }
    long long Final = 0;
    Final = (Multiplier * C1) + C2;
    return Final;
}
long long** shrink(long long** Matrix, int size, int* pSize)
{
    if (size % 2 != 0)
    {
        return 0;
    }
    *pSize = size / 2;
    long long A, B, C;
    long long **New_Matrix = 0;
    New_Matrix = (long long**)malloc(*pSize * sizeof(long long*));
    for(int i=0; i<*pSize; i++)
    {
        New_Matrix[i] = (long long*)malloc(*pSize * sizeof(long long));
        for (int j = 0; j < *pSize; j++)
        {
            A = Power(Matrix[2 * i][2 * j], Matrix[2 * i][2 * j + 1]);
            B = Power(A, Matrix[2 * i + 1][2 * j]);
            C = Power(B, Matrix[2 * i + 1][2 * j + 1]);
        }
    }
    return New_Matrix;
}
int main()
{
    long long** Matrix = 0;
    int size;
    int *pSize;
    long long** result=0;
    printf("Size Insertion : \n");
    scanf("%d", &size);
    Matrix = (long long**)malloc(size * sizeof(long long*));
    printf("Matrix Insertion : \n");
    for (int i = 0; i < size; i++)
    {
        Matrix[i] = (long long*)malloc(size * sizeof(long long));
        for (int j = 0; j < size; j++)
        {
            scanf("%lld", &Matrix[i][j]);
        }
    }
    printf("Matrix Display : \n");
    for (int i = 0; i < size; i++)
    {

        for (int j = 0; j < size; j++)
        {
            printf("%lld  ", Matrix[i][j]);
        }
        printf("\n");
    }
    result = shrink(Matrix, size, &pSize);
    for (int i = 0; i < pSize; i++)
    {
        for (int j = 0; j < pSize; j++)
        {
            printf("%lld ", result[i][j]);
        }
        printf(" \n");
    }

    free(result, Matrix);
    return 0;
}
 result = shrink(Matrix, size, &pSize)

Here you're passing the address of the pSize pointer to the function, which when dereferenced, returns the underlying pointer. So the line *pSize = size / 2; assigns the value to the underlying pointer which is simply wrong. Pointers only hold memory addresses. You may probably have meant to have written this: **pSize = size / 2; You should dereference once to get the underlying pointer, twice to get to the variable it points to.

Also, you need to dereference pSize in the for loops as well to get the size value set by the function

for (int i = 0; i < *pSize; i++)
    {
        for (int j = 0; j < *pSize; j++)
        {
            printf("%lld ", result[i][j]);
        }
        printf(" \n");
    }

Also, pSize is an empty pointer, it points to an undefined memory address. Make sure that's not the case:

int * pSize = (int *) malloc(sizeof (int));
// rest of code
free(pSize);

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