malloc / irrlicht dll problem

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Endar
Posts: 145
Joined: Mon Jun 14, 2004 7:59 am

malloc / irrlicht dll problem

Post 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?
"The reputation of a thousand years may be determined by the conduct of one hour."
-Japanese Proverb
thesmileman
Posts: 360
Joined: Tue Feb 10, 2004 2:20 am
Location: Lubbock, TX

Re: malloc / irrlicht dll problem

Post 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
Endar
Posts: 145
Joined: Mon Jun 14, 2004 7:59 am

Post 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.
"The reputation of a thousand years may be determined by the conduct of one hour."
-Japanese Proverb
Endar
Posts: 145
Joined: Mon Jun 14, 2004 7:59 am

Post 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!
"The reputation of a thousand years may be determined by the conduct of one hour."
-Japanese Proverb
thesmileman
Posts: 360
Joined: Tue Feb 10, 2004 2:20 am
Location: Lubbock, TX

Post 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?"
thesmileman
Posts: 360
Joined: Tue Feb 10, 2004 2:20 am
Location: Lubbock, TX

Post by thesmileman »

Oh you could also just use std vector to do the same thing.
Endar
Posts: 145
Joined: Mon Jun 14, 2004 7:59 am

Post 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*/
"The reputation of a thousand years may be determined by the conduct of one hour."
-Japanese Proverb
Rocketeer
Posts: 9
Joined: Sun Jul 11, 2004 9:13 am
Contact:

Post 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.
Endar
Posts: 145
Joined: Mon Jun 14, 2004 7:59 am

Post 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.
"The reputation of a thousand years may be determined by the conduct of one hour."
-Japanese Proverb
thesmileman
Posts: 360
Joined: Tue Feb 10, 2004 2:20 am
Location: Lubbock, TX

Post by thesmileman »

@Endar, read up on varible scope it is a very important topic that is often times overlooked.
Endar
Posts: 145
Joined: Mon Jun 14, 2004 7:59 am

Post 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.
"The reputation of a thousand years may be determined by the conduct of one hour."
-Japanese Proverb
thesmileman
Posts: 360
Joined: Tue Feb 10, 2004 2:20 am
Location: Lubbock, TX

Post 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
Endar
Posts: 145
Joined: Mon Jun 14, 2004 7:59 am

Post 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
"The reputation of a thousand years may be determined by the conduct of one hour."
-Japanese Proverb
jox
Bug Slayer
Posts: 726
Joined: Thu Apr 22, 2004 6:55 pm
Location: Germany

Post 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.
Post Reply