Title: Myriad of questions
Description: ...
Homer - September 25, 2004 02:30 AM (GMT)
Well i started c++ about a week ago, mainly because i'm interested in AI, and a bit of game cracking so i figured this would be a good place to start. coded through about half of Learn c++ in 21 days till i hit the arrays section, and the first half was easy but then all of a sudden it slammed you into making your own string class and creating nodes, which confused the hell out of me being on my third day of programming. so i read through all the rest of the days absorbing about half then moved onto wrox's guide. much easier and things were explained better overall(except inheritance which 21 days did better at). so i'm back on c++ in 21 days again working through the few section that really threw me.
well that's my story, didn't really have to put it, oh and my background for programming is a class of VB like 4 years ago which i hardly remember, and the tiniest bit of java, and i mean tiniest.
so now i have a crapload of questions as tutorial don't usually give the full depth or assume you memorized everything they spit at you. but i'll start simple off the tops of my head as my questions would easily fill a couple pages.
first when using sizeof array/sizeof array[0] works fine, but when i pass an array to a function and then try using it it always gives a, i forget 1 or 0, but of course that's not what i'm looking for. i'm assuming the fix is using pointers? and am curious why i can't do this?
next, what's the difference between the copy constructor and operator overriding the = sign?
when i use a reference, do i really have to test EVERY time to see if there's enough memory? as in creating a pointer checking if it's null then assigning the reference to the pointer(maybe i should have just put the code in...). i mean it's safe, but then you have a useless pointer and some overhead everytime you use a reference. hmm.. well i guess you'd only need 1 pointer to test all fo them, still the overhead. that's what i get for thinking as i type.
thanks for your time. and next time i'll use more code then english if i can.
C-Man - September 25, 2004 07:58 AM (GMT)
In sheer experience i would never recomend reading Learn X in Y days type of
books . I would rather recomend
Thinking in C++ 2nd vol 1/2 ,
The C++ Programming Language 3d /spetial edition
You should go on Amazon and read reviews on c++ books
1) when you pass a array to a function the pointer only gets passed
and all size information is lost
| CODE |
void f(int foo[]) { cout << sizeof foo << '\n'; // theese 2 are cout << sizeof (void *) << '\n'; // allways the same }
|
You should probably pass the size along with the array
Also theres a way to do what you want with templates but i leave that for more
experienced guy's ;)
2)AFAIK you override the copy constructor to define the beheavior of your
class when it's beeing copied with = sign or passed by value
overriding the = sign should only define the beheavior of = operation
and not pass by value operation
3)well you should have posted code but definatly not you have no need to check
if a reference is null couse thats probably imposible if you don't explicitly
do something like this
| CODE |
void f(int & a) { a = 0; }
int main() { f(*(int *)(0)); }
|
also if it's memory alocation your talking about then new and delete take care of
out of memory error's (i'm not 100% but prety sure ;) again for the more experienced guys )
KTC - September 25, 2004 11:50 AM (GMT)
| QUOTE |
| and a bit of game cracking so i figured this would be a good place to start. |
That's something that can't talk about here as it's against invisionfree rules but if you have any C++ problem in general, then we'll be happy to try and help :)
| QUOTE |
| You should go on Amazon and read reviews on c++ books |
Even those reviews can be very wrong. Amazon review's usually written by people who don't know any better than you trying to tell you whether a book is techincally accurate. If you want some help in finding which book is actually really good, have a look at
ACCU Reviews &
CUJ recommendation.
1) One can't pass the array by value into a function in C++ (nor return it...). When you try to do it, an automatic conversion happens & a pointer to the first element is passed. As C-Man have already said, all size information is lost when an array is passed into a function. Neither the function being passed an array or the compiler knows its actual size. One way around it is to for example make the array have a unique termination element. E.g. C character string use the null character. You can make this terminating element one of the function parameter. Another way, like C-Man said, is to pass the size of the array expliciting as one of the function parameter. Yet another way, is to declare the parameter as a reference to an array. When done this way, the array size becomes part of the parameter and argument types.
| CODE |
| void foo( int (&arr)[10] ) { } |
Of course, such a function can only works on a particular size array. (In this case, one of size 10.) To make it flexible, you can make the function a template:
| CODE |
template <typename T, int size> void foo( T (&arr)[size] ) { } |
2) Yes, this part of C++ is bloody confusing, especially when one consider the fact that the copy constructor can be invoke by using = . Basically a constructor is called when something is being defined & initialized, and operator= is used when an object already exist and it's being assigned to.
| CODE |
Object a; Object b = a; // Copy constructor b = a; // b already exist, so operator= |
3) I have no idea what you're trying to say here. I don't see why one under normal circumstance (any circumstance...) need to check reference for memory ?? :blink:
Homer - September 26, 2004 12:46 AM (GMT)
doh!, i just wrote a few questions and had them deleted when i tried to preview them and hit back on the web browser to edit :angry: .
but thanks for the quick replies, the tips are great. as i'm too lazy to rewrite all that.... i'll just clarify my third question.
int *pint = new int;
if(pint != NULL)
int &rint = *pint;
just seems that this creates a bit of everhead everytime you want to use a reference to something on the free store.
ok one more i guess.
Cat Family = new Cat[500];
Cat *pCat = 0;
for(int i = 0; i < 500; i++)
{
pCat = new Cat;// why do i need to use new again? aren't the cat objects already created on the free store? this line would make perfect sense to me if it was an array of pointers to cat objects. to me just pCat = Cat[i] makes more sense. and the rest of the program. would be edited if i did it my way.
pCat->SetAge(1);
Family[i] = *pCat;
delete pCat;
}
yet again thanks for the help.
C-Man - September 26, 2004 05:47 AM (GMT)
Well thats not the point for using a reference , references are mostly used for
parameter passing and returning data . And as i told you new exits the program
with runtime error whne it's out of memory (afaik) but i realy doubd you'll run
out of memory on windows ;)
(s*** didn't notice that :unsure: )
| CODE |
Cat * Family = new Cat[500];
|
this creates all the cats u ever need ;)
| CODE |
for(int i = 0; i < 500; i++) { pCat = new Cat;// why do i need to use new again? aren't the cat objects already created on the free store? this line would make perfect sense to me if it was an array of pointers to cat objects. to me just pCat = Cat[i] makes more sense. and the rest of the program. would be edited if i did it my way. pCat->SetAge(1); Family[i] = *pCat; delete pCat; }
|
and this actualy leeks memory ;)
FrozenKnight - September 26, 2004 08:31 AM (GMT)
| QUOTE |
| and a bit of game cracking |
if your intrested in cracking all i'll say is your studying the wrong language. C++ is a programming language not a cracking language ;)
| CODE |
Cat Family = new Cat[500]; Cat *pCat = 0;
for(int i = 0; i < 500; i++) { pCat = new Cat;// why do i need to use new again? aren't the cat objects already created on the free store? this line would make perfect sense to me if it was an array of pointers to cat objects. to me just pCat = Cat[i] makes more sense. and the rest of the program. would be edited if i did it my way. pCat->SetAge(1); Family[i] = *pCat; delete pCat; } |
i'd change this to
| CODE |
Cat *Family = new Cat[500]; Cat *pCat = 0;
for(int i = 0; i < 500; i++) { pCat = Family[i]; pCat->SetAge(1); } |
almost this whole thing was done wrong. you had the right idea, except you had your Family as a class not a class pointer. so basically you were redefining your Family each time you were moving through your refrences. and in the end you left 499 Cat structures allocated with no way to free them. (i hope your cat structure was a small one :))
Homer - September 27, 2004 10:56 PM (GMT)
thanks again for the quick replies. and i realized last post this was in the wrong section but that got deleted... so if someone wants to move it?
so then i don't have to check to see if there's memory on the free store everytime i want to use a reference, as in windows won't run out of memory? dumb learn c++ in 21 days.
also
[/CODE]
CAT * Family[500];
int i;
CAT * pCat;
for (i = 0; i < 500; i++)
{
pCat = new CAT;
pCat->SetAge(2*i +1);
Family[i] = pCat;
}
| CODE |
this was from c++ in 21 days, i was almost positive it was wrong, bastards trying to confuse new programmers. but i have thinking is C++ now so after a couple days of rest time to start digging into that.
as for more questions now :)
|
Compiling...
Project.cpp
c:\program files\microsoft visual studio\myprojects\project.cpp(4) : fatal error C1083: Cannot open include file: 'Box.h': No such file or directory
Error executing cl.exe.
Project.exe - 1 error(s), 0 warning(s)[CODE]
i got this error message, and i KNOW i included box.cpp and box.h into the project. they are both in the file view and both have 0 errors when only debugging them. it was copy and pasted from a turorial. hard to give more info then that without putting all the code, so is there any simple reason that this might be happening?
ok now this is a dumb simple question i'm just cirous about. for the simple declaration let's say right after main()
int num = 5;
is there anyway to get rid of this value, delete it like something on the free store? it kind of seems like a memory leak because it will always be there till the end of the program even if you don't need it anymore. is my thinking just off on this?
do you guys use #define ASSERT(x) all over your programs?
and lastly, if you can always pass to a function with pointers and references to avoid overhead, and declare them const if you need to, why would you ever pass by value?
had more questions but i wasted a few hours and coded a bunch to figure them out... tough to do it that way when a 1 minute explanation would get the point. thanks again
FrozenKnight - September 28, 2004 08:28 AM (GMT)
Id your box.h in the same directory as your box.cpp?
also
| CODE |
CAT * Family[500]; int i; CAT * pCat; for (i = 0; i < 500; i++) { pCat = new CAT; pCat->SetAge(2*i +1); Family[i] = pCat; } |
ok to get started
this allocates an array of 500 pointers that are specifyed to be pointers to the class CAT.
this is similar to above but it's just a single pointer.
| CODE |
for (i = 0; i < 500; i++) { pCat = new CAT; pCat->SetAge(2*i +1); Family[i] = pCat; } |
this is pretty self explanitory it's a for loop that loops 500 times.
first it allocates space for a new CAT class and stroes this in pCat.
then it refrences the SetAge() function in the CAT class and sets the age to '2*i +1'
then it stores the newly created and aged CAT into the Family pointer using the i variable specifyed in the for loop definition as the pointer refernce.
When done you should have 500 cats ages varying from 1 to 999 each cat 2 years older than the last.