Programming: Pointers and Memory Allocation In C++

Post here about scripting and programming for HaloPC (audio, network, ai, etc.)
Post Reply
kaptainkommie




Wordewatician 500

Posts: 732
Joined: Wed Nov 26, 2003 11:59 pm
Location: Raleigh, NC, USA

Programming: Pointers and Memory Allocation In C++

Post by kaptainkommie »

C++ Tutorial: Pointers and Memory Allocation

Pointers:
A pointer is a variable that holds the address of a location in memory.
Two operators are needed when working with pointers are the dereferencing operator (*) and the address-of operator (&). The dereferencing operator is used at declaration to let the compiler know that you want to declare a pointer. Otherwise the dereferencing operator is used to return the value in the variable that is being pointed to. The address-of operator is used to return a variable
User avatar
Locke




Socialist Advisor Translator

Posts: 2225
Joined: Fri Aug 06, 2004 1:42 am
Location: Woot.com
Contact:

Post by Locke »

Nice tut.It adds flavor to this place.So people can choose C++ or vb.I like vb cause its easier,but c++ cause its better.
Image
Feel free to send me an instant message if you need anything.
smellylobster





Posts: 104
Joined: Sun Oct 10, 2004 5:12 pm

Post by smellylobster »

I read two chapters on this and your tutorial is a good job in summing this in a short amount of space. :D
Spartan III




Wordewatician 500

Posts: 663
Joined: Tue Nov 11, 2003 7:07 am
Location: Manchester, United Kingdom

Post by Spartan III »

Nice job, but i'm still learning VB anyway.
Image
mojo





Posts: 30
Joined: Sun Apr 04, 2004 5:09 pm

Post by mojo »

speaking of vb and c++ wich one do u think i should get if the only knowledge i have of programming is yomamas 3 tuts and jsut a little more in vb but i want sumthing thats capable of a lot more stuff, should i stick with vb or get c++
User avatar
Locke




Socialist Advisor Translator

Posts: 2225
Joined: Fri Aug 06, 2004 1:42 am
Location: Woot.com
Contact:

Post by Locke »

mojo wrote:speaking of vb and c++ wich one do u think i should get if the only knowledge i have of programming is yomamas 3 tuts and jsut a little more in vb but i want sumthing thats capable of a lot more stuff, should i stick with vb or get c++
It depends on what you want. VB is visual, and is easier to design layouts for it, objects, ect... But C++ you can do alot more stuff.
Image
Feel free to send me an instant message if you need anything.
Thebirthdayhat





Posts: 94
Joined: Mon Nov 15, 2004 7:01 pm
Location: over there

Post by Thebirthdayhat »

tahnks i think ill go wit c++ any1 one of like a free trial or sumthing i can get cause i have no money right now
Signature exceeded 3 lines of text.
User avatar
Flying Poo




Articulatist 50

Posts: 101
Joined: Sat May 15, 2004 5:31 am
Location: moved to containment

Post by Flying Poo »

Is xbox programming done in C or C++?
My Sig
User avatar
Locke




Socialist Advisor Translator

Posts: 2225
Joined: Fri Aug 06, 2004 1:42 am
Location: Woot.com
Contact:

Post by Locke »

Xbox games are made in C++, becuase you need a game engine. So the xbox was probably done in C++
Image
Feel free to send me an instant message if you need anything.
User avatar
ExileLord




Socialist Snitch! Mad Hatter Coagulator
Advisor Orb

Posts: 1118
Joined: Fri Sep 10, 2004 11:06 am
Location: Bourbonnais, Illinois

Post by ExileLord »

Yeah I'm still having trouble understanding this :? and i know a little programming.
Image
If there is a problem on the forums, message me through IRC, AIM, or through PM.
Why We Mod - Completed
kyboren





Posts: 58
Joined: Thu Feb 12, 2004 8:49 pm

Post by kyboren »

You should always check to make sure the pointer returned by new is not NULL.
No. The current default C++ standard says that on error, new will throw the std::bad_alloc exception. You can do some tricks to change that, but keep in mind that you should test for the exception.

If anyone here doesn't know how:

Code: Select all

char *newBuffer = NULL;

try{
     newBuffer = new char[500];
}
catch(std::bad_alloc){
    //display error message, clean up code, etc. etc.
}
//...

delete [] newBuffer;
To make it just return a NULL ptr:

Code: Select all

#include <new>
//...
char *newBuffer = NULL;

newBuffer = new (std::nothrow) char[500];

if(newBuffer == NULL)
{
    //message, cleanup, etc.
}

//...

delete [] newBuffer;
You can also use the C allocation functions in C++. However, you can't mix and match... i.e. you can't malloc() and then free that memory with delete.

EDIT:
Oh, and while you should always delete (or free()) the memory you allocate, it doesn't just delete RAM or anything. At the very worst, that RAM will be lost until a restart. However, most good memory managers (like those included with Windows, Unix and most of its variants, and Linux) will free up the memory on program termination. Don't use that as an excuse, though... memory leaks are never a good thing. They're bad for the system, are considered bad programming, and are generally not looked upon as "OK".
mopopo





Posts: 84
Joined: Wed Mar 23, 2005 5:58 pm
Contact:

Post by mopopo »

can someone tell me how to run cpp files. i have a windows 2000 and i have a cpp application that helps me make cpp files but i cant run them. please tell me what program i can uset o make them work.
kyboren





Posts: 58
Joined: Thu Feb 12, 2004 8:49 pm

Post by kyboren »

You need a compiler and linker. Dev-C++ from http://bloodshed.net is free and easy to use.
kyboren





Posts: 58
Joined: Thu Feb 12, 2004 8:49 pm

Post by kyboren »

Oh, and if you want to not worry about memory leaks, deallocating memory, etc. you can use smart pointers and STL containers.

For an automatically-resizing, heap-allocated array lookalike, use std::deque

If you just want to have a single object allocated on the heap and not have to worry about allocation and such, you can write a simple smart pointer class. The basic smart ptr class is templated, has a pointer to a value as a private member, has public functions to access the variable's value (this is a great use of smart pointers.. you don't have to worry about dereferencing and such), and has a constructor and destructor which allocate and deallocate the memory for the object (respectively).
TfAv1228




Miner Logistician Droplet

Posts: 276
Joined: Sun Sep 05, 2004 4:40 pm

Post by TfAv1228 »

Yomama wrote:
mojo wrote:speaking of vb and c++ wich one do u think i should get if the only knowledge i have of programming is yomamas 3 tuts and jsut a little more in vb but i want sumthing thats capable of a lot more stuff, should i stick with vb or get c++
It depends on what you want. VB is visual, and is easier to design layouts for it, objects, ect... But C++ you can do alot more stuff.
you can do both use vb6 for you ui and basic stuff and for the complicated stuff and stuff that requires more speed make a c++ dll i recently did that with map encryption to get started using c++
Sig breaks rules, read the rules before reposting.
Post Reply