C++ and 'new'
Ok, I have decided to dive into C++ a bit by writing a basic 3D engine (loads a model, loads a terrain, etc). In the days of C, I''d load the model in by first declaring a structure to store all of the verts, UV mapping, texture coords, whatever, and then I''d do the linked list thing.
Can I avoid having to do this by simply declaring a variable (say verts) and using it like an array or is that a really, really bad thing to do?
While I am at it, I can pass objects as parameters to functions, but not return them using ''return'' (I''d have to do a pass by reference to get anything out of it), right?
BTW, the engine is just going to sit on top of OpenGL, if things go right, it would be possible to sit it on any API.
You''re moving from C++ to C then? It might be worth your while to learn STL, because most code is just manipulating data and that''s what STL seems to be extremely fast at. It has types for nearly all the data structures you can think of, plus it is extensible enough that you can make your own data structures from it if you need to. I wouldn''t try to learn it from the docs though; I''d recommend a book. A good book. Maybe STL for Dummies, if it exists (I love ...for Dummies books!)
I forget how C did the variable passing (by reference?), but C++ does passes parameters by value, and returns them by value. You can use the & operator (i.e., int& x) to make it use the exact same object, if you want. You can also return by reference if you want, but only return by reference things that are not temporary. You can return basically any object that you want to return; it doesn''t have to be one of the intrinsic types (int, char, float, etc.).
The real power of C++ is that you can use classes just like the intrinsic types, like you develop your own "extension" to the language. If you are going to delve into C++ by writing a basic 3D engine, probably using overloaded operators (+, +=, /, etc. on your own classes), you might do well to get a book on the subject, or at least on general C++. Especially if you''ve never heard of "temporary" variables and "unnamed" variables, because they can be especially irritating at first when you don''t know how they work.
I wish you the best of luck on your engine!
- null_pointer
Sabre Multimedia
I forget how C did the variable passing (by reference?), but C++ does passes parameters by value, and returns them by value. You can use the & operator (i.e., int& x) to make it use the exact same object, if you want. You can also return by reference if you want, but only return by reference things that are not temporary. You can return basically any object that you want to return; it doesn''t have to be one of the intrinsic types (int, char, float, etc.).
The real power of C++ is that you can use classes just like the intrinsic types, like you develop your own "extension" to the language. If you are going to delve into C++ by writing a basic 3D engine, probably using overloaded operators (+, +=, /, etc. on your own classes), you might do well to get a book on the subject, or at least on general C++. Especially if you''ve never heard of "temporary" variables and "unnamed" variables, because they can be especially irritating at first when you don''t know how they work.
I wish you the best of luck on your engine!
- null_pointer
Sabre Multimedia
Basically, anything you can do in C, you can do in C++. But C++ offers more: it provides object oriented programming. I suggest getting a good book on the subject. Any good C++ book should introduce the subject sufficiently.
C passes arguments by value as well. The easiest in C++ is to pass by reference. If the function doesn''t change the object, then pass it by const reference (const &). It''s an extra safeguard so that the compiler can check if the object indeed isn''t modified.
Passing by reference is way more efficient than pass-by-value, because by the latter a copy of the object is created on the stack. If the object is large, it''s pretty inefficient.
When returning an object, you can return by value. Most compilers include an optimisation called Named Return Value which transforms a function internally in one that has an extra paramter that will be the return value.
Example:
class A { ... }
A foo()
{
A a;
a.bar();
return a;
}
A val = foo();
Good compilers will transform this internally to
void foo(A& a)
{
a.bar();
}
A val;
foo(val);
If you return a reference or pointer, make sure it''s not an object that''s in the functions'' scope. If that''s the case, it will be destroyed after the function returns, leaving you with a dangling reference/pointer.
Erik
C passes arguments by value as well. The easiest in C++ is to pass by reference. If the function doesn''t change the object, then pass it by const reference (const &). It''s an extra safeguard so that the compiler can check if the object indeed isn''t modified.
Passing by reference is way more efficient than pass-by-value, because by the latter a copy of the object is created on the stack. If the object is large, it''s pretty inefficient.
When returning an object, you can return by value. Most compilers include an optimisation called Named Return Value which transforms a function internally in one that has an extra paramter that will be the return value.
Example:
class A { ... }
A foo()
{
A a;
a.bar();
return a;
}
A val = foo();
Good compilers will transform this internally to
void foo(A& a)
{
a.bar();
}
A val;
foo(val);
If you return a reference or pointer, make sure it''s not an object that''s in the functions'' scope. If that''s the case, it will be destroyed after the function returns, leaving you with a dangling reference/pointer.
Erik
I don't know if VC++ 5.0 supports returning custom objects. I do know it doesn't support 'string'. Most agitating.
As for overloading, I might be able to leave that until I write the OpenGL interface layer. Since GL uses its own variable types, so to speak, things should get interesting. I might just hack together a header file that has a few #ifdefs in it to determine how/what to overload, for portability. If I were lazy, I'd ignore that and just use the common data types (float as opposed to glfloat).
Gotta love that inheritance stuff. I just have a base object with the functions defined to do what all objects in the engine will be able to do - move, rotate, delta move, delta rotate, etc. I'll probably not overload the base functions and just use type casting, as needed. Not the most elegant approach, but it maintains the flexibility to add other API support to it.
Will 'new' do what I need it to? EX:
float *a;
new a;
for(j=0; j<10; j++)
{
a[j][0]=0;
a[j][1]=0;
a[j][2]=0;
}
C++ won't smoke my OS for trying the above will it?
There are some rather neat looking template libraries from a book I would love to intergrate, eventually. It is a rather good random number generator. Unfortunately, I get a headache from trying to understand all the notations for templates, but I can figure out what the template does.
Oh yeah, the reason I am so interested in being able to pass and return objects is that it will save me a headache in designing the base model class. It will save me the pains of having to intergrate the drawing routines with the actual object. I'd rather just have the graphics class do that - it keeps things modular. Good, now I just need to pass the camera object(for dirty visibility checks) and the model object to draw the model.
Well, thanks for the info!
Edited by - Whirlwind on May 21, 2000 8:25:37 PM
As for overloading, I might be able to leave that until I write the OpenGL interface layer. Since GL uses its own variable types, so to speak, things should get interesting. I might just hack together a header file that has a few #ifdefs in it to determine how/what to overload, for portability. If I were lazy, I'd ignore that and just use the common data types (float as opposed to glfloat).
Gotta love that inheritance stuff. I just have a base object with the functions defined to do what all objects in the engine will be able to do - move, rotate, delta move, delta rotate, etc. I'll probably not overload the base functions and just use type casting, as needed. Not the most elegant approach, but it maintains the flexibility to add other API support to it.
Will 'new' do what I need it to? EX:
float *a;
new a;
for(j=0; j<10; j++)
{
a[j][0]=0;
a[j][1]=0;
a[j][2]=0;
}
C++ won't smoke my OS for trying the above will it?
There are some rather neat looking template libraries from a book I would love to intergrate, eventually. It is a rather good random number generator. Unfortunately, I get a headache from trying to understand all the notations for templates, but I can figure out what the template does.
Oh yeah, the reason I am so interested in being able to pass and return objects is that it will save me a headache in designing the base model class. It will save me the pains of having to intergrate the drawing routines with the actual object. I'd rather just have the graphics class do that - it keeps things modular. Good, now I just need to pass the camera object(for dirty visibility checks) and the model object to draw the model.
Well, thanks for the info!
Edited by - Whirlwind on May 21, 2000 8:25:37 PM
quote:
Will 'new' do what I need it to? EX:
float *a;
new a;
for(j=0; j<10; j++)
{
a[j][0]=0;
a[j][1]=0;
a[j][2]=0;
}
C++ won't smoke my OS for trying the above will it?
yes it will. you should read up on the new operator before using it.
http://msdn.microsoft.com/library/devprods/vs6/visualc/vccore/_langref_new_operator.htm
Edited by - iwasbiggs on May 21, 2000 8:54:12 PM
___________________________Freeware development:ruinedsoft.com
Is it a really bad thing to declare a variable and use it like an array? Yes - if you declare a variable, it''s pointer may poitn to any place in memory. Though if you declare it as a variable (not an array) and use it like an array, you can end up overwriting other variables, arrays, or even program code itself. You''ll probably crash your system. Use NEW. It''s the C++ way of declaring dynamic arrays.
Can you return things without using return? Sure. Make a pointer to the parameter and the procedure will change it for you. Like this:
void ConvPnt (Point3D &Pnt) {
// Calculations here
// If you change Pnt, you will change the calling argument
}
Hope this helps.
-Ender Wiggin
Can you return things without using return? Sure. Make a pointer to the parameter and the procedure will change it for you. Like this:
void ConvPnt (Point3D &Pnt) {
// Calculations here
// If you change Pnt, you will change the calling argument
}
Hope this helps.
-Ender Wiggin
May 22, 2000 08:48 AM
quote: Original post by iwasbiggs
Will ''new'' do what I need it to? EX:
float *a;
new a;
for(j=0; j<10; j++)
{
a[j][0]=0;
a[j][1]=0;
a[j][2]=0;
}
C++ won''t smoke my OS for trying the above will it?
yes it will. you should read up on the new operator before using it.
http://msdn.microsoft.com/library/devprods/vs6/visualc/vccore/_langref_new_operator.htm
Good stuff. I need to find my crib notes for C++….
Anyways, if I change the ''new a'' to ''a = new int[numverts][3];'' that should work, cool. I really need to find my quick ref for C++.
Thanks again, all.
new() is basically a type-safe version of malloc(), and because it is type-safe it allows the type''s constructor to be called. Same thing with delete()/free(). You should always use new() and delete() in C++ as mixing them with malloc()/free() calls will give you errors, plus new() and delete() use much cleaner syntax. Remember that if you allocate something with new[]() (an array), then you must deallocate it with delete[](). Here is an example for new and delete:
Note that you don''t have to remember how many elements you allocated -- delete[]() takes care of that for you. Also, if you use delete() on an array, only the first element is deleted. If you use delete[]() on an array, it will call the destructors of all the array elements. Note also that if the memory allocation in new() (or new[]()) fails, it will return a NULL pointer.
Whirlwind: You can return any type of object, even user defined classes, all the way down to MS Visual C++ 1.0 (and mostly likely MS C++ 1.0, which is OLD). Are you talking about a C-style string or a std::string (STL)? C-style strings are just char[], and the string''s name is just a char* there''s no reason why you can''t return it. Perhaps VC++ 5.0 has some issues with STL, but I don''t know why it wouldn''t let you return a std::string. Any class that is legal to write is considered a type, and can be returned just like the intrinsic types. You can pass by value or reference, and return by value or reference. There is no reason why this wouldn''t work:
- null_pointer
Sabre Multimedia
int main()
{
// allocate one int, internally takes care of sizeof() etc.
int* p = new int;
// set the value
*p = 5;
// deallocate one int
delete p;
// allocate an array of 5 ints
p = new int[5];
// set the values
for( int j=0; j<5; j++ )
{
p[j] = j;
}
// deallocate an array of 5 ints
delete[] p;
}
Note that you don''t have to remember how many elements you allocated -- delete[]() takes care of that for you. Also, if you use delete() on an array, only the first element is deleted. If you use delete[]() on an array, it will call the destructors of all the array elements. Note also that if the memory allocation in new() (or new[]()) fails, it will return a NULL pointer.
Whirlwind: You can return any type of object, even user defined classes, all the way down to MS Visual C++ 1.0 (and mostly likely MS C++ 1.0, which is OLD). Are you talking about a C-style string or a std::string (STL)? C-style strings are just char[], and the string''s name is just a char* there''s no reason why you can''t return it. Perhaps VC++ 5.0 has some issues with STL, but I don''t know why it wouldn''t let you return a std::string. Any class that is legal to write is considered a type, and can be returned just like the intrinsic types. You can pass by value or reference, and return by value or reference. There is no reason why this wouldn''t work:
class my_class_type
{
};
my_class_type& do_something(my_class_type& example_object)
{
return example_object;
}
int main()
{
// declare an instance
my_class_type my_object;
// pass it to a function, and the function can return it
do_something(my_object);
}
- null_pointer
Sabre Multimedia
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement