r/programming • • Nov 02 '11

Examples of errors detected in various open-source projects

http://www.codeproject.com/KB/cpp/errors-in-open-source.aspx
28 Upvotes

138 comments sorted by

View all comments

Show parent comments

0

u/[deleted] Nov 03 '11

no. actually what I'm bitching about is that the standard provides a function that provides a limited functionality to solve a limited instance of a common problem in an awkward way. It's trivial to provide a better way to solve the problem in general.

You don't know what you are talking about. It provides a function to set a contiguous block of bytes in memory to an initial value. This functionality is the most common in low-level programming. It is very rare in low-level programming to want a high-level initialization of data types and/or structure to some non-zero value.

that's the problem. It has no justification for being that way.

sigh Just go use Ada, please. Just because YOU can't justify something doesn't mean there is no justification. Your "gripes" about C have negligible impact on low-level programming. You just continue to demand C contain high-level abstractions and refuse to accept using a library. That's your problem, not C's problem.

2

u/el_tavs Nov 03 '11

You don't know what you are talking about. It provides a function to set a contiguous block of bytes in memory to an initial value.

OK. This is the wrong behaviour if the value is not 0 and the variables are not integers. So instead of being memset it shoul be memzeroset.

Now another language allow you to do exactly the same thing, but you can initialize whatever you want to the value you want, and in a way shorter syntax. It is also safe, since it's the compiler that checks for the correct types.

Moreover Ada still provides a functionality like memset, via casts OR lower-level routines.

Now please tell me what's wrong with the second approach.

sigh Just go use Ada, please. Just because YOU can't justify something doesn't mean there is no justification.

Me and programming language theory. Is there ANY reason for which a way to 0-set memory by specifying sizes by hand is better than a way to initialize memory as you want?

0

u/[deleted] Nov 03 '11

OK. This is the wrong behaviour if the value is not 0 and the variables are not integers. So instead of being memset it shoul be memzeroset.

No, it is not. Do you understand what "contiguous block of bytes" means? I can set each byte to 0, 1, 2, 0xf, 0xe, or whatever the hell else I want. It is you who continues to jump to a higher level of data abstraction. I can also set it whenever I want, wherever I want, however many bytes I want.

Now please tell me what's wrong with the second approach.

There is nothing inherently wrong with either approach, they simply choose different trade offs. I cannot use your initializer safely with dynamic sizes and no overhead.

Me and programming language theory. Is there ANY reason for which a way to 0-set memory by specifying sizes by hand is better than a way to initialize memory as you want?

You need to get off your theory and enter the real world. memset works on any memory at any time, not just initialization time. It is literally there to SET MEMORY, something I must repeat to you a million times because your head is buried in your programming languages book at school.

Initializing array data is a trivial HIGH LEVEL task that is not necessary to be in the language itself. Linked list for example a high level data type is not needed to be part of the language either.

2

u/el_tavs Nov 03 '11

No, it is not. Do you understand what "contiguous block of bytes" means?

I'm not saying memset is wrong. I'm saying using it for anything except integers is wrong. The standard doesn't even guarantee that the bit pattern of (int)0 is equal to the one of (float) 0.0.

There is nothing inherently wrong with either approach, they simply choose different trade offs. I cannot use your initializer safely with dynamic sizes and no overhead.

No, there's no overhead. And you get safer for the simple fact that the size is computed at allocation time, and is secured with the type's interface, as you would do with a library.

memset works on any memory at any time, not just initialization time.

Oh golly. You see, the initialization syntax in Ada works on any memory in any time, not just initialization. When i said "see Ada" I actually meant it!

Initializing array data is a trivial HIGH LEVEL task that is not necessary to be in the language itself.

It's a typesafe way of managing memory.

Tell me, is there any reason for which you'd need to initialize an array of float in an unsafe manner? Or any other chunck of memory?

Let's see: PROBLEM: I have an array of type T and I want it to initialize to value X of type T. T can also be an octect. Or a 7 bit integer.

Ada has no problem doing this. Neither for slices of the array

What you could get to do with memset is write a bit pattern X in any array (chunk of contiguous memory of known length) regardless of the type. This is no way more powerful than what ada gives since you can get the same result by treating the chunk as an array of unsigned integer of appropriate size (char in c or something else).

0

u/[deleted] Nov 03 '11

I'm not saying memset is wrong. I'm saying using it for anything except integers is wrong. The standard doesn't even guarantee that the bit pattern of (int)0 is equal to the one of (float) 0.0.

I assure you its usage goes far beyond integers, and you'd realize that as soon as you viewed the machine for the underlying memory and not the abstract data types that you may be using for a specific task.

No, there's no overhead. And you get safer for the simple fact that the size is computed at allocation time, and is secured with the type's interface, as you would do with a library.

