New does not allocate memory?

I have this piece of code (in the abc function)

 matriz = new (nothrow) int*[qnt_objetos]; if (matriz == 0) exit(0); for (int i = 0; i < qnt_objetos; i++) { matriz[i] = new (nothrow) int[tam_mochila]; if (matriz[i] == 0) exit(0); } 

matriz is a two-dimensional array declared as such (basically)

 int **matriz = NULL; 

However, memory space is not allocated when using the new operator. What am I doing wrong? And also, is it better to allocate memory directly in the main function? Doesn't that make the code more illegible?

PS: debugging it on NetBeans (or using the terminal), I got this matriz address = 0x0 ( NULL if I'm not mistaken)

+4
source share
2 answers

This means that the new operation is not performed calmly (nothrow) and returns NULL, how much memory do you allocate?

+3
source

I compiled and ran it using g ++ in code blocks. Everything went perfectly, without any problems. You should provide some error checking to make sure that the allocators have the correct value, which is not ridiculously large (IE: unsigned long, which got -1). So be sure to check how big the buffer will be.

0
source

Source: https://habr.com/ru/post/1438535/


All Articles