Programming: Pointers and Memory Allocation In C++
-
- Posts: 732
- Joined: Wed Nov 26, 2003 11:59 pm
- Location: Raleigh, NC, USA
Programming: Pointers and Memory Allocation In C++
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
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
-
- Posts: 104
- Joined: Sun Oct 10, 2004 5:12 pm
-
- Posts: 663
- Joined: Tue Nov 11, 2003 7:07 am
- Location: Manchester, United Kingdom
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.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++
Feel free to send me an instant message if you need anything.
-
- Posts: 94
- Joined: Mon Nov 15, 2004 7:01 pm
- Location: over there
- Flying Poo
- Posts: 101
- Joined: Sat May 15, 2004 5:31 am
- Location: moved to containment
Yeah I'm still having trouble understanding this and i know a little programming.
If there is a problem on the forums, message me through IRC, AIM, or through PM.
Why We Mod - Completed
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.You should always check to make sure the pointer returned by new is not NULL.
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;
Code: Select all
#include <new>
//...
char *newBuffer = NULL;
newBuffer = new (std::nothrow) char[500];
if(newBuffer == NULL)
{
//message, cleanup, etc.
}
//...
delete [] newBuffer;
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".
You need a compiler and linker. Dev-C++ from http://bloodshed.net is free and easy to use.
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).
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).
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++Yomama wrote: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.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++
Sig breaks rules, read the rules before reposting.