Thread: As i guess something is wrong with memory allocation. Need help!

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    3

    As i guess something is wrong with memory allocation. Need help!

    My tasks was to write a program which finds difference between two arrays (elements which are in first array but not in second and vice-versa).
    Program works, but something wrong is with the memory allocation for array.
    As i guess something is wrong with memory allocation. Need help!-capture-jpg

    My code is:
    main.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "header.h"
    
    
    int main()
    {
        int n, m;
    
    
        printf("Iveskite pirmo masyvo ilgi: ");
        scanf("%d", &n);
    
    
        int *Mas;
        Mas = (int*) malloc (sizeof(int) * n);
    
    
        printf("Iveskite pirmo masyvo elementus: ");
        Skaito(Mas, n);
    
    
        printf("\n\nIveskite antro masyvo ilgi: ");
        scanf("%d", &m);
    
    
        int *Mas_2;
        Mas_2 = (int*) malloc (sizeof(int) * m);
    
    
        printf("Iveskite antro masyvo elementus: ");
        Skaito(Mas_2, m);
    
    
        Raso(Mas, n);
        Raso(Mas_2, m);
    
    
        int *Skirt;
        Skirt = (int*) malloc (sizeof(int));
    
    
        int kiek = 0;
        kiek = Skirtumas(Skirt,Mas,Mas_2,n,m,kiek);
        kiek = Skirtumas(Skirt,Mas_2,Mas,m,n,kiek);
        Raso(Skirt,kiek);
    
    
        return 0;
    }
    header.h
    function "ar_buvo" checks if certain element is already inserted in difference's array.
    function "ar_tinka" checks if certain element meets requirements.
    Counts how many certain elements are in first and second arrays then counts difference.
    function "Skirtumas" makes difference's array: allocates memory and etc.
    Code:
    #ifndef HEADER_H_INCLUDED
    #define HEADER_H_INCLUDED
    
    
    int ar_buvo (int *A, int kiek, int reiksme);
    void Raso (int *A, int kiek);
    void Skaito (int *A, int kiek);
    int ar_tinka (int reiksme, int *A1, int *A2, int n, int m);
    int Skirtumas (int *Skirt, int *A1, int *A2, int A1_ilgis, int A2_ilgis,int kiek);
    
    
    #endif // HEADER_H_INCLUDED
    func.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int ar_buvo (int *A, int kiek, int reiksme)
    {
        int i;
        int buvo_nebuvo = 0;
        if (kiek == 0)
        {
            buvo_nebuvo = 0;
        }
        else
        {
            for (i = 0; i < kiek; i++)
            {
                if (reiksme == A[i])
                {
                    buvo_nebuvo = 1;
                }
            }
        }
        return buvo_nebuvo;
    }
    
    
    void Raso (int *A, int kiek)
    {
        int i;
        for (i = 0; i < kiek; i++)
        {
            printf("%d ",A[i]);
        }
        printf("\n");
    }
    
    
    void Skaito (int *A, int kiek)
    {
        int skc, i;
        for (i = 0; i < kiek; i++)
        {
            scanf("%d", &skc);
            A[i] = skc;
        }
    }
    
    
    int Skirtumas (int *Skirt, int *A1, int *A2, int A1_ilgis, int A2_ilgis, int kiek)
    {
        int i, j;
        for (i = 0; i < A1_ilgis; i++)
        {
            if (ar_buvo(Skirt, kiek, A1[i])==0)
            {
                int kiek_2 = ar_tinka(A1[i], A1, A2, A1_ilgis, A2_ilgis);
                for (j = kiek; j < (kiek + kiek_2); j++)
                {
                    Skirt = (int*) realloc (Skirt, sizeof(int));
                    Skirt[j] = A1[i];
                }
                kiek = kiek + kiek_2;
            }
        }
        return kiek;
    }
    
    
    int ar_tinka (int reiksme, int *A1, int *A2, int n, int m)
    {
        int kiek = 0;
        int kiek_1 = 0;
        int kiek_2 = 0;
        int i;
        for (i = 0; i < n; i++)
        {
            if (A1[i] == reiksme)
            {
                kiek_1++;
            }
        }
        for (i = 0; i < m; i++)
        {
            if (A2[i] == reiksme)
            {
                kiek_2++;
            }
        }
        if (kiek_1 > kiek_2)
        {
            kiek = kiek_1 - kiek_2;
        }
        else
        {
            kiek = kiek_2 - kiek_1;
        }
        return kiek;
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    505
    Code:
    Skirt = (
    Code:
    int*) realloc (Skirt, sizeof(int));
    


    you need to realloc to the new size of the array, (j+1)*sizeof(int) I think, but I haven't followed the logic closely.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    3
    didn't work that :/ any more suggestions?

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    What didn't work? Show your changes.

    Bye, Andreas

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    3
    so i have made changes like Malcolm said and the program crashes if i input:
    as first array: 1 1 1 1 1
    as second array: 0 0 0 0 0 0
    ahhh... not only crashes. With other tests the results are non-sense

    and this is how it looks now:
    Code:
    int Skirtumas (int *Skirt, int *A1, int *A2, int A1_ilgis, int A2_ilgis, int kiek){
        int i, j;
        for (i = 0; i < A1_ilgis; i++)
        {
            if (ar_buvo(Skirt, kiek, A1[i]) == 0)
            {
                int kiek_2 = ar_tinka(A1[i], A1, A2, A1_ilgis, A2_ilgis);
                for (j = kiek; j < (kiek + kiek_2); j++)
                {
                    Skirt = (int*) realloc (Skirt, (j+1)*sizeof(int));
                    Skirt[j] = A1[i];
                }
                kiek = kiek + kiek_2;
            }
        }
        return kiek;
    }
    Last edited by autorius; 03-13-2013 at 12:50 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > int Skirtumas (int *Skirt, int *A1, int *A2, int A1_ilgis, int A2_ilgis, int kiek)
    ...
    > Skirt = (int*) realloc (Skirt, (j+1)*sizeof(int));
    Are you expecting the caller of this function to receive a possibly moved pointer to the memory you've been realloc'ing?

    Because if you are, you need to make some changes.
    Code:
    int Skirtumas (int **Skirt, int *A1, int *A2, int A1_ilgis, int A2_ilgis, int kiek){
        int i, j;
        for (i = 0; i < A1_ilgis; i++)
        {
            if (ar_buvo(*Skirt, kiek, A1[i]) == 0)
            {
                int kiek_2 = ar_tinka(A1[i], A1, A2, A1_ilgis, A2_ilgis);
                for (j = kiek; j < (kiek + kiek_2); j++)
                {
                    *Skirt = (int*) realloc (*Skirt, (j+1)*sizeof(int));
                    (*Skirt)[j] = A1[i];
                }
                kiek = kiek + kiek_2;
            }
        }
        return kiek;
    }
    And you need to call it with &mySkirt to pass in a pointer to your pointer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-12-2013, 04:00 AM
  2. Replies: 17
    Last Post: 10-20-2011, 06:32 PM
  3. Replies: 7
    Last Post: 10-19-2011, 08:45 AM
  4. Memory allocation problem, don't know what's wrong
    By Metalmurphy in forum C Programming
    Replies: 10
    Last Post: 04-22-2008, 04:32 AM
  5. Memory allocation
    By ski_photomatt in forum C Programming
    Replies: 1
    Last Post: 06-02-2004, 01:52 PM