George
George

Reputation: 51

dynamic memory allocation in c throws an error for a certain size

I'm trying to create an array (dynamically) and populate it with random numbers.

I'm on Linux. The program compiles without errors. This is the C code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void create_array(int **, int);
void populate_array(int *X, int size, int low, int high);
void display_array(int *X, int size);


int main()
{
    int *A = NULL;
    int size = 7;
    int low = 10;
    int high = 1000;
    create_array(&A, size);
    populate_array(A, size, low, high);
    display_array(A, size);
    return 0;
}

void create_array(int **X, int size)
{
    *X = (int *)(malloc(size));
}

void populate_array(int *X, int size, int low, int high)
{
    srand(time(0));
    for (int i = 0; i < size; ++i)
    {
        *(X + i) = low + rand() % (high + 1 - low);
    }
}

void display_array(int *X, int size)
{
    for (int i = 0; i < size; ++i)
    {
        if (i % 10 == 0)
            printf("\n");
        printf("%d\t", *(X + i));
    }
    printf("\n");
}

However, when I run it, I get the following error:

malloc.c:2394: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed. Aborted (core dumped)

This error is only produced for size = 7. For lower values it's all good. But for higher values, it's another story! Output for size = 20:


455     526     335     719     907     695     1041    0       154481972       154546741
154481459       154743095       154482992       875836721       960049720       926419250       909326389       154219063       808465977       842479924

In contrast, the same program (almost) in C++ gives me the expected output. Here's the code:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

void create_array(int *&, int);
void populate_array(int *X, int size, int low, int high);
void display_array(int *X, int size);

int main()
{
    int *A;
    int size = 100;
    int low = 10;
    int high = 1000;
    create_array(A, size);
    populate_array(A, size, low, high);
    display_array(A, size);
    return 0;
}

void create_array(int *&X, int size)
{
    X = new int[size];
}

void populate_array(int *X, int size, int low, int high)
{
    srand(time(0));
    for (int i = 0; i < size; ++i)
    {
        X[i] = low + rand() % (high + 1 - low);
    }
}

void display_array(int *X, int size)
{
    for (int i = 0; i < size; ++i)
    {
        if (i % 10 == 0)
            cout << endl;
        cout << X[i] << "\t";
    }
    cout << endl;
}

What am I doing wrong?

Upvotes: 0

Views: 229

Answers (1)

kiran Biradar
kiran Biradar

Reputation: 12742

*X = (int *)(malloc(size));

You are allocating size number of bytes perhaps what you want is

*X = malloc(sizeof(int)*size);

Note: malloc takes number of bytes to be allocated as argument. Also for for c implementation you might want to read Why not to cast malloc return.

Upvotes: 5

Related Questions