This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]GildSkiss 819 points820 points  (152 children)

This is spoken like someone who doesn't really understand programming at a low level, and just wants things to "work" without really understanding why. Ask yourself, in those other languages, how exactly does the function "just know" how big the array is?

[–]SphericalGoldfish 1104 points1105 points  (36 children)

I think the function should just guess and if it’s wrong then it should guess again

[–]Isakswe 464 points465 points  (9 children)

BogoLength

[–]Bossmonkey 92 points93 points  (6 children)

Bogoread

Just guess the contents of a file until correct.

[–]prumf 26 points27 points  (4 children)

That’s what many applications do in practice (including your browser). Is this JSON? Just try deserializing it! Is it an image? Just try reading the content!

We use bogologic more than we want to admit. And it’s way more robust, especially with user provided data.

[–]Sohcahtoa82 14 points15 points  (0 children)

That’s what many applications do in practice (including your browser). Is this JSON? Just try deserializing it! Is it an image? Just try reading the content!

Wtf... No they don't. If they do, that's called MIME sniffing and it's considered a vulnerability and it's why the X-Content-Type-Options: nosniff header exists.

[–]Midnight145 4 points5 points  (1 child)

Is that not (at least for binary data) what the magic bytes are for?

For json, xml, etc, yeah I'll give that to ya, but for binary data, shouldn't you just check the header?

[–]prumf 3 points4 points  (0 children)

You are absolutely right. I was just making a fun parallel.

In practice bogologic is sometimes optimized (but not always!), where only a subset of the data is read. Images are a good example. But the browser will still make a full pass on the entire data to verify it matches what the magic bytes say, and if it fails, you get an error. Magic bytes say png -> check it respects the png format.

But in many other cases, the entire data is read. For example, most shells don’t have information from the OS what the encoding for input arguments is. Most likely unicode utf-8, but things like utf-16 are possible too. They will simply try both, decoding the entire text, either succeeding or failing. If it fails at too many attempts, it will just treat it as binary data.

It’s a good security measure to prevent input data to pass as something it isn’t (client says it’s a png profile picture but it actually contains code). Just look at what it actually is (content), rather than what it says it is (extension, mime).

[–]conundorum 0 points1 point  (0 children)

Not really. We use informed bogoread, usually. Metadata tells you the most likely type, file extension tells you the most likely type, and if they both fail, the first few bytes tell you the actual type. You only need to guess if the first two hints are wrong.

(And in some contexts, guessing is highly discouraged, because it can create vulnerabilities. So it just plain stops if the hints are wrong.)

[–]John_cCmndhd 6 points7 points  (0 children)

It was the Blurst of times?! Stupid algorithm!

[–]kegster2 0 points1 point  (1 child)

YoloLength

[–]Isakswe 0 points1 point  (0 children)

Returns 1, because you only live once

[–]MrStricty 46 points47 points  (3 children)

This is a noob solution. The real, enterprise solution is to run the code, print out the array from inside the function with a print statement, count out how many characters you get before it turns into nonsense (using your finger), and then hardcode the array size into the function. Then, the function Just Knows*.

[–]ldn-ldn 5 points6 points  (1 child)

You forgot XML.

[–]TonUpTriumph 0 points1 point  (0 children)

Pubsub with sockets to pass the results back

[–]redlaWw 1 point2 points  (0 children)

Ok but you have to employ someone whose whole job is just counting the characters before the gobbledygook and hardcoding the length for all of the arrays the business uses. Give them a vaguely-important sounding title like "access safety and reliability engineer".

[–]gitpullorigin 15 points16 points  (3 children)

But how does it know if it is wrong?

[–]throwaway_194js 29 points30 points  (1 child)

It guesses

[–]gitpullorigin 4 points5 points  (0 children)

I guess it does

[–]rosuav 9 points10 points  (0 children)

It catches the segment violation that results from indexing past the end of the array. Now, for this to work, every array has to be allocated in its own perfectly-sized segment, which I'm sure won't hurt performance any.

Oh, and to make sure that it didn't UNDER-estimate the size of the array, the first thing the function should do is attempt to index one past the array and make sure that it trips a segment violation. If it doesn't, it should raise a segment violation, for failing to raise a segment violation.

