Lame Fanello
Lame Fanello

Reputation: 615

How can I use malloc from a function?

I am trying to understand how malloc works. I did a program searches for the largest element in a one dimensional array int.

This is the code.

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

void largest_element(int *nbr)
{
    int i;
    int n;
    int m;

    i = 1;

    nbr = (int*)malloc(sizeof(nbr) + 8);

    while (i < 8)
    {
        if (*nbr < *(nbr + i))
            *nbr = *(nbr + i);
        i++;
    }
    printf("%d ", *nbr);
}

int main(void)
{
    int i;
    int tab[8] = {11, 2, 4, 5, 9, 7, 8, 1};
    int n = sizeof(tab)/sizeof(int);

    i = 0;

    largest_element(&tab[8]);

    return(0);
}

The program works without malloc but how can I make it work with malloc? What did I do wrong and why does my code only give me garbage numbers?

Upvotes: 0

Views: 490

Answers (2)

Siliace
Siliace

Reputation: 652

I think you are lost with pointers and arrays so you can not understand malloc properly (no offense, everyone who is learning C do the same mistake).

Let's take your main function. When you run:

int tab[8] = {11, 2, 4, 5, 9, 7, 8, 1};

You staticly allocate an array of 8 integers and you fill it with your numbers.

The dynamic equivalent would be:

int* tab = malloc(sizeof(int) * 8);
tab[0] = 11;
tab[1] = 2;
/// Etc...
tab[7] = 1;

First thing: the first element of an array has the index 0. So in your largest_element function, i should be initialized at 0 instead of 1.

The reason is, when you deal with array, you deal with pointers. In your case, tab is a pointer to the first element of the array. So, when you do tab[3], you get the forth element of your array.

Second thing: when you do:

largest_element(&tab[8]);

You send to your function the eighth element after the begining of your array. The problem is: you do not own this memory area! You own the memory only until tab[7].

If you want to send the complete array to your function, just use:

largest_element(tab);

Now, let's talk about your largest_element function.

  1. You do not need to call malloc here since the memory is already allocated
  2. When you do *nbr = *(nbr + i); you change the value of the first element of your array. I think you wanted to do m = *(nbr + i); isn't it.
  3. Why do you not use the nbr[i] instead of *(nbr + i)?

A correct implementation of this function would be something like (not tested):

void largest_element(int *nbr)
{
    int i = 0;
    int max = 0;

    while (i < 8)
    {
        if (max < nbr[i])
            max = nbr[i];
        i++;
    }

    printf("%d ", m);
}

A last thing, using malloc involve using the function free to release the memory when you do not need it anymore.

Upvotes: 3

Jabberwocky
Jabberwocky

Reputation: 50775

What did I do wrong and why does my code only give me garbage numbers??

In largest_element(int *nbr) nbr points to the array tab in main (at least if you call it like this: largest_element(tab); instead of like this largest_element(&tab[8]);

Then you call nbr = (int*)malloc(sizeof(nbr) + 8); now nbr points to some allocated memory which has not been initialized and which contains garbage values. Now if you read from that memory it's normal that you get garbage values.

You simply don't need malloc for this problem, just as you don't need floating point math or file system related functions for this problem.

Upvotes: 0

Related Questions