[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
>Be C++ programmer >Never used pointers
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 121
Thread images: 8
File: mpv-shot0120.jpg (51 KB, 640x480) Image search: [Google]
mpv-shot0120.jpg
51 KB, 640x480
>Be C++ programmer
>Never used pointers
>>
>>53628246
And?
>>
>>53628255
Pointers are pretty annoying.
>>
File: 1457992221969.jpg (166 KB, 1024x751) Image search: [Google]
1457992221969.jpg
166 KB, 1024x751
>>53628246
good for you
>>
>>53628374
I wonder how they get people to do these shots.
>>
>>53628246
>Call myself a programmer
>I am not
>Go to interviews to steal jobs from real programmers
>PR can't tell the difference.
>>
>>53628404
They pay them.
>>
>>53628374
How many shutterstock watermarks is enough?
>>
>>53628498
23
>>
>tfw very rarely use exceptions
>>
>>53628246
raw pointers are bad form in c++, and have been for decades. You should be wrapping everything you 'new' in a shared_ptr or a unique_ptr, and only resorting to raw on library calls that you don't control.
>>
>>53628481
But do they tell you what will you have to do? Are you allowed to mention how retarded this whole thing is? And who are they? So many fucking questions.
>>
>>53628246
Sounds like you're a shit programmer.
>>
>>53629221
how bad is this?
>>
>>53629379
overgeneralizing and spouting mem.. idioms are bad form in c++, and have been for decades. You should be prepared to use things sparely and when appropriate only. And smart pointers aren't apropriate in nano mini micro controllers.

Idioms are like cancer for programming languages and usually a workaround for shit-tier design.
>>
>>53629221
Good. You should very rarely use exceptions because exceptional things are very rare.
>>
>Novice programmer
>Read C code easily while mainstream Java(script) shit makes me feel like picture related.
>>
>>53630027
embedded devices are generally programmed in some flavor of C rather than C++. there is no operational or memory overhead of using a vector over an array, but there is a larger program size due to specialize template code.

when your device is so constrained on resources that program size is an issue, you won't be writing c++.

and smart pointers are definitely appropriate in the devices that CAN handle c++ programs, since the additional "overhead" of an add rx/sub rx and long int are not going to overwhelm the device, but missed free's will very quickly. nobody is a perfect developer, memory manipulation is not safe, and c++ provides effective, effectively free resource management facilities without the opportunity of programmer error. use them.
>>
>tfw a TA tells you not to use pointers

Nigga anyone that works with C or C++ is going to have to know pointers. You shouldn't discourage their use just because they can be confusing.
>>
>>53629777
You can say whatever you want. But you must do as they tell you in order to get paid. Capitalism 101.
>>
>>53628246
For every neat trick you can do with a pointer, there are a dozen different ways you can completely fuck yourself in the ass while using them. Tread carefully.
>>
>>53628246
> learned how to print "Hello World" at CC
> calls self C++ programmer
> typical /g/
>>
If you are 100% sure that you aren't breaking any rules ever, pointers can make insanely fast, optimized code.
However if you make the slightest mistake, you get extremely difficult bugs and crashes to deal with.

Is it really worth living dangerously?
>>
>>53628435
I am convinced that most interviews for programmers are designed to weed out programmers.

It makes it more likely to get the job if you have no idea about the field of programming whatsoever, but just have people skills.
>>
File: g.jpg (62 KB, 660x596) Image search: [Google]
g.jpg
62 KB, 660x596
>>53630888
>>
You have probably never done anything even slightly complex then
>>
Explain in a nutshell what pointers do.
>>
>>53628374
>mfw I have that cooler
scythe mugen, great cooler
>>
>>53631371
Average /g/ poster.
>>
>>53631371
point
>>
>>53631371
Nothing. They are just values that hold a memory address.
>>
>>53629221
>>53629992
>exception thrown
>instead of just doing something else, letting the whole fucking thing crash

Sounds like a good idea
>>
>>53630226

That makes sense. C is a very simple language. Those other languages have many more features and complex syntax to keep track of.
>>
>>53631371

It is a variable that stores a memory address and the language has a bit of special syntax designed for working with it.
>>
>>53631428
>I have never coded anything more complex than hello world or fizzbuzz
Okay.
>>
>>53631371
Opposite of what the '&' reference operator does. Stick '*' in front of a variable that was assigned a memory address as its value, and it'll return back the value that is hosted at that memory address.
>>
>>53631460
>a pointer is a variable
No.
>>
>>53631467
Sounds like you if you don't even know how exceptions work. Just how much of a moron are you?
>>
>>53631482
(Not the poster) Can you explain why?
A pointer can be reassigned, although it doesn't have a data structure as such doesn't that still make it a variable?
>>
If you use pointers in C++ you're doing it wrong.
>>
>>53628246
But how do you understand code from other programmers if you can't use pointers?
>>
>>53629777
a talent hiring agency puts out ads or has recruiters use social media to get randumbs onboard for whatever photoshoots the agency needs. it's a low paying but easy as fuck gig. literally pose like an idiot for a few hours and collect your check.
>>
I use raw pointers all the fucking time and nobody can stop me.
Shared pointers are slow as shit, and even unique pointers incur extra overhead.
Also I don't like to use exceptions, since they also add extra overhead (because they have to propagate up the entire call stack until they're caught)