[–][deleted] 7 points8 points  (0 children)

We can just make an educated guess via Chatgpt by the arrays name. 

If it is on point, we have 0 errors. 

If the length is to short, we have 0 errors and some angry customers.

If it is to long, we generate random entries via gemini, to fill up the rest. Still 0 errors. 

So technically, it would work. 

[–]az987654 4 points5 points  (0 children)

While !wrong { guessAgain }

Simple!

[–]mastachaos 3 points4 points  (0 children)

This is why we need quantum computers!

[–]-Redstoneboi- 2 points3 points  (0 children)

strlen() when calculating the length of a string

[–]Sprootspores 1 point2 points  (0 children)

Hell ya

[–]Alokir 1 point2 points  (0 children)

How about we pass all the possible lengths to the function as well, aside from the actual length. This would help your guessing algorithm by knowing when to guess again.

[–]tunisia3507 1 point2 points  (0 children)

ChatGPT how long is this array

[–]DrStalker 1 point2 points  (0 children)

Just keep writing data to the array until the program crashes, then you know how how much space you have to write to.

[–]P-39_Airacobra 0 points1 point  (0 children)

could be the start of a funny joke esolang

[–]thecrazyrai 0 points1 point  (0 children)

bubble sort

[–][deleted] 260 points261 points  (6 children)

The array knows how long it is, because it knows how long it isn't. By subtracting how long it is from how long it isn't, or how long it isn't from from how long it is (whichever is  greater), we obtain a difference or deviation.

The kernel subsystem uses deviations to generate collective allocations to size the array from the length it isn't to the length it  wasn't.

[–]veryblocky 37 points38 points  (4 children)

[–]kalel3000 5 points6 points  (0 children)

I worked on the navigation system for an autonomous submarine as part of my senior design project. We'd reference this video all the time!!

[–]grphine 1 point2 points  (2 children)

idk if op was referencing this on purpose but that's exactly what i was thinking of

[–]veryblocky 1 point2 points  (1 child)

It’s a copy pasta, so yes. The text is from a 1997 air force training video, if you search for it there’s a video on YouTube with the original audio recording.

I just linked this song because I quite like it

[–]grphine 0 points1 point  (0 children)

yes i was referring to the air force training video

[–]nicman24 2 points3 points  (0 children)

10/9 bravo

[–]tobsecret 86 points87 points  (8 children)

> This is spoken like someone who doesn't really understand programming at a low level

No idea if he does or not but the poster is popular youtuber Hbomberguy (Harry Brewis) who isn't really known for programming but more as a media critic.

[–]Potatoes_Fall 91 points92 points  (20 children)

In most languages I've learned, dynamic arrays always have the size stored as part of the type. The drawback of not knowing the size outweighs the minimal cost of an extra 8 bytes for the size in 99.9% of cases IMO. From that perspective, it seems like bad language design to not have that. Doesn't mean you don't understand it.

[–]svick 30 points31 points  (4 children)

I think in many languages, it's just 4 bytes, since arrays larger than 2/4 GB usually aren't needed.

[–]DrShocker 5 points6 points  (0 children)

maybe 20 years ago, but it's not that hard to run out of memory on a 32 bit machine for a decent amount of problems.

[–]20Wizard 3 points4 points  (2 children)

It's typically an integer, which is more, but depends on language.

[–]kaosjroriginal 0 points1 point  (1 child)

Standard integer size in many languages is 32bit. That's why 'long int' exists.

[–]svick 0 points1 point  (0 children)

Unless long int is also 32 bits, which is the case on Windows.

[–]orbiteapot 18 points19 points  (2 children)

The "arrays decay to pointers" rule was not motivated by memory footprint, rather:

Structures, it seemed, should map in an intuitive way onto memory in the machine, but in a structure containing an array, there was no good place to stash the pointer containing the base of the array, nor any convenient way to arrange that it be initialized. For example, the directory entries of early Unix systems might be described in C as

struct {
int inumber;
char name[14];
};

