# Dynamic allocation from Cairo's playground

**URL:** <https://community.starknet.io/t/dynamic-allocation-from-cairos-playground/2195>\
**Category:** Starknet Technical Development\
**Tags:** cairo\
**Created:** [October 24, 2022, 8:22pm UTC](https://community.starknet.io/t/dynamic-allocation-from-cairos-playground/2195 "2022-10-24T20:22:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![henryf10h](https://dub1.discourse-cdn.com/flex005/user_avatar/community.starknet.io/henryf10h/32/1041_2.png) [@henryf10h](https://community.starknet.io/u/henryf10h)\
**Post date:** [October 24, 2022, 8:22pm UTC](https://community.starknet.io/t/dynamic-allocation-from-cairos-playground/2195/1 "2022-10-24T20:22:30Z")

</div>

Hello, I have an issue when doing the “dynamic allocation” lesson. Here the code I used.

```auto
%builtins output

from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.serialize import serialize_word

// Fills `new_array` with the squares of the first `length` elements in `array`.
func _inner_sqr_array(array: felt*, new_array: felt*, length: felt) {
    if (length == 0) {
        return ();
    }

    assert [new_array] = [array] * [array];

    _inner_sqr_array(array=array + 1, new_array=new_array + 1, length=length - 1);
    return ();
}

func sqr_array(array: felt*, length: felt) -> (new_array: felt*) {
    // we need to allocate memory,
    // because we are doing function
    // calls and variables within could be revoked
    alloc_locals; 
    //how to allocate memory to an array?
    let(local new_array) = alloc();
    _inner_sqr_array(array=array, new_array=new_array, length=length);
    return(new_array);
}

func main{output_ptr: felt*}() {
    alloc_locals;
    // Allocate a new array.
    let (local array) = alloc();
    // Fill the new array with field elements.
    assert [array] = 1;
    assert [array + 1] = 2;
    assert [array + 2] = 3;
    assert [array + 3] = 4;

    let (new_array) = sqr_array(array=array, length=4);
    serialize_word([new_array]);
    serialize_word([new_array + 1]);
    serialize_word([new_array + 2]);
    serialize_word([new_array + 3]);

    return ();
}

```

I get the following error within sqr\_array() function:

```auto
Error: code:40:12: Expected expression of type '(new_array: felt*)', got 'felt*'.
    return(new_array);

```

---

<div class="post-metadata">

**Author:** ![henryf10h](https://dub1.discourse-cdn.com/flex005/user_avatar/community.starknet.io/henryf10h/32/1041_2.png) [@henryf10h](https://community.starknet.io/u/henryf10h)\
**Post date:** [October 25, 2022, 2:52am UTC](https://community.starknet.io/t/dynamic-allocation-from-cairos-playground/2195/2 "2022-10-25T02:52:20Z")

</div>

Error found: we need to explicitly return new\_array.

```auto
func sqr_array(array: felt*, length: felt) -> (new_array: felt*) {
    alloc_locals; 
    let(local new_array) = alloc();
    _inner_sqr_array(array=array, new_array=new_array, length=length);
    return(new_array = new_array);
}

```