It's all fine if you're not a dumbfuck retard that can't program
>>
>>53631547

Wrote "hello world" in C++ without using pointers. Keep in mind that the type of a string literal is const char*.
>>
>>53631748

Write*. Meant to be imperative, not past tense.
>>
>>53631547
If you don't you're doing it wrong. If you're writing proper OOP and you're passing around large objects you are copying the whole object per instance. Resulting in double memory usage. Pass a pointer? It's lighter and faster.
>>
>>53631371
Just as they are named. They point to an in-memory address. The memory segment contains a value, according to the variable type.

That's all there is to it, really.
>>
>>53631482
Not him, but isn't a pointer a variable which stores a memory address? If not, why?
>>
>>53631793

I think he means you should be use unique_ptr/weak_ptr/shared_ptr instead, or references if your thing is read-only and can't be null.

That literally eliminates 99% of the need for pointers, while leaving your program safer and while also making it much more clear who owns what objects, all while usually having to write less code and being more exception-safe. It's win:win.
>>
>>53631428
>letting the whole fucking thing crash
Sounds like someone doesn't understand exceptions.

Protip: the exception is catched at a higher level so it can be handled properly based on the kind of exception.
Using an if/else construction where exceptions would fit is bad design.
>>
>>53631793
Somebody has never never heard of move semantics. They completely eliminate the need to haphazardly pass pointers around and avoid needless copies without giving up the RAII idiom. You should only need to use naked new/delete in containers that can properly manage themselves.
>>
>>53632879

References do not need to be read only.

And I'm referring to the fact that every time you use a string literal anywhere, you are using pointers... At least temporarily.

But yes, smart pointers and references are much more convenient than pointers, and should be preferred. That said, there are still uses for raw pointers, namely, interop with C libraries, and the implementation of one's own containers.
>>
>>53631440
This. C at its core is a very simple language. C++ is a totally different beast, and C has nothing on its complexity.
>>
>>53631508
>>53632020

Don't worry, he's talking out of his ass. You guys are right.
>>
>>53633442
>smart pointers aren't pointers
>>
>>53628246
I get the point of pointers but I've never really used them outside of linked lists
>>
>>53632982
Exceptions are garbage for performance.
>>
>>53636621
>t. 1st year comp sci student
>>
>>53636705
second year :^)
>>
>>53636705
https://reddit.com/r/outoftheloop
what does t. stand for
>>
I wish I could say that.
Too many legacy interfaces I use directly pass back pointers, or only accept pointers.

I try to wrap that in smart pointers when I can, but still a pain.
>>
>>53632982
You're one stupid fucking nigger
>>
>>53630027
>And smart pointers aren't apropriate in nano mini micro controllers.

Citation needed. Other than the fact that a C ABI is probably the only available target for embedded devices, there is no reason why smart pointers are "discouraged." Heap allocation, sometimes, but that is separate from smart pointers entirely.
>>
>>53628246
...............how
>>
>>53635759
I don't know who this guy is, but he's not talking out his ass. A pointer is a value, not a variable.