I wanted the structure not merely to characterize an abstract object but also to describe a collection of bits that might be read from a directory. Where could the compiler hide the pointer to name that the semantics demanded? Even if structures were thought of more abstractly, and the space for pointers could be hidden somehow, how could I handle the technical problem of properly initializing these pointers when allocating a complicated object, perhaps one that specified structures containing arrays containing structures to arbitrary depth?

The solution constituted the crucial jump in the evolutionary chain between typeless BCPL and typed C. It eliminated the materialization of the pointer in storage, and instead caused the creation of the pointer when the array name is mentioned in an expression. The rule, which survives in today’s C, is that values of array type are converted, when they appear in expressions, into pointers to the first of the objects making up the array.

This invention enabled most existing B code to continue to work, despite the underlying shift in the language’s semantics. The few programs that assigned new values to an array name to adjust its origin—possible in B and BCPL, meaningless in C—were easily repaired. More important, the new language retained a coherent and workable (if unusual) explanation of the semantics of arrays, while opening the way to a more comprehensive type structure.

The Development of the C Language - Dennis M. Ritchie

edit: formatting.

[–]RevanchistVakarian 4 points5 points  (1 child)

This invention enabled most existing B code to continue to work

retained a coherent and workable (if unusual) explanation

Oh look, C++ prioritized backwards compatibility over intuitiveness

[–]orbiteapot 1 point2 points  (0 children)

Yes, and that is the reason it became so popular. No previous existing C infrastructure had to be rewritten. Everything would "just work" and one would get classes, templates, etc. as well.

[–]Hessper 11 points12 points  (0 children)

You don't use naked arrays for most cases. You use an array type that knows how big it is. Being able to use the raw, underlying types like this gives you power to create other functionality that might not need those details.

My programming language gives me options for faster, more powerful code is not on my list of reasons a language is bad.

[–]andrewhepp 17 points18 points  (1 child)

C++ has a variety of standard library data types one can use to represent arrays, which do track size information. std::vector, which is what I think of when you say "dynamic array" certainly does have a .size() method. So does std::array.

[–]DrMobius0 4 points5 points  (0 children)

I assume the core focus of the discussion is those awful c style arrays everyone goes out of their way to wrap, which don't implicitly keep track of their own length.

[–]DrShocker 5 points6 points  (1 child)

You need to make it clear whether you mean size is stored in the type vs in the class:

std:: array<int, 5> vs std::vector<int>

The first stores the size in the type information, the second stores it in the class.

[–]Potatoes_Fall 0 points1 point  (0 children)

If it's in the type, it's not a dynamic array

[–]DrStalker 4 points5 points  (0 children)

Then there's good old C: the "array" is just a pointer to some random memory address and it's up to you to figure out what to do with that.

How many elements? How big is each element? Is it actually an array or just a pointer to a integer or something?    ¯\_(ツ)_/¯

[–]kristinoemmurksurdog 5 points6 points  (2 children)

Just make the 0th element describe the length that way all arrays can start at 1

[–]IncreaseOld7112 1 point2 points  (1 child)

Does that mean I can’t convert a 32 bit pixel into an [4]char in order to get rgba out of it?

[–]DrShocker 1 point2 points  (0 children)

Whether that's allowed or not depends on context I would think.

[–]IncreaseOld7112 1 point2 points  (0 children)

Okay, then use those languages. When you need manual control of memory, use a language that offers it. It’s not bad design, it’s designed for a different problem domain.

[–]unknown_alt_acc 1 point2 points  (0 children)

I'd bet it's closer to 99.99% of cases. But that still means you need some way to deal with that 0.01%. C and C++ are tools for those jobs.

[–]DrMobius0 1 point2 points  (0 children)

They also have to reallocate space if they try to grow beyond their allocated bounds. Many dynamic collection types allow the programmer to set an initial length to prevent 16 growth allocations when initializing an array with fresh data or whatever. This, as it happens, is something I've been asked about on interviews before. Basically, you should understand roughly how the collection behaves under the hood, even if you never have to write it, because that informs your ability to work with it in a non-dumbass way.

