Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

C++ pointer vs. non-pointer

Name: Anonymous 2010-05-22 19:54

Sup /prog/

C++, if I make my own class, lets just say class cShitHead

cShitHead dave * new cShitHead( "dave" );
dave->PrintName();

or

cShitHead dave( "dave" );
dave.PrintName();

I myself like the pointer notation better. However, which one is more efficient and better on memory? and why?

Name: Anonymous 2010-05-22 20:03

#1 is better for speed while #2 is better for memory.

Name: Anonymous 2010-05-22 20:04

I'm not surprised a Sepples user isn't bright enough to run his own tests.

Name: Anonymous 2010-05-22 20:07

cShitHead
Oh god, systemsmisused polish notation

Name: Anonymous 2010-05-22 20:13

I'll go with speed. My assumption is when you create a pointer it'll have to create space for the pointer as well as the object, where as the other just creates an object?

Name: Anonymous 2010-05-22 20:14

Micromanaging speed and memory...what is this the 50s?

Name: Anonymous 2010-05-22 20:17

No, but my computer is from 1999.

Name: Anonymous 2010-05-22 20:24

>>7
16 MB of RAM can store over half a million 32-bit pointers. Also, you're an idiot.

Name: Anonymous 2010-05-22 20:32

You do realize that you have been trolled?

Name: Anonymous 2010-05-22 20:38

>>8
So what if I have more than just over half a million pointers? you lose.

Name: Anonymous 2010-05-22 20:41

>>10
virtual memory

Name: >>11 2010-05-22 20:43

Also, you lose.

Name: Anonymous 2010-05-22 20:58

>>11
So what if I go over the over half a million pointers PLUS the amount of pointers I could possibly store in my virtual memory?

You lose.

Name: Anonymous 2010-05-22 21:00

>>13
That means you program poorly.

Name: Anonymous 2010-05-22 21:04

>>13
At that point I hit you over the head with a blunt object and interrogate you until I find out what it is you are actually trying to do. What you are doing now is the equivalent of saying that we should all use Assembly, because every last bit is precious. It's 2010, memory is plentiful. Every last bit is not precious.

Name: Anonymous 2010-05-22 21:17

>>15
My bits are precious.

Name: Anonymous 2010-05-22 21:41

>>15
it's 2010, most people are still stuck with a 4GB RAM limit. until machines with 6GB of RAM cost less than $50, every last bit will still be precious.

Name: Anonymous 2010-05-22 21:45

>>17
Even Win32 isn't stuck with a 4 GB RAM limit anymore, and you're an idiot if you think counting pointers is where you're going to get any significant improvements in memory usage.

Name: Anonymous 2010-05-22 21:46

>>15
What if I live in a desert where silicon isn't plentiful?

Name: Anonymous 2010-05-22 21:47

>>17
Technically speaking, Flash memory is Random Access Memory. My music player has 8 GB of Flash memory on it and I bought it for 45$. You lose.

Name: Anonymous 2010-05-22 21:48

>>19
I love you.

Name: Anonymous 2010-05-22 21:51

>>21
I love you too

Name: Anonymous 2010-05-22 22:24

>>18
it doesn't matter how much win32 supports if my motherboard doesn't support more than 4GB.

>>20
too bad flash memory is slow as fuck.

Name: Anonymous 2010-05-22 22:37

>>23
Now imagine a computer running a Ruby On Rails application while using Flash memory as RAM.

Name: Anonymous 2010-05-22 22:44

>>24
I came

Name: Anonymous 2010-05-23 0:21

In the first case memory is allocated on the HEAP.

In the second case memory is allocated on the STACK.

It's the same amount of memory in both cases.  The memory on the heap needs to be de-allocated manually, while the memory on the stack is released automatically when the function returns.

Name: Anonymous 2010-05-23 0:27

>>26
is sepples really that retarded? does it really specify implementation details like that?

Name: Anonymous 2010-05-23 0:34

>>27
I only barely managed to decipher your comment! Someone has apparently sabotaged your Shift key and left it nigh unreadable!
The answer you are looking for, though, is this: Sepples pretends to be a low-level language, so ``implementation'' details actually aren't.