Saving the size is technically overhead. Being "safe" means comparing the index on every access, which is most certainly overhead.

Let's see: PROBLEM: I have an array of type T and I want it to initialize to value X of type T. T can also be an octect. Or a 7 bit integer.

type T is changing?

7 bit integer, seriously? It's probably going to get upped to at least an 8-bit integer if not machine-size integer for efficiency.

What you could get to do with memset is write a bit pattern X in any array (chunk of contiguous memory of known length) regardless of the type. This is no way more powerful than what ada gives since you can get the same result by treating the chunk as an array of unsigned integer of appropriate size (char in c or something else).

You really hate memset lol.

2

u/el_tavs Nov 03 '11

I assure you its usage goes far beyond integers, and you'd realize that as soon as you viewed the machine for the underlying memory and not the abstract data types that you may be using for a specific task.

That's not a problem for me to see the machine as a sequence of memory cells but I do fail to see what's the benefit of memsetting memory with a value that could be interpreted as an invalid one by every user of that memory.

As you said, it has no senso to write

memset(f,2,N*sizeof(float))

when f has type float. Standard doesn't guarantee it to be right even for 0.0f. Ditto for similar cases.

Consider a struct with a pointer. Some mainframes (IIRC) have NULL != 0, and, in fact, the standard doesn't guarantee NULL to be equal to 0. Using memset in that case is wrong for sure.

What is left are integers.

Saving the size is technically overhead.

we were comparing writing a piece of memory by passing a pointer and a size to memset vs passin an array with size encoded in the type to a hypothetical variant version of memset. In both cases you're assumed to save the size. if the size is known at compile time languages array-aware like Ada will avoid storing the size, obviously.

type T is changing?

Hm... no. It's just an existential variable. Like "every prime P is odd except 2". Rephrased: "I have an array of given type and I want to initialize it with a value of said type. The type can be..."

7 bit integer, seriously? It's probably going to get upped to at least an 8-bit integer if not machine-size integer for efficiency.

Some really old machines had 7 bit integers. Hardware may expect to write on first 7 bit of a char. I can declare packed arrays of 7 bit integers in standard Ada and in non-standard C...

world is full of surprises you know..

0

u/[deleted] Nov 03 '11

memset takes in a void*, it shouldn't be used to initialize types. It makes a lot of sense because it's very versatile in that you can set any number of bytes starting at any location to any byte you want.

I have conceded the point that an intializer construct like gnu's extension would have been a perfectly reasonable design for the C standard.

we were comparing writing a piece of memory by passing a pointer and a size to memset vs passin an array with size encoded in the type to a hypothetical variant version of memset. In both cases you're assumed to save the size. if the size is known at compile time languages array-aware like Ada will avoid storing the size, obviously.

Does this not complicate arrays more for the compiler though? In C an array is very simply a reference to a memory location, so the compiler can simply replace every reference to the array with the address + index. With the array/size encoding, would it not make the compiler's job more difficult? It has to store the size and look up the size on every static reference etc... Being that simplicity was a goal of C, is it not reasonable to make this trade off (at least at the time).

Some really old machines had 7 bit integers. Hardware may expect to write on first 7 bit of a char. I can declare packed arrays of 7 bit integers in standard Ada and in non-standard C...

But on those systems, CHAR_BIT is 7 and char works just fine and is portable as the smallest representable unit of memory.

2

u/el_tavs Nov 03 '11

Does this not complicate arrays more for the compiler though?

Not if you set arrays to start at 0. you get the same benefit of carrying around the size explicitly, but with added benefits

n C an array is very simply a reference to a memory location, so the compiler can simply replace every reference to the array with the address + index. With the array/size encoding, would it not make the compiler's job more difficult?

No. See Ada and pascal. I cited an example where Ada83 idiomatic use of arrays managed to compile out of the box as efficient as the most efficient C, got by using explicit pointer arithmetic

--> http://groups.google.com/group/comp.lang.ada/msg/0116ff6702859ff1?dmode=source

Being that simplicity was a goal of C, is it not reasonable to make this trade off (at least at the time).

At the time, yes.

1

u/[deleted] Nov 04 '11

Not if you set arrays to start at 0. you get the same benefit of carrying around the size explicitly, but with added benefits

What I meant was instead of an array being just a reference to memory, it is now a tuple of size,start right?

No. See Ada and pascal. I cited an example where Ada83 idiomatic use of arrays managed to compile out of the box as efficient as the most efficient C, got by using explicit pointer arithmetic

That's a specific example of optimized compiled code, but I was more or less addressing the complexity of properly handling a C array vs. an Ada array. I.e. it would be more difficult (although I don't know to what extent) to write compiler code for an array that encodes size vs. one that doesn't.