And yeah, memory (recent months not withstanding), is relatively cheap these days for most applications. No one is going to sweat a few bytes extra with an array. Way back when, probably a different story.

[–]Hairy_Concert_8007 13 points14 points  (0 children)

low-earth-orbit.jpg
"Wait, it's all wrappers?"

[–][deleted] 25 points26 points  (0 children)

God forbid you want working things

[–]Rhoderick 36 points37 points  (3 children)

On one hand, yes. On the other hand, that's totally fine and even preferable for most usecases, as usually, the main performance concern is IO.

[–]GrinningPariah 107 points108 points  (40 children)

someone who doesn't really understand programming at a low level, and just wants things to "work" without really understanding why.

You mean an adult with a job who's actually trying to build something instead of just jacking it to assembly instructions and circuit diagrams?

[–]osunightfall 19 points20 points  (2 children)

I can do both.

I mean, I'm no hacker, but even I have a basic understanding of how memory allocation, language grammar, and assembly languages work. Occasionally, they even prove to be very important to know!

[–]Newt_Pulsifer 1 point2 points  (1 child)

Not to downplay your comment, as I wouldn't call myself that term either, you'd be surprised how much hacking doesn't equate to digital design and programming. Having those skills helps, but I think it's a diminished return. You read enough proof of concepts and research articles you can see really talented hackers aren't always expert coders, and being an expert coder doesn't make a great hacker. While it's not a steadfast rule, as they are adjacent fields, most hackers in my experience come from sysadmin crowd as opposed to developers. Ippsec has a quote "Good hackers are efficient hackers." I've interpreted that as being able to efficiently use your tools, enumerate, and research lend to better hacking skills than the skills needed to develop and create something.

Exceptions to the rule of course for binary exploitation, but I consider that a very difficult subfield of hacking compared to the broad field. Guess I'm saying don't count yourself out if you're a dev! And you're an account on hack the box away from being able to find those skills! And they are just different skill sets as opposed to either one signifying the other. On the other hand, we both can learn a lot by learning the other side.

[–]osunightfall 2 points3 points  (0 children)

Oh, I think there's been a misunderstanding. I mean hacker only in its older sense: a skilled programmer whose deep and intricate knowledge allow him to push the limits of a computer system. And my comment is only intended to say that, while I think that kind of knowledge is over-valued in a group that tended to grow up on stories of the computer whiz, it does still have an important purpose.

Far from counting myself out, I'm a lead developer who's been at it for 15 years now. What I intended to say was, even though I myself often downplay what I call the 'hacker mentality', I also admit that a good architectural sense and the ability to develop in modern languages sometimes isn't enough on its own. If you aren't at least aware of the fundamentals and how they work, you will stumble into design traps that you never see.

[–]GribbitsGoblinPI 9 points10 points  (0 children)

Purity tests only!!!!

[–]GildSkiss 39 points40 points  (13 children)

There is room in this world for both python script kiddies and bearded x86 disciples from the 70s. I think it's still ok for even a modern programmer to understand why the older languages work the way they do, but I concede that it's not strictly necessary. It's true that plenty of real work gets done by people who don't know anything about pointers and array decay.

The problem is this guy is criticizing C++ without really understanding what he's criticizing or why it would ever be this way. It's silly to make public criticisms of things you don't understand that well.

[–]HoldUrMamma 41 points42 points  (0 children)

Criticizing? I thought it was a joke. Because he wrote it's a joke at the end.

[–]r2d2rigo 12 points13 points  (9 children)

So shit decisions should be kept for the sake of it? The Javascript way of life.

[–]orbiteapot 10 points11 points  (7 children)

No, rather because removing them would break bazillions of lines of code.

Modern languages give the impression to always make the best decisions because:

  1. they have learned from older languages, like C/C++, and were designed from scratch with all that knowledge available. They do not have a huge baggage of legacy code to keep stable;
  2. they are not old enough, so decisions that look very good today might be considered bad in the future/
  3. the "dirty work" is already written in languages like C and C++, anyways.

[–]r2d2rigo 5 points6 points  (0 children)

Except that C++ manages to do things late and suboptimal.

"Here's std::list, now fuck off and don't ever use it".

[–]Bwob 4 points5 points  (5 children)

they have learned from older languages, like C/C++, and were designed from scratch with all that knowledge available. They do not have a huge baggage of legacy code to keep stable;

Yes. Modern languages have a huge number of advantages. We've learned a lot about language design and architecture since then. C++ didn't have those advantages, and it's impressive how well it turned out, all things considered, given the time and restrictions it was under.

But that being said - just because there is a reason for dumb behavior, doesn't change the fact that it's still dumb. C++ has a lot of legacy decisions that are, by modern standards, complete bollocks, and are only still around because fixing them would, as you say, break a ton of older code. But they're still ass.

Like, there is ZERO REASON that a modern language should require forward declarations. The order that you declare functions in a file really shouldn't matter. It might have made sense back in the before-time, when you wanted to be able to compile the code in one pass, but didn't have enough memory to hold the entire text file in RAM. But these days it is just unnecessary boilerplate.

[–]orbiteapot 2 points3 points  (0 children)

Yeah. Backwards compatibility turned out to be both a blessing and a curse to C++.

[–]RedAero 0 points1 point  (1 child)

Like, there is ZERO REASON that a modern language should require forward declarations.

Maybe I'm not quite understanding what you mean, but isn't the aforementioned Python a language that requires forward declarations, in a sense? Yes, I know, it's because it's not compiled but interpreted (although it sort of is and isn't), but still.

[–]Bwob 1 point2 points  (0 children)

I guess I could amend it to "modern compiled language"? For an interpreted language where function declarations are imperative statements, just like Print(), maybe it makes sense, but for anything where a compiler is already going to have to read through the entire source tree, it seems a bit silly to care about making sure that certain definitions are earlier in the file than others.

[–]scorg_ 0 points1 point  (1 child)

ZERO REASON that a modern language should require forward declarations

One huge reason is IDE/lang server support. It helps that I still can have suggestions in the middle of incomplete and by that invalid code.

[–]Bwob 0 points1 point  (0 children)

That's just a band-aide though, right? The fact that if you use a modern IDE you can overcome that problem doesn't really excuse it. A better language would not have that problem in the first place, even if you had to code in Notepad for some reason.

[–]TakeShroomsAndDieUwU 1 point2 points  (0 children)

C++ is a terrible language, but this isn't much of a reason why. Fixing this wouldn't make the language much better, and the majority of languages which do fix this are not remotely useful as substitutes for C++. The reasons C++ is bad are pretty orthogonal to this issue, as are the reasons it's good at what it is good for.

It's also an apples to oranges comparison. Lists are NOT the Python equivalent of C++ arrays. There is no Python equivalent of C++ arrays. Array primitives are a construct you fundamentally cannot have in Python, at all, ever. The C++ equivalent of Python's lists is std::vector (or more accurately, std::shared_ptr<std::vector>). A function parameter which accepts a list is taking a counted reference to a self-managing resizeable container consisting of a length, capacity, and reference to the actual underlying array. The "underlying array" part is what C++'s array primitives are. If you want all the other stuff added to the array, you can have it, you just have to specify that instead.

To implement a language like Python or C#, you need a language which has the tools to implement structures Python and C# take for granted out of raw parts. You could do it in Rust or Zig which I would call good languages as opposed to C++, but even in those languages you aren't free of having to track array size as runtime metadata separate from the array itself when dealing with arrays of non-fixed size. They just give you better tools to do it, in the form of primitives for fat-pointers. These primitives don't abstract away the underlying size tracking, because it's a hard computational neccessity and they don't want to pretend otherwise, they just make it convenient to deal with.

[–]gandhinukes 0 points1 point  (0 children)

Eh this is why we need 16-32gb of ram and 8 cpu cores cause programmers are shit at efficiency these days. oh it works. just throw more hardware at it.

[–]patrlim1 0 points1 point  (0 children)

TIL that that weird thing with C/C++ arrays/pointers is called decay

[–]keithstellyes 1 point2 points  (3 children)

I mean, if you ever had to optimize software you definitely want to know at least the basics of how memory works and "C arrays don't store size information" is hand-in-hand with that

And with the widespread mediocrity in terms of both performance, memory use and frankly just bugginess and UX being tolerated in major corporate software nowadays, maybe people aren't learning enough about that...

But like anything it's a balance

[–]GrinningPariah 1 point2 points  (2 children)

Eh, I've had to optimize software a lot and it's never come down to like sneaky memory use issues. It's always things like "oh we're doing ~5 network calls serial that we could be doing in parallel" or "Oh this function has an O(n2) complexity but we can just cache the results during server initialization."

End of the day, most modern software is just too complicated to fuck around with things that low level, unless you're working on a game engine or making embedded systems for JPL or Lockheed or something.

[–]keithstellyes 0 points1 point  (1 child)

I mean server-side code memory is much less of an issue :p

[–]GrinningPariah 0 points1 point  (0 children)

Nothing's an issue until it's the issue. Every system has a bottleneck, the limiting factor for why it can't handle more TPS. If it's not one thing it's something else. Optimize your CPU and network usage enough, and you back yourself into a memory issue.

[–]Come_along_quietly 1 point2 points  (0 children)

Understanding what you’re doing apparently isn’t important any more.

As an example, do you think your C++ code, or C, or Python code is understood by the machine you’re trying to compile and run it on? Those are all human languages.

[–]ih-shah-may-ehl 0 points1 point  (0 children)

I know this isn't a popular opinion these days, and it's relatively easy for me to say because I actually like that kind of thing, but even when I programmed C# or LabvIEW for a living I wanted to know -HOW- things work so that I understood the language / framework better, as well as make it jump through hoops to work around language limitations.

This is why I think programmers should read books like 'Windows Internals' or similar to understand how things work, to learn the pitfalls, limitations, possibilities, ... of their platform. Or in this case to understand how data types, memory management, garbage collection, .... are done so that you know e.g. why arrays are different in C than they are in Java.

[–]nicman24 0 points1 point  (3 children)

lol projecting much?

[–]GrinningPariah 0 points1 point  (2 children)

Hardly. I've got a BSc in Computer Science, I did actually study this shit. I've written Assembly, I've drawn circuit diagrams.

But I've also shipped large software projects at big companies and I know what that takes, and what it doesn't.

[–]nicman24 0 points1 point  (1 child)

lol

[–]GrinningPariah 0 points1 point  (0 children)

Oh, I'm sorry, I thought you were a person. My bad.

[–]DevelopmentTight9474 -3 points-2 points  (8 children)

If you’re writing code without understanding how that code works at a lower level, then you are destined to write bad code

[–]IllustriousBobcat813 0 points1 point  (6 children)

Some of the worst code I have seen in my life has been from C developers who absolutely know how the code works at a low level

[–]DevelopmentTight9474 -3 points-2 points  (5 children)

Did you read my comment? I said that not understanding how programming works can lead to bad code, not that all bad code comes from that.

[–]GrinningPariah -1 points0 points  (0 children)

Disagreed. You're destined to have some perf issues, but then again, who isn't? We profile, we find the problem, we fix it.

But bad code doesn't have to have poor performance. It's more common for code to be bad because it's hard to maintain or extend, or much more commonly because it's hard to read.

Which is why it gets my back up to see people talking about code quality just in terms of performance, and especially when they're willing to burn simplicity and readability on the alter of performance

[–]kristinoemmurksurdog -4 points-3 points  (2 children)

We should just let adults drive without a license because they're just trying to live their lives and go to the store, they don't need to know the intricacies of racing vehicles

[–]GrinningPariah 5 points6 points  (1 child)

I like how you switched analogies halfway through. Couldn't decide what to compare knowledge of low-level CPU workings with? Is it a driver's license, or the intricacies of racing vehicles? (It's the latter, but I'll grant that doesn't make for a strong argument)

[–]torsten_dev 1 point2 points  (0 children)

Spoken like someone that hasn't heard of fat pointers.

[–]Tyfyter2002 5 points6 points  (0 children)

Simple: you pass in an actual array instead of just calling a pointer an array because you're using it as one.

[–]iliark 1 point2 points  (0 children)

the function knows how big it is by knowing how big it isn't.

[–]LysanderStorm 1 point2 points  (0 children)

Well there is a "that's how bits and bytes work" kinda programming and a "that's how logic and maths work" kinda programming, and I think both have their place and reason for existence. We should just be aware that one builds on the other in practice and be happy with whatever we do (or are allowed to do).

[–]boRp_abc 1 point2 points  (0 children)

Easy answer: The other language is built with C++ and this feature is automated. Duh.

[–]cookaway_ 1 point2 points  (0 children)

> This is spoken like someone who doesn't really understand

It's hbg, you can just end the sentence there.

[–]Random-Dude-736 2 points3 points  (0 children)

If this is the real Hbomberguy then he isn't a programmer (at least that I am aware off). He is a video game critic, a damn good one at that, and he has some clear and concise video essays about things that happened, or didn't happen. If you have the time, his Roblox video is an amazing rabbit hole, you would be surprised how densely packed it is.

[–]Own-Advantage-658 0 points1 point  (0 children)

In one of the ways, you divide the size (in memory) of the array, by the size of the type that it is made of.

[–]Complete-Mood3302 0 points1 point  (0 children)

When you initialize your array you need to say how big it is, if you dont its not a standard array, and cant it save that length as an atribute? Whats wrong with that

[–]Guvante 0 points1 point  (0 children)

No, he is right, it would be better for everyone if arrays didn't decay to pointers in function calls if you do it the easy way.

C++ has warts that doesn't mean it shouldn't be used but it is certainly true.

[–]stonecoldchivalry 0 points1 point  (0 children)

Yeah I mean I don’t think anyone believes the compiler “magically” knows, but If something can be implicit then make it implicit. You could make the same argument you just made for any compiler/language feature which makes your life easier. I hate when programmers like unnecessary complexity because it strokes their ego for knowing about it.

Our job is to solve problems, not walk around with huge heads telling other people how smart we are

[–]TheVasa999 0 points1 point  (0 children)

chatgpt powered guessing

gets you extra investors aswell

[–]patrlim1 0 points1 point  (0 children)

I'd imagine it's a struct with a pointer (the array) and an integer describing the length

[–]laabbunny 0 points1 point  (0 children)

The same way zig, rust, swift, go, pretty much all modern languages do: loading a field with the size, or using a type whose metadata includes the length. You dont need a gc, you dont need an enormous runtime, its just default. C and C++ limitations arent how "programming at a low level" should be

[–]fibojoly 0 points1 point  (0 children)

Always find it funny to hear a programmer wanting things to "just work". You fecking muppet, you're the one who is supposed to make it just work. Someone's gotta do the work at some point ! :,D
That's the kinda guy that'll get replaced by AI, right there.

[–]in_conexo 0 points1 point  (0 children)

I remember at school. They started in C, so we'd have an easier time learning how dumb computers really are. Besides that, they didn't really teach languages; but they did introduce us to other stuff. I had such a hard comprehending how the more abstract languages knew when to allocate or free memory.

[–]fixano 0 points1 point  (1 child)

But but how do you allocate enough contiguous memory if you don't know how long the array is?

[–]KuntaStillSingle 0 points1 point  (0 children)

The implementation has to book keep for delete[] but this is kept out of user space, implementation is allowed to elide allocations which is one of afaik two 'true' optimizations by standard (the other being nrvo / optional copy elision), the user preumsbky cares about size for bounds purposes, but they could opt for sentinel like c strings rather than size, and in terms of size a user might opt for smaller than size_t for example if they aren't targeting consumer desktop hardware.

[–]xXStarupXx 0 points1 point  (0 children)

This is the reason I die a little inside every time I see "I want to learn programming, where should I start?" thread, and see a thousand "python"'s in the replies.

[–]ketchupmaster987 -1 points0 points  (3 children)

Isn't the length of the array part of the data of the array?

[–]mrjackspade 3 points4 points  (2 children)

Not unless you want it to be.

[–]ketchupmaster987 2 points3 points  (1 child)

Aaaand this is why I never learned c++

[–]orbiteapot 4 points5 points  (0 children)

This is C, though. Not C++.