Page 1 of 1

Programming: Pointers and Memory Allocation In C++

Posted: Fri Oct 22, 2004 5:54 am
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

Posted: Fri Oct 22, 2004 6:33 pm
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.

Posted: Sun Oct 24, 2004 1:57 pm
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

Posted: Mon Nov 01, 2004 5:27 am
by Spartan III
Nice job, but i'm still learning VB anyway.

Posted: Sun Nov 14, 2004 9:29 am
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++

Posted: Sun Nov 14, 2004 10:43 am
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.

Posted: Wed Nov 24, 2004 6:26 pm
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

Posted: Sun Dec 12, 2004 9:33 am
by Flying Poo
Is xbox programming done in C or C++?

Posted: Sun Dec 12, 2004 10:04 am
by Locke
Xbox games are made in C++, becuase you need a game engine. So the xbox was probably done in C++

Posted: Sat Feb 19, 2005 5:12 am
by ExileLord
Yeah I'm still having trouble understanding this :? and i know a little programming.

Posted: Wed Mar 16, 2005 6:10 pm
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".

Posted: Fri Mar 25, 2005 1:14 pm
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.

Posted: Fri Mar 25, 2005 3:01 pm
by kyboren
You need a compiler and linker. Dev-C++ from http://bloodshed.net is free and easy to use.

Posted: Mon Apr 11, 2005 6:20 pm
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).

Posted: Wed Oct 12, 2005 9:48 pm
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++