Name: Anonymous 2010-05-23 0:58

>>28
after a bit of searching, i've determined that the c++ standard only mentions stacks when talking about exception handling and the container in the standard library.

also, your shift key seems to be broken. your post is full of mixed case.

Name: Anonymous 2010-05-23 1:27

>>26,27

I'm a C guy, but in C there is automatic storage and dynamic storage, which people often confuse with "the stack" and "the heap" respectively, because they don't understand that the implementation they are accustomed to (x86 architecture in 99.99% of cases) is not necessarily how it works in every implementation / environment / architecture.

I'm not sure what the C++ standard says about this because I haven't read it, but I imagine that it too has "automatic storage" (which our x86 implementations would put on the stack just like in C) and "dynamic storage" (which comes from malloc(), and in C++'s case, new).

In any case, it is true that the memory for OP's first example is allocated dynamically and needs to be freed manually (by delete in this case), and the 2nd example will be freed when the function it is local to ends.

Name: Anonymous 2010-05-23 1:41

Name: Anonymous 2010-05-23 2:01

it's not confused. automatic storage is default in c/c++ and goes on the stack.  there is a max stack size to be aware of if you're creating a lot of local variables, or doing a lot of recursion.  the keyword 'auto' is implicit in c/c++, so when you have 'int i;' it's equivalent to 'auto int i;'

dynamic storage (malloc, new) allocates memory on the heap, and does require a more cpu because a free block of space needs to be found, assigned, and recorded. yes there are different implementations of dynamic storage, but heap is the most common so let's not get all corner-case here.

Name: Anonymous 2010-05-23 2:05

CORNER-CASE MY ANUS

Name: Anonymous 2010-05-23 2:51

>>33
Get lost.

Name: Anonymous 2010-05-23 2:57

goes on the stack
>>31

Name: Anonymous 2010-05-23 3:02

it's not confused. automatic storage is default in c/c++ and goes on the stack.

See, that is where you are confused.  C does not specify a stack.  It is entirely up to the implementation how to arrange the automatic storage.

In before another round of "YA BUT SHOW ME  A IMPLMENTATION DAT DOESNT USE CALL STACK 4 AUTO STORAGE :D".  It amazes me what lengths people will go to avoid understanding what C is about just because it contradicts what they want to believe.

Name: Anonymous 2010-05-23 3:04

another round of "YA BUT SHOW ME  A IMPLMENTATION DAT DOESNT USE CALL STACK 4 AUTO STORAGE :D".
too bad >>31 already answered that.

Name: not >>1-36 2010-05-23 3:09

>>36
While C doesn't, specific implementations for which people tend to write their code (besides libraries, most applications contain architecture-specific code) do specify things like stack layout in great detail. I've also yet to see a C compiler which doesn't use a stack, and almost all va_start/va_arg/va_end macros operate on the stack directly. Just because it doesn't have to, doesn't mean it won't. While you're programming in ANSI C, you have no right to assume those things, but once you step down and use a real implementation (or a group of real implementations), you can start very clearly making assumptions about how allocation will work, and you can confirm these by inspecting libc source code or the compiler's source code, as well as the generated assembly listing.

Name: Anonymous 2010-05-23 3:09

>>36
Hello, Zhivago.

Name: Anonymous 2010-05-23 3:10

>>38
Don't bother. We all know that's the right answer, but we'll argue the extremes anyway just for fun and profit.

Name: Anonymous 2010-05-23 3:12

>>39
I wish!

>>38
Yes, that was definitely a more thoughtful reply than >>36.  In any case he should keep his shit straight unless he is explicitly talking about a particular implementation.

Name: Anonymous 2010-05-23 6:34

More than 40 replies in such shitty thread? This is worse than /a/

Name: Anonymous 2010-05-23 8:30

>>42
Yeah, in /a/ no one keeps a decent conversation going for more than ten or fifteen.

Name: Anonymous 2011-02-04 15:16

Don't change these.
Name: Email:
Entire Thread Thread List