There is a difference. And knowing the difference between variables, rvalues, and lvalues could save your life someday.
>>
>>53635480

I meant non-changing and non-owning, by read-only, but I guess I typed something else.

And no shit under the hood pointers are everywhere, but you shouldn't be using them explicitly very often except in quite limited cases.
>>
>don't know what a pointer is
>google pointers
>realise i already knew what pointers are, and have used them extensively
this is how i passed all of my cp classses, yet still can never get a job
>>
Not using pointers, smart or raw is actually decently doable with C++11+, if you confine yourself to a well defined set of data structures and libraries. the result is probably not all that bad either. Obviously you will still be using many of them under the hood but you need not know or implement any of those.
>>
>tfw using std::deque everywhere you'd need an array
>everything is on the stack, the heap is only used by the stl behind the scenes
C fags will never know this feeling.
>>
>>53629379
OP here. Never had to use that either.
>>
>>53639046
>I used pointers extensively
>without knowing what they are
Wanna explain how you didn't know what they are but used them?
>>
>>53639354
I knew what they were, just not what they were called.
I'm horrible with two things: names and terminology.
>>
>>53639405
let me guess your declarations look like shit
>>
>>53639435
They aren't that bad.
I do try to be neat, otherwise I'm easily confused.
>>
File: 1426331552526.png (11 KB, 470x454) Image search: [Google]
1426331552526.png
11 KB, 470x454
>>53639405
>doesn't know what a thing is called
>having to write its name down to use it
>>
>>53628246
memory leaks, security vulnerabilities, difficult to find bugs...
>>
OP again. I've used them in C a few times, but in actual C++ I haven't needed them at all. I will say you need to learn C to be good at C++ though.
>>
>>53628246
>Never used pointers
Because references are much more sane and safe.
>>
>>53639760
This post is pure retardation.
>>
>>53629379
Smart pointers have indirection overhead.
>>
>>53631946
>The memory segment contains a value, according to the variable type
Why do you need to assign a variable type to a pointer when its job is just to point to something?
>>
>>53628246
>2016
>be c++ programmer
>using raw pointers
>>
>>53630027
>And smart pointers aren't apropriate in nano mini micro controllers.
unique_ptr is literally costless
>>
If i pass something by reference (e.g. use the &) is that different than a pointer? It accomplishes the same thing, does it not?
>>
File: shutterstock_70490008_revised1.jpg (23 KB, 300x250) Image search: [Google]
shutterstock_70490008_revised1.jpg
23 KB, 300x250
>I never put an object in the heap
>>
>>53631371
t. sanjeep prajrahmujilgonda
>>
>>53628246
I work in a subfield of AI and data science and have never used a low-level programming language.

I know some java, js, python (a lot), prolog, lisp, and perl.

Mind you, I do not have a CS degree.
>>
>>53640909
1. A reference cannot be empty
2. You cannot make a reference point to another object
3. No pointer arithmetic
>>
>>53640909
A reference is a pointer but with additional constraints, allowing for optimisations and additional code safety.

A reference can not be NULL and you can't do pointer arithmetics on a reference.
>>
File: 1392889714282.png (312 KB, 506x662) Image search: [Google]
1392889714282.png
312 KB, 506x662
>>53636717
Practice your pointer arithmetic, you'll need it if you want to git gud.
>>
>>53641219
>allowing for optimisations and additional code safety.
No.
>>
>>53631428

But it is.

In a language with unchecked exceptions, you should only throw when there is nothing the program can do to fix it, and let the whole program burn down.

If you are using an API that throws exceptions, you should wrap it in an adapter layer that returns error structs or maybe / either types (if your language supports that).
>>
>>53631748

Write hello world in any high level language without using pointers. Keep in mind that whatever language you're using uses pointers as an implementation detail that you don't even care about.
>>
>>53631371
They point.

How fucking retarded are you?
>>
>>53631371
Name says.
But they can be dangerous.

Honestly if you don't work CPU/GPU intensive tasks in C++, you don't need to use them at all.
And honestly if someone haven't mastered them 100%, they should not ever use them in production cde.
>>
>>53643530
Try writing anything without it being converted to assembly, I dare you
>>
>>53643709
>But they can be dangerous.
Will they make my computer explode?
>>
>>53643755
I just wrote this sentence, does that count?

I guess it depends on whether or not you consider your browser's renderer an interpreter for this language, which is technically just a graphics EDSL that lets you easily render words on a screen.
>>
>>53637609

Why are rvalues and variables not interchangeable? Legitimately curious now
>>
>>53643755
>>53643787
BTFO
>>
>>53644380
>Why are rvalues and variables not interchangeable?
x = 12345
makes sense
12345 = x
does not
>>
>>53644485
I didn't say rvalues and lvalues, I said rvalues and variables.

In that snippet you posted, x is a variable. x is also an rvalue. Explain when they are not interchangeable?
>>
>>53644510
>>53644485

Fuck me, I meant lvalues and variables.
>>
>>53644530
>Why are lvalues and variables not interchangeable?

*((char *) 0x12345)
is an lvalue but not a variable.
>>
>>53637609
>A pointer is a value, not a variable.
No.
>>
>>53644613
Actually, I stand by my original point. Why do you not consider a pointer a variable, and rather a value?
>>
>>53644738
Pointers are types - derived types to be exact - not values or variables.
>>
>>53644613
>an lvalue but not a variable.

C language guy here.

The standards documents (X3J11 and successors) do not use the term "variable" very much, they don't bother defining it precisely, and they tend to use it only in more informal circumstances (like in the explanations of examples) where the term "lvalue" would sound over-jargony.

So that means we (the world at large) get to define "variable" however we want.

To keep things simple, I have always recommended using the word "variable" as a synonym for "lvalue".

For example, let's say somebody has this code:

long a[5];
long *p = (long *)malloc(sizeof long);
long *q = (long *)12345;

a[2] = 0;
*p = 0;
*q = 0;


When discussing the last three assignment statements, I would typically say that "0 is being assigned to the variables a[2], *p, and *q".

I would not edit my language to use the term "lvalue" instead of "variable" simply because they don't have identifiers directly bound to them.

I faced exactly this issue in the past when I had to decide how I was going to speak about these things in my C language class. I made the judgment call that using the term "variable" probably resonates better with the students than the term "lvalue", even though "lvalue" is the preferred technical term in X3J11 (and successor documents).

tl;dr: It's a judgment call that balances technical precision versus the desire to avoid overuse of jargon with students who are new to the concepts.
>>
>>53644878
>Pointers are types - derived types to be exact - not values or variables.

Nope. Pointers are very much values.

Values can be of quite a few different types: character, integer, double, struct, pointer, etc.

Since a value can be of a pointer type, then a pointer expression is definitely a value.
>>
C bros

What happens if I declate

int p;

//and do

p = "hello"
>>
>>53645276
Compiler will give you a type error.
>>
>>53644485
Both make sense in functional programming since = isn't assignment.
>>
File: 1406342198440.png (58 KB, 403x426) Image search: [Google]
1406342198440.png
58 KB, 403x426
>>53644485
>>
>>53645403
They make sense in a lot of languages outside the C family because = is a test for equality.
>>
>>53639934
So you know what the type of the pointed-to thing (pointee?) is
>>
>>53645238
>Nope. Pointers are very much values.
Wrong.
Definition of pointer are given in section 6.2 Types and Representation of types.

>then a pointer expression is definitely a value.
Completetly wrong.
>>
>>53628246
>2016
>memory management
gc all the way
>>
>>53644878
Type

Variables can be of different types

We're pretty much arguing the same thing
>>
>>53645171
Thanks for the clarification, C guy.

I-I like people who know standards. Are you single?
>>
>>53645403
λ pattern Foo x <- y
λ let Foo x = 12345
λ let 12345 = Foo x

<interactive>:72:13:
non-bidirectional pattern synonym ‘Foo’ used in an expression
In the expression: Foo x
In a pattern binding: 12345 = Foo x


Haskell won't just let you swap the sides of = without changing the meaning heavily. Nor will any other functional language I know.
>>
>>53645639
you seem to mispelled smart pointers
Thread replies: 121
Thread images: 8

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.