memory Management

Pages: 12
What do you prefer. Besides this being a c++ forum.Do you prefer managing memory allocation by yourself, or the other way round.Was doing c++ in a c# lab.These guys jus started talking about how useless it is, to use pointers.So, it got me thinking, are they hating because its c++, or is it just the truth.
It's hate.
Pointers are awesome.
I love them.
It gives you the lowlevel access every programmer should deserve.

It gives you the lowlevel access every programmer should deserve.


This is a very common misconception spread among C++ programmers.

It is *not* the lowest level and it is totally different from how real hardware operates - memory management in C and C++ is already an abstraction. Not as high level as GC, but still quite high level (let's call it mid-level). In some rare cases (embedded stuff) it might be implemented directly by hardware, but more often there is some quite large level of indirection you have no control over.

For example, in any modern OS, you still have no control over how the OS is allocating real memory for you, nor you have control over when memory is released. You have no full control over cache behaviour (some OSes give some limited tools to help, e.g. cache prefetch hints, but they are like helping Java do a GC by manually calling System.gc()). A contiguous block of memory in C++ you sequentially iterate using a pointer, might not be contiguous in RAM, it may even not be in RAM. This is all only slightly closer to hardware than automatic memory management and it is still not deterministic and gives no guarantee about pause times (dereferencing a pointer may take arbitrary long time and trigger thousands of operations to execute, just as doing a GC).

Sure, there are advantages... sometimes.

BTW: C# does have pointers, just as C++ and Java (called references, but Java's references are like shared_ptr in C++ only better). So I don't understand what they mean by "pointers are useless"? They are probably using them all the time.
Last edited on
closed account (z05DSL3A)
EssGeEich wrote:
It gives you the lowlevel access every programmer should deserve.
rapidcoder wrote:
This is a very common misconception spread among C++ programmers.
It is *not* the lowest level and it is totally different from how real hardware operates...

He didn't say it was the lowest level.
Last edited on
I'm with rapidcoder on this one, as he made many of the points I would have made if I could find my words. So, cave lector...

I don't understand why the concept of "power" is so often interlinked with that of "pointer".

So all right, pointers make polymorphism work.

Function pointers have all but been replaced by the lambda functions. Do you use pointers to hand optimize your C++ code? You should probably be using references instead.

Then what is left, that you can use pointers for? Corruption of memory? Leaking it?

If you heavily rely on low-level mechanism like the dumb pointer, perhaps you'd be better off using C instead, which is dumb enough to make their usage safer (i.e. no exceptions).

I'm willing to change my opinion if you show how it is wrong.
What do you prefer. Besides this being a c++ forum.Do you prefer managing memory allocation by yourself, or the other way round

Define "yourself" and "the other way"? Memory allocation is performed automatically when a static or automatic object is constructed, and by the allocator-aware classes and functions (such as std::map or std::packaged_task) through the allocators I supply.
It's neither hate or truth, those people do not know C# well enough to know that it allows the use of pointers. Just because you have to jump through hoops to use them, and they don't play well with the other C# keywords doesn't mean that they don't exist. The fact that they're included at all after all of the effort to actively discourage them proves that they have a very important place in any programming language.
Nobody saw EssGeEich's sarcasm?
closed account (3qX21hU5)
Like others have stated C# itself has pointers so I don't see why the C# students are ripping on them. They are part of the unsafe code context.

Now granted they are rarely used in C# but they do have their uses like when dealing with COM you will sometimes need them and in performance critical code along with other edge cases.

Pointers are just another tool that you have in your toolbox. Even if you hardly ever use them it is better to have them and not need it then to need it and not have them in my opinion. The more options you have to solve your problem the better.
Last edited on
I think it's not whether a language has pointers but whether a language has RAII. Garbage collection only handles memory, and memory is just one of many resources you can forget to release. Pointers or not, the determining factor is RAII.
closed account (D80DSL3A)
Catfish4 wrote:
Then what is left, that you can use pointers for? Corruption of memory? Leaking it?

A big one! The ability to vary which variable you are working with.

A pointer assignment in one section of code can determine which variable you are performing an operation upon in another section of code.

EDIT: In my brief study of C# I saw that pointers are not needed nearly as much because references are re-bindable (ie. you can change which variable they refer to, unlike in C++). When I saw that I had to try writing a basic linked list class, using references instead of pointers. No problem!
Last edited on

A pointer assignment in one section of code can determine which variable you are performing an operation upon in another section of code.


This is what references are for, not pointers. But again - C#/Java references are like C++ pointers, only the name is different. So C#/Java have pointers. Oh, and ther is a NullPointerException. You see?


I think it's not whether a language has pointers but whether a language has RAII. Garbage collection only handles memory, and memory is just one of many resources you can forget to release. Pointers or not, the determining factor is RAII.


The real purpose of GC is not guarding against simple forgetting to free memory (yeah, it is nice, but it is not a problem if you follow some rules and good static analysis tools catch such bugs very easily). The real purpose of GC is allowing more flexible design, where you are not constrained by object ownership so you can concentrate more on the logic of the program than bookkeeping.

Freeing non-shared memory/resources is just as easy with or without RAII. There is try-with-resources / try-finally construct in C# and in Java, and forgetting to use it is signalled enough visibly that it is never a problem.
Last edited on
closed account (D80DSL3A)
This is what references are for, not pointers.
In some cases perhaps, but you can't change which variable is referenced by a reference. This is a large limitation.
Try writing a linked list in C++ using references to nodes instead of pointers to nodes. There's no way to change the link assignments.
The difference between a reference and a pointer is like the difference between
const int x; and int x;. In the former, you can't vary what's stored in x.
I love pointers.

In some cases perhaps, but you can't change which variable is referenced by a reference.


Eh, what?! C# and Java references are rebindable. They are like pointers, only name is different.

Java/C# references != C++ references
Java/C# references (almost ==) C++ shared_ptr
Last edited on
closed account (D80DSL3A)
Sorry rapidcoder, I was writing about pointers vs. references in C++ only.
I recognized the greater usefulness of references in C# in my 1st post.
closed account (z05DSL3A)
When you talk about C# references, what are you talking about?

In the following, What are references, where is the object stored (heap or stack) can you store the objects where you want and what are the values of x for the four objects?
1
2
3
4
5
6
7
8
9
10
11
12
SomeType t1 = new SomeType();
OtherType t2 = new OtherType();

t1.x = 5;
t2.x = 5;

SomeType t3 = t1;
OtherType t4 = t2;

t1.x = 8;
t2.x = 8;
NB: code is C#
Last edited on
closed account (3qX21hU5)
Grey Wolf wrote:
When you talk about C# references, what are you talking about?

In the following, What are references, where is the object stored (heap or stack) can you store the objects where you want and what are the values of x for the four objects?


I'm not sure if that was meant as a question or not but I will try and answer it anyways though I am limited with my C# knowledge so anyone more experienced please feel free to correct me.

In you example it would depend on whether or not SomeType and OtherType are value types or reference types.

All types in C# fall into four categories Value Types, Reference Types, Generic type parameters and Pointer Types.

I won't touch on the last two but will focus on Value and Reference types.


Value types comprise most of the built-in types (All numeric types, the char type, bool) and also enum's and struct's.

Whereas Reference types comprise all class's, array's, delegate's and interface types.


Value types are just like variable in C++ they hold the content.

Whereas reference types are a bit more complex. Reference types have two parts a Object and a reference to that object. By that I mean the content of a reference type is a reference to the object that contains the value.

So in the example above lets say that t1 and t2 are reference types. Then the code below would not create new objects for t3 and t4 instead it would just create a reference to the t1 and t2 objects respectively. So any changes that are made with t3 and t4 will affect t1 and t2 directly.

1
2
SomeType t3 = t1;
OtherType t4 = t2;


As for the memory question I am not to familiar with it but will try and give some information on it, though I can't provide much. But the link below has some good information on it.

1. A Reference Type always goes on the Heap - easy enough, right?

2. Value Types and Pointers always go where they were declared. This is a little more complex and needs a bit more understanding of how the Stack works to figure out where "things" are declared.

http://www.c-sharpcorner.com/UploadFile/rmcochran/csharp_memory01122006130034PM/csharp_memory.aspx


Last edited on
closed account (9wqjE3v7)
It is *not* the lowest level and it is totally different from how real hardware operates.

You're right, operations such as new[] and delete[] are system calls through an ABI which are included in the executable. On a basic level, you are requesting the memory management 'module' of the kernel to scan for a suitable block size in the heap of the process which is marked as free and a suitable size, which of course like rapid coder mentioned is time consuming.

+ Why do some people think the only data segments used are the stack and the heap? Where do you think your global variables that have an extent throughout the whole program are stored?
closed account (z05DSL3A)
Zereo,
Sorry, it was kind of rhetorical and more than likely miss-worded . I don't tend to refer to C# as having 'references' but that it has 'reference types'. As rapidcoder said, a C++ reference =/= a C# 'reference' but I was a bit curious as to the concept that people attribute to a C# reference type.

I'm not entirely sure what my own mental image of value/reference types is. I kind of see them as an object with its state separated. A value type has its state stored on the stack and the state is not shared and a reference type has its state stored on the heap and can be shared.

As for the code sample I thought that I had put
1
2
3
class SomeType{public Int32 x;}
struct OtherType {public Int32 x;}
//... 
at the top. (again sorry for the confusion)
The idea of the code was to gauge what people thought was a reference in the code. As t1 and t2 are used the same but end up with different results later on, is this good. And as the user of the type you have no say in where the state is stored.


You're right, operations such as new[] and delete[] are system calls through an ABI which are included in the executable. On a basic level, you are requesting the memory management 'module' of the kernel to scan for a suitable block size in the heap of the process which is marked as free and a suitable size, which of course like rapid coder mentioned is time consuming.


No, you're wrong. If we're talking Windows, Linux or Unix, they *are not* direct system calls. They are userland routines. OS does not give ability to allocate memory in 3-byte blocks, like new allows.
Pages: 12