Search code examples
c++memorydynamicallocation

Dynamic Allocation of Memory in C++


I'm trying to figure out dynamic allocation in c++ but i encountered a problem.

#include <stdio.h>
#include <malloc.h>

float *alocareV(int n)
{
    float *v;
    v = (float *)malloc(n*sizeof(float));
    return v;
}

float **alocareM(int m, int p)
{
    float **a;
    int i;
    a = (float **)malloc(m*sizeof(float));
    for (i = 0; i < m; i++)
        *(a + i) = (float *)malloc(p*sizeof(float));
    return a;
}

void dezalocareM(float **a ,int p)
{
    int i;
    for (i = 0; i < p; i++)
        free (*(a + i));
}

void citireV(float *v, int n)
{
    int i;
    for (i = 0; i < n; i++)
    {
        printf("v[%d]= ", i);
        scanf_s("%f", *(v + i));
    }
}

void citireM(float **a, int m, int p)
{
    int i, j;
    for (i = 0; i < m;i++)
    for (j = 0; j < p; j++)
    {
        printf("a[%d][%d]= ", i, j);
        scanf_s("%f", *( *(a + i) + j ) );
    }
}

void main()
{
    float *v=0, **a=0;
    int n, m, p;
    int i, j;

    printf("n este egal cu ");
    scanf_s("%d", &n);
    printf("m este egal cu ");
    scanf_s("%d", &m);
    printf("p este egal cu ");
    scanf_s("%d", &p);

    alocareV(n);
    alocareM(m, p);

    citireV(v, n);
    citireM(a, m, p);

    free(v);
    dezalocareM(a, p);

}

Firstly i had to initialize *v=0 and **a=0 in main because i got an error telling me "uninitialized local variable used". If i didn't use subprograms that error wouldn't appear. Secondly after entering the 3 variables (n, m, p) i get a stopped working box with the problems detalis being:

Problem signature:
  Problem Event Name:   APPCRASH
  Application Name: AlocareDinamica.exe
  Application Version:  0.0.0.0
  Application Timestamp:    54f1d7f9
  Fault Module Name:    AlocareDinamica.exe
  Fault Module Version: 0.0.0.0
  Fault Module Timestamp:   54f1d7f9
  Exception Code:   c0000005
  Exception Offset: 00014059
  OS Version:   6.3.9600.2.0.0.768.100
  Locale ID:    1033
  Additional Information 1: 5861
  Additional Information 2: 5861822e1919d7c014bbb064c64908b2
  Additional Information 3: a10f
  Additional Information 4: a10ff7d2bb2516fdc753f9c34fc3b069

Can someone help please? Thank you


Solution

  • First of all, note that your question has little to do with C++. It's completely C, as far as I can see. In C++, you would use new instead of malloc for the rare occasions in which you deal with low-level memory business, and classes like std::string or std::vector for higher levels.

    Still, let's see what's wrong here.

    The most apparent problem is that there is no connection between that which happens in alocareV and that which happens in citireV, same for alocareM/citireM. The result of your allocation functions is discarded. You must use the results:

    v = alocareV(n);
    a = alocareM(m, p);
    

    There is also an error in alocareM. When you allocate memory for the double pointer, you should use sizeof(float*), not sizeof(float).

    The next error is in citireV. scanf_s requires you to pass a pointer to the object to be written to. *(v + i), however, yields a float, not a float*, because you dereference the pointer. The correct call would be:

    scanf_s("%f", (v + i));
    

    citireM has exactly the same error. The correct call should be:

    scanf_s("%f", ( *(a + i) + j ) );
    

    Finally, main must return int. void main is not legal in C or C++.

    That should fix it.