Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C++ use pointers? [closed]

Tags:

c++

pointers

Why does C++ need and use pointers? I know they add power to the language but they make it a lot harder to understand for beginners. Languages like F#, Java, Ruby, Python, Lua, etc. get by just fine without them, and they're quite powerful.

like image 922
RCIX Avatar asked Jul 04 '09 04:07

RCIX


People also ask

Why do we use pointers in C at all?

Pointers save memory space. Execution time with pointers is faster because data are manipulated with the address, that is, direct access to memory location. Memory is accessed efficiently with the pointers. The pointer assigns and releases the memory as well.

Can you use C without pointers?

“Pointer-less C” is very possible in all compilers today, using arrays, unions, and structures, a new compiler is not required.

Are pointers still used in C++?

A pointer is a variable that stores the memory address of an object. Pointers are used extensively in both C and C++ for three main purposes: to allocate new objects on the heap, to pass functions to other functions.

What is the use of pointers Why do we use it?

Pointers are used to store and manage the addresses of dynamically allocated blocks of memory. Such blocks are used to store data objects or arrays of objects. Most structured and object-oriented languages provide an area of memory, called the heap or free store, from which objects are dynamically allocated.


1 Answers

All other languages you mention (and you forgot, at least, Java!!!), while each very useful and usable, don't let you get anywhere as close to the machine as C++ (and C) allow: simply put, all of those languages impose on you a higher level of abstraction... which may mostly be fine but will occasionally get in your way.

C++ is a bigger and more complicated language because it allows programming at really low levels of abstraction (very close to the machine) AND at pretty high levels (close to many of the languages you mention) within the same language, indeed within the same source files.

Beginners are well advised to stay FAR away from this amount of power and (inevitably) complication -- but not every programmer is a beginner, and not every bit of code needs (or, actually, can at all stand!-) being within an environment that has "let's protect the poor shmucks from themselves" as a major design goal!-)

like image 140
Alex Martelli Avatar answered Oct 28 '22 15:10

Alex Martelli