What about Pointer Arithmetic with Void Pointers?

preview_player
Показать описание
---

What about Pointer Arithmetic with Void Pointers? // After my recent video about void pointers, some of you commented about pointer arithmetic. So, this is a follow-on video to address this issue. This video talks about why pointer arithmetic with void pointers should be avoided.

Related Videos:

***

Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.

About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.

More about me and what I do:

To Support the Channel:
+ like, subscribe, spread the word

Рекомендации по теме
Комментарии
Автор

I'm not sure why every CS/ECE student doesn't follow you. I honestly can't imagine a better source of information than this channel.

oneletterz
Автор

Thanks for shouting out to us here in the Southern Hemisphere!

lonelymtn
Автор

Superb i've learn for the first time what casting really means and even more about pointers. Thanks. ❤

Byynx
Автор

Your lessons has transformed my code, especially when it comes to optimization. Thanks a lot

ramadhanafif
Автор

Doesn't look like anyone has taken me up on recommending you to learn C with, but I'm going to keep pushing people your way anyhow. Your channel is easily one of the top 5 for programming content, and certainly the number one channel for C.

anon_y_mousse
Автор

Fascinating. A long time ago I drove hardware using pointers, that's what you did. You add 2 to a pointer, it is pointing 2 bytes up the line. You get your C code pointer numbers wrong, the code does something wrong. - it'll read the wrong value or write something horrid to your CAMAC. It was rather charming how the original C allowed you to really, really screw up.

chrissaltmarsh
Автор

I always increment void pointers directly when I want to add bytes (and I do that a lot in my programs), I didn't know this was not standard, thanks for the info

leokillerable
Автор

will get it. Just don't get burnt out. Whenever you need a break, take one.

abdallahmah
Автор

One thing I might add is that everyone should turn up their warning levels when they compile (I have level4 set when using msvc from the command line). When you do that, it doesn't even let you do pointer arithmetic on void pointers

evan_game_dev
Автор

This is why I love C and low level. You shouldn't do it, but you are free to try. Full control.

nieczerwony
Автор

Correction: You stated "The compiler didn't have to do something reasonable, because the C standard doesn't actually support this behavior, it doesn't make any guarantees".
That's in fact not true. The C standard prohibits void pointer arithmetic, so this is not an undefined behavior, it's very clearly defined.
However, having a compiler adding this feature as a "compiler extension" is something completely different than a UB (same goes for something like nested functions with GCC).
Again GCC and other compilers have many extensions (that doesn't make your program undefined as you mentioned in your final words). But if you choose to be strictly compliant with the C standard, you may use some compiler flag like "-Werror=pedantic" and you're good to go.

MoustafaRaafat
Автор

Excellent as always!
What puzzels me that it is possible to define an array of void pointers. That means that there must be a size of a void pointer otherwise indexing such an array would be difficult

keesterwee
Автор

learned this the hard way, way back when 😂

my assumption was that void pointers would always increment by the size of the compiler's integer, but nope. Been using char* since then

insu_na
Автор

I was inspired by your void pointer video to build a pointer type in java that works on native memory. While writing the classes I stumbled over this exact problem, "what should p.add(int i) do on a void type." I thought it must be 1 byte but how does languages with pointers do it? And now I have my answers, they don't know it either XD. Thanks for the video.

redcrafterlppa
Автор

That font looks so nice how to set it up ?? plus what colour theme, file icon theme and product theme you use ?? I am learning a lot from you as a Embedded student your videos help a lot to understand C/OS concepts thank you.

laxmikantbotkewar
Автор

You know, I'd always assumed that compilers wouldn't allow you to do void pointer arithmetic so I never thought to even try it. I mean as you said void doesn't have type information so you have no guarantees it would do anything remotely sane. But I guess compilers allowing you to do that is pretty C.

beardlyinteresting
Автор

nice, I actually didn't know that's all UB

nexovec
Автор

Good try to avoid UB ! However You still have ONE on the 22 line !)

viacheslav
Автор

We should talk more about hexspeak and what OSes uses it.

georgecop
Автор

Personally I think that since void is size 0 (as a result of no type info) pointer arithmetic with it should just result in addr + 0 * 2

zxuiji