I need to allocate memory to a pointer variable passed by parameter.

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

typedef int* PTR_VALUE;

int initialize(PTR_VALUE value_array)
{
    value_array = (PTR_VALUE)malloc(sizeof(PTR_VALUE) * 94);

    return 0;
}

int main()
{
    PTR_VALUE pValue;

    initialize(*pValue);
    printf("%d", pValue);
    gets("");

    return 0;
}

but I get an error when I run the program.

Recommended Answers

All 5 Replies

What error do you get?

Also, I think you should be getting at least a few warnings out of your compiler, if not errors. Are you?

Things I notice at a first glance:

You have...

int initialize(PTR_VALUE value_array);

... which is equivalent to:

int initialize(int* value_array);

... but you call it like this:

PTR_VALUE pValue;
initialize(*pValue);

*pValue is dereferencing your pointer, so you're passing in the value pointed at by pValue, not the pointer itself.

Try this for starters:

initialize(pValue);

You have typedef'd PTR_VALUE, but in this function:

int initialize(PTR_VALUE value_array)
{
    value_array = (PTR_VALUE)malloc(sizeof(PTR_VALUE) * 94);
    return 0;
}

You initialized the value_array parameter, but it is never returned to the calling function, or set as a global or static variable. IE, it has taken up memory, but it isn't accessible from anywhere outside of this function. Try this instead:

PTR_VALUE initialize(void)
{
    PTR_VALUE value_array = (PTR_VALUE)malloc(sizeof(PTR_VALUE) * 94);
    return value_array;
}

I need to pass the PTR_VALUE via parameter and have memory assigned that way to it.

I now have a different problem. The error occures when I try to free(value_array) The error message only says that a breakpoint has been triggered.

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

#define MAX_VALUE_ARRAY_SIZE 94

typedef int* PTR_VLAUE_ARRAY;

int initialize(PTR_VLAUE_ARRAY value_array)
{
    value_array = (PTR_VLAUE_ARRAY)malloc(sizeof(int) * MAX_VALUE_ARRAY_SIZE);
    if (value_array == NULL)
        return -1;

    return 0;
}

void populate_value_array(PTR_VLAUE_ARRAY value_array)
{
    for (int i = 0; i < MAX_VALUE_ARRAY_SIZE; i++)
        value_array[i] = 1;
}

void clean_up(PTR_VLAUE_ARRAY value_array)
{
    free(&value_array);
}

int main()
{
    PTR_VLAUE_ARRAY pValue;
    initialize(&pValue);
    populate_value_array(&pValue);
    clean_up(&pValue);
    getch();
}

The problem is that you are passing a pointer by value and expecting to be able to change it. What you need to do is to pass a pointer to the int pointer:

int initialize(PTR_VLAUE_ARRAY *value_array)
{
    *value_array = (PTR_VLAUE_ARRAY)malloc(sizeof(int) * MAX_VALUE_ARRAY_SIZE);
    if (*value_array == NULL)
        return -1;
    return 0;
}

I'm surprised the compiler doesn't yell at you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.