allocate memory in device code?

Is it possible to allocate memory in device code?

I need to allocate device memory dynamically in device code.
I tried to use cudaMalloc() and malloc(), but it does not work.

You can’t do that, you need to do it from the host code