Page 1 of 1

malloc / irrlicht dll problem

Posted: Fri Jul 16, 2004 6:18 am
by Endar
Okay, I'm trying to re-compile the engine with an extra function that I've written, and in one place, I'm using malloc. The function malloc, being a std C function, is not included in C++. So, in the engine source file, should i just go ahead and include the <stdlib.h> header file, or should I use the 'new' operator? I probably should use the 'new' operator.....

Here's what I'm trying to accomplish:

Code: Select all

height_map_matrix = (f32**) malloc( sizeof(float)*( (xf+1) * (yf+1) ) );
How would I use this with the 'new' operator??

Code: Select all

height_map_matrix = new f32[xf+1][yf+1];
Something like this?

Re: malloc / irrlicht dll problem

Posted: Fri Jul 16, 2004 6:40 am
by thesmileman
Endar wrote:The function malloc, being a std C function, is not included in C++.
That is an incorrect statements in its entirety. You are able to use mallac as well as any ASCII C code in C++. That being said new is the usually recommended choice for C++ as if will call an objects constructor. mallac is just interested in the size of the object.

Rather than give you the answer I am going to send you on a learning lesson.:D Here is an website with an explanation of the differences of mallac and new how to use them correctly: :http://www.codeproject.com/tips/newandmalloc.asp

Enjoy :D

Posted: Fri Jul 16, 2004 8:35 am
by Endar
Appreciated :D

I usually use the term "std C" for the functions that are in all the header files that I used while learning C.

Posted: Fri Jul 16, 2004 10:13 am
by Endar
Small problem: I'm getting "non-constant expression as array bound".

Code: Select all

f32** height_map_matrix = new f32[xf+1][yf+1];
any help?

EDIT:
@thesmileman: I just now really looked at what I wrote about malloc not being included in C++ and I had a damn good laugh. I really have to pay more attention to what I write!

Posted: Fri Jul 16, 2004 11:57 am
by thesmileman
This is because you are using a non constant for the bounds of the arrays.

The new keyword treats an array still as an array so you can not do what you are wanting to that way.

check this out it is EXCACTLY what you are looking for:
http://www.parashift.com/c++-faq-lite/f ... #faq-16.15

The title of the article is: "How do I allocate multidimensional arrays using new?"

Posted: Fri Jul 16, 2004 11:58 am
by thesmileman
Oh you could also just use std vector to do the same thing.

Posted: Fri Jul 16, 2004 12:36 pm
by Endar
Okay, a somewhat more confusing problem.

The array looks like its being declared alright, and I call it almost straight after it's been created and everything is fine, but then I call it later in the function and the compiler gives me an "undeclared identifier" error. I am thinking that it could be because it isn't actually declared at the start of the function, its declared inside the body of an 'else' condition.
Here's the layout (psuedocode):

Code: Select all

function(){
     if{
          // code
     }
     else{
          while{
          }
          // create matrix here
          f32* height_map_matrix = new f32[ (xf+1)*(yf+1) ];
          while{
               // Access matrix here (no compiler error)
               height_map_matrix[ (curr_xf * xf + curr_yf) ] = curr_zf;
          }
     } /* end else */

     while{
          while{

               for{
                     for{
                           // trying to access, error here
                           f32 height = height_map_matrix[ ((x+processed.X) * xf + (y+processed.Y)) ];
                     }
               }
          }/* end while*/
      }/* end while */

}/* end function*/

Posted: Fri Jul 16, 2004 12:42 pm
by Rocketeer
Endar wrote:The array looks like its being declared alright, and I call it almost straight after it's been created and everything is fine, but then I call it later in the function and the compiler gives me an "undeclared identifier" error. I am thinking that it could be because it isn't actually declared at the start of the function, its declared inside the body of an 'else' condition.
I'd say you hit the nail on the head there... think it through, what if the if was true? Then the array has not been declared... so you can't call it.
It works fine with the while because that is still inside the else that declared the array.

Posted: Fri Jul 16, 2004 12:56 pm
by Endar
Yeah, I see your point, but wouldn't that just be a runtime error instead of a compiler error?? It really doens't matter because if the 'if' is true, then it 'return's out of the function.

But I do think that I would just get a memory error at runtime instead of a compiler error.

Posted: Fri Jul 16, 2004 1:02 pm
by thesmileman
@Endar, read up on varible scope it is a very important topic that is often times overlooked.

Posted: Fri Jul 16, 2004 1:10 pm
by Endar
Yeah, I had a feeling it was something like that. I just didn't know that there was scope inside loops and 'if' statements. Okay, thanks.

Posted: Fri Jul 16, 2004 5:05 pm
by thesmileman
By the way Endar, how old are you. I really have no idea but everytime I see cookie monster I think you are like 5 years old. Well I think of that and cookies and milk. :D

Posted: Mon Jul 19, 2004 9:17 am
by Endar
:D:D

I'm 19.

I know, I know, cookie monster is kinda for the little kids, but hey, I think he's cool!

:D

Posted: Mon Jul 19, 2004 10:23 am
by jox
@Endar:

Code: Select all

   f32** height_map_matrix;
   height_map_matrix = new f32*[sizex]; 
   for(int x=0; x<sizex; x++) data[x] = new f32[sizey]; 
We discussed this recently here:

http://irrlicht.sourceforge.net/phpBB2/ ... hp?p=17413

Maybe this helps.