Page 1 of 1

Clarifying a few things about c++

Posted: Thu Jan 14, 2010 9:36 am
by Live-Dimension
Hey all.

I know I could probably find out all of this if I sat down and read a big book of learning c++, but I've always found that I barely learn from it. This is partly because I know enough from other languages that I tend to skip 90% of it, partly because it completely zaps my motivation to program in the first place, and finally because I usually do a good job of picking things up myself. These days I find I only really learn by going in head-first and doing something, rather then going through tutorials/books, etc. So - I HIGHLY appreciate everyone's time with this.

I've got some experience over a wide range of languages but for whatever-reason I've never been 100% sold on what I was using, mostly due to the lack of flexibility in said language. So for the up-teenth time I've tried c++ and finally after a round of c# and its namespaces I'm getting a semi-decent grasp of it. There's a few things about c++ that I've never dealt with before that I'm having difficult to understand (mostly conceptually). As such I'm making a small simple game kinda like missile command but a bit more..... modern - pretty much a 3D-rendered, 2D plane version of it, defending a planet, with enemies coming from all directions.

Well, to start, I know how to use a pointer, and what they do, but coming from a C#/AS background, I'm finding it difficult to know when to actually use them. I never seem to use them at all, really - all I seem to know is new classes should be pointed to? and so should constant strings? What's the effect of using a pointer over not? Faster? Less memory? When do I use them? Also, I know what a pointer is ( * ) (points to a memory location) and I know what ( & ) does (translates a memory location back to a value) but what is it called? (the &).

And what the hell is the deal with (const <stringtype> *) being shown everywhere for strings? I mean, how can you have a constant variable that you can remove? I assume that I'm reading it wrong, but rather it's saying that the variable's value is a constant............. which it isn't always is? What's the deal with const?

To start with I've made everything that should only appear/happen once as static as I'm used to writing this way, but I want to learn singletons and do it properly (also so I can use constructors/destructors properly instead of Init()/Shutdown()). I've looked a bit at how to do them them but I'm not really picking up on 'how to' do them.

What are some peoples favourite places on where to find winapi functions? The MSDN or is there some other perhaps easier/better places out there?

A question for ORGE users -
And finally (for now heh :lol:) ascii/unicode. I haven't delved into unicode much, though I want to, it seems like orge is almost pure-ascii based? In my experiences it hasn't dealt well with unicode. I'd assume by now it'd fully support unicode where it is needed. I don't understand why we can't just work in a fully unicode orge/codeing enviroment. In my mind it'd make things so much simpler.

Finally, I've always had troubles trying to work out dynamic controls, the concept, pseudo-code, if you will. But that can wait for another topic when I've done a bit more work on this fantastical terrible engine of fail I'm working on. XD

Big thanks for everyone who takes the time to read and comment.

Re: Clarifying a few things about c++

Posted: Thu Jan 14, 2010 11:02 am
by avansc
I'll just be answering the pointer part.

okay so first thing thats wrong. & does not give you the value of the pointer, & is a reference of a var.

exp.

int a = 10;
int *pInt = &a;

&a returns the mem location where var "a" starts.

if you did printf("%d\n", *pInt) you would get 10. *pInt is called DEreferencing the var. it give you the value where pointer "p" is pointing to.

now for the use.

this is kinda a broad subject and can only be learnt by doing. pointers can be faster, but you need to know how to use them. they can also save memory if you use them properly, they dont make memory smaller. memory has to be defined somewhere, but you can have multiple pointers to the same memory instead of having multiple objects in memory.

here is just a small example. this is not really functional, but just show some simple pointer inc.

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char * const argv[])
{
	char *name = "avansc";
	do{
		printf("%c", *name);
	} while(*name++);
	return 0;
}

Re: Clarifying a few things about c++

Posted: Thu Jan 14, 2010 11:04 am
by qpHalcy0n
This is one of the primary gripes from your systems programmer types about managed code like C# or Java which is that memory localization and allocation is almost completely abstracted from you. Of course in C# there is the option to go unsafe and write out regular ole' C++ style heap allocation routines, but that is not recommended. With C++ there are numerous ways to "get memory".

So to start, "new" operates in much the same way as C#, however in C# you can use new as a modifier to hide inherited members from other classes, in C++ you can not do this. In terms of memory its just the same, its a heap allocation. Heap allocation is fairly fast and the memory persists until you explicitly delete/delete[] it (no garbage collectors). So if you fail to delete it, it's a leak. Now why heap allocation? Really, in C++ there's only two good reasons: 1) persistence/scope 2) size. First, if you need a block of memory to persist BEYOND the point in which it was created, new it. Secondly, if it's a massive chunk of memory that you need, new it. The reason "new" is so important in C# where C++ doesnt particularly care is because in C# it's illegal to use uninitialized values. C++ won't complain, you just won't get desired results. Thusly, you can have a class called "crap" and say crap p; If you try to use "p" in C# it will complain that it hasn't been initialized. C++ won't complain, but you'll have a bunch of garbage in "p". The new operator, like C# also invokes the constructor, if one was not supplied, a default will be given (don't count on this, however....write constructors). Similarly delete invokes destructors.

That said, there is an alternate method of grabbing memory which is called stack allocation. This is your typical int stuff[50]; notation. This memory exists in a different location and uses the stack set aside for that function space. It's limitations are: 1) persistence/scope 2) size. First, it goes bye-bye when it goes out of scope. Secondly, you're limited to the amount of memory you can address there. Stack allocation tends to be faster because they're all near references to memory. Eg: it's just right there, instead of WAY over there. So in general, if you only need a little bit "for right now", prefer stack allocation.

So the pointer notation (*) is just that. It's JUST a notation. The underlying CONCEPT...is what I just said above. The pointer just says "memory is here". Thats it.....that's all, there's not a damn thing else that it does, its just a placeholder for memory. No mystique, no behind-the-scenes, it's not a "dont bother the man behind the curtain". So if you are RECEIVING a memory block....the receiver should be.......a pointer. "new" returns a memory address. Thusly, the type on the left hand side is ....a pointer. If you want to pass memory around from one place to another...you pass....pointers. Its not a speed versus X kindof thing either. This is just what you must do to handle memory. Where ppl tend to get confused is in the & (address of) operator. You can pass a THING to something else "by reference", which is essentially an implicit pointer to the caller. Or you can take a THING's address by saying int* p = &thing; So, since & is the address of, then the left hand side should expect to RECEIVE a memory address, which means.......pointer. To dereference a pointer you use (*). This means, to take the value of what the pointer points to.

Code: Select all

int p = 5;     // P holds the value of 5
int* ptr = &p;   // ptr holds 0x488853BA (some memory address)  p is AT 0x488853BA
int stuff = *ptr;   // stuff holds 5, because what ptr points to holds the value of 5
I'm not sure what you mean by (const <stringtype> *). I'm not exactly sure what "stringtype" is. You'll have to clarify that one.

OGRE is agnostic to unicode/ASCII. It doesn't care. It uses what you feed it, so in that regard its a user interface ONLY issue. So if you setup your project build options to do UNICODE only....well, there you go....thats it. I'm not sure what kind of GUI subsystem they're using these days, if its still CEGUI, then yes, CEGUI can display UNICODE glyphs.

Dynamic controls? As opposed to controls that don't move? ;]

Re: Clarifying a few things about c++

Posted: Thu Jan 14, 2010 4:24 pm
by Live-Dimension
avansc wrote:I'll just be answering the pointer part.

okay so first thing thats wrong. & does not give you the value of the pointer, & is a reference of a var.
I was pretty much saying & did the same as a pointer! Opps!
avansc wrote:this is kinda a broad subject and can only be learnt by doing. pointers can be faster, but you need to know how to use them. they can also save memory if you use them properly, they dont make memory smaller. memory has to be defined somewhere, but you can have multiple pointers to the same memory instead of having multiple objects in memory.
That's pretty much what I figured, I just wanted to make sure. And I meant make memory usage smaller for the program as a whole...... It was 2am when I writ this!

I think I understand everything else about pointers though. Thanks!
qpHalcy0n wrote:Heap allocation is fairly fast and the memory persists until you explicitly delete/delete[]
Do I only need to delete on each "new" statement, or will there be others times too?

qpHalcy0n wrote:I'm not sure what you mean by (const <stringtype> *). I'm not exactly sure what "stringtype" is. You'll have to clarify that one.
edit:
Consider this which I found somewhere out there. What's the deal with const in these situations?
1) Strictly ANSI (Cannot use with UNICODE)

1. char - ANSI char.
2. LPSTR - Long pointer to ANSI string.
3. LPCSTR - Long pointer to const ANSI string.
4. std::string - Ansi string.
5. "hello" - An ansi string.


2) Strictly UNICODE (Cannot use with ANSI)

1. wchar_t - UNICODE char.
2. LPWSTR - Long pointer to UNICODE string.
3. LPCWSTR - Long pointer to const UNICODE string.
4. std::wstring - UNICODE string.
5. L"hello" - An Unicode string
qpHalcy0n wrote:OGRE is agnostic to unicode/ASCII. It doesn't care. It uses what you feed it, so in that regard its a user interface ONLY issue. So if you setup your project build options to do UNICODE only....well, there you go....thats it. I'm not sure what kind of GUI subsystem they're using these days, if its still CEGUI, then yes, CEGUI can display UNICODE glyphs.
IIRC, the project is set to default ASCII, and orge is probably #defineing to use ascii because of that. I'll change the project to unicode and see what happens.

By dynamic controls I mean user-changeable controls, haha :P IE, "Up instead of W" for someone like my mum o.O

edit: Decided to just sit on my ass, stop my whining and bitching and go get a book and at least try to get through it. c++ is just far more complicated then anything I've tried before, even though I already understand a good deal of it, but the online tuts are just not cutting it.

Re: Clarifying a few things about c++

Posted: Thu Jan 21, 2010 8:32 pm
by Ginto8
Live-Dimension wrote:edit: Decided to just sit on my ass, stop my whining and bitching and go get a book and at least try to get through it. c++ is just far more complicated then anything I've tried before, even though I already understand a good deal of it, but the online tuts are just not cutting it.
Good choice, but you'll also want to do a lot of learning by doing; it allows you to develop your own coding style and determine what parts of a language you like/dislike. For example, with C I like that it's:
  • concise
    does no more than you tell it to do
    does no less than you tell it to do (these 2 are the case with MOST languages)
    it provides a pretty deep understanding of how the computer actually does things
But then there's OOP, which is a bitch to emulate in C, what with everything having to be dynamic, etc. So my coding style in C++ is mainly procedural, but I use OOP when it makes more sense. You may find that OOP is better for you than procedural; who knows? And that is why you need to make some mini-projects of your own instead of only reading some books. Books are like a boat, but you're the one that must trim the sails

Re: Clarifying a few things about c++

Posted: Fri Jan 22, 2010 8:26 am
by Live-Dimension
I know all sorts of languages and am pretty decent with PHP (Procedural) and C# (OOP). I understand how to program and quite well in a few languages. I've just never done anything at such a low level like c++ - it's taking time for me to get wrapped around specific concepts. I just don't learn well with books =\

Re: Clarifying a few things about c++

Posted: Wed Feb 03, 2010 9:42 pm
by eatcomics
Good talk, I've learned things :D