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

C problem

Name: Anonymous 2012-01-18 19:35

Is the behaviour of this program well defined?

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>

int main() {
    size_t num, min, max;
    if(scanf("%zu%zu%zu", &num, &min, &max) != 3) return 0;
    if(min >= num || max >= num || min > max) return 0;
    int *arr = calloc(num, sizeof(*arr));
    if(!arr) return 0;
    int *x = arr + min, *y = arr + max, *z = arr + (y - x);
    *x = 1, *y = 1, *z = 1;
    return 0;
}

Name: Anonymous 2012-01-18 19:40

no

Name: Anonymous 2012-01-18 19:42

I like your coding style.

Name: Anonymous 2012-01-18 21:35

yes

Name: Anonymous 2012-01-18 22:30

Yes. I don't see any hacky code there.

Name: Anonymous 2012-01-18 22:47

Yes. Congratulations!

Name: Anonymous 2012-01-18 23:03


int *arr = calloc(num, sizeof(*arr));


hmm a variable is used in its own definition. It is perfectly sensible of course, but maybe that's waldo?

Name: Anonymous 2012-01-18 23:18

>>7
learn2C, that's a perfectly valid and good way to alloc memory from the heap with any type of primitive/struct pointer.


//This would be wrong, notice the missing * in this example
int *arr = calloc(num,sizeof(arr));

Name: Anonymous 2012-01-19 0:21

>>8
I'm not a C wizard, but isn't *arr dereferencing an uninitialized variable inside the declaration? Or does the sizeof operator work differently?

Name: Anonymous 2012-01-19 0:30

>>9
sizeof causes no dereference to occur. Only the size of the object (based on its type) is taken into account. This example, double *dp = alloc(sizeof *dp);, is from the C standard itself.

Name: Anonymous 2012-01-19 1:00

>>9

yesh what >>10-san say. Sizeof doesn't evaluate the expression, it just infers the type of the expression, and then retrieves the size of the expression's type.

Name: Anonymous 2012-01-19 16:19

Help guys!

Name: Anonymous 2012-01-19 16:20

>>12
Help my anus! ⌒‿⌒

Name: Anonymous 2012-01-19 16:27

>>11
Reread the standard you faggot. sizeof() can evaluate an expression.

Name: Anonymous 2012-01-19 16:34

>>11
Sizeof doesn't evaluate the expression, it just infers the type of the expression, and then retrieves the size of the expression's type.


And before you get your panties in a bundle, let me quote the standard...

"The sizeof operator yields the size (in bytes of its operand), which may be an expression or parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of operand is a variable length array type, the operarand is evaluated; otherwise the operand is not evaluated and the result is an integer constant."

Name: Anonymous 2012-01-19 18:36

>>15

#include <stdio.h>

int main(int argc, char **argv) {
    char a[argc];
    printf("%zu\n", sizeof a);
    return 0;
}


So, shouldn't that actually output sizeof (char *), given a is evaluated (since it's a VLA) and the value of an array is a pointer to its first element?

Name: Anonymous 2012-01-19 19:45

>>16
no

Name: Anonymous 2012-01-19 22:32

>>11

Actually, sizeof can evaluate an expression, specially in C99 where VLAs may apply. This code >>16 shows how a sizeof will actually evaluate to argc.

This indeed is not the case in the original code, though.

>>14
>>15

Hello, ``faggot''. Talking to "mental midgets" and "toilet scrubblers" once more?

Name: Anonymous 2012-01-19 22:39

>>18
Nah. I was too busy writing software.

Name: Anonymous 2012-01-20 0:04

>>18

nice. I didn't know variable length arrays were in the standard actually. I always thought they were a gnu extension. Good to know.

Name: Anonymous 2012-01-20 0:07

>>20
I think it's a C99 feature. So in other words, if you're using C89, all bets are off.

Name: Anonymous 2012-01-20 2:16

Check my well defined doubles.

Name: Anonymous 2012-01-20 3:11

>>22
nice dubs

Name: Anonymous 2012-01-20 6:23

>>18
It will evaluate to (size_t) argc.

Name: Anonymous 2012-01-20 9:03

>>21
If you're using C89 it won't compile.

Name: Anonymous 2012-01-20 12:34

>>20
>>21
Yes, VLAs are a C99 feature. Chances are these will become optional in C1X, however.

>>24
Yes. Whatever.

Name: Anonymous 2012-01-20 12:52

>>10,11
Thank you! I thought it did something like that, but I wasn't sure.

Name: Anonymous 2012-01-20 13:29

>>26

C1X is here already: C11 was approved in December.

Name: Anonymous 2012-01-20 14:03

>>28
Dude! No kid! I've got to rush and read up on it then.

Name: Anonymous 2012-01-20 14:09

>>29
Yes, feel the power of Generic Selections in C11.

Name: Anonymous 2012-01-20 14:20

>>30
C11 generic selection is shit, it's worthless.

Name: Anonymous 2012-01-20 14:40

>>29

No, you don't.

Name: Anonymous 2012-01-20 15:21

<< CHECK EM

Name: Anonymous 2012-01-20 19:16

For anybody wondering, the program is not well defined, although nobody has come close to the reason why yet.

Name: Anonymous 2012-01-20 19:30

>>34
I can see a lot of things wrong with it. The program has no intentional  side-effects so I will decline to comment on how well defined its behavior is. Print something or return something, right now it's a function of nothing.

Name: Anonymous 2012-01-20 20:37

>>34
Is it because the memory allocated by calloc isn't freed?

Name: Anonymous 2012-01-20 23:38

>>36
No.

Name: Anonymous 2012-01-21 1:53


is it the order of evaluation in:


    int *x = arr + min, *y = arr + max, *z = arr + (y - x);


?

Name: Anonymous 2012-01-21 3:54

What if num is 0? Also, this indirection (*x, *y, and *z) is not reliable.

Name: Anonymous 2012-01-21 4:05

>>39
The numbers are unsigned, and min and max both must be less than num for the code to proceed (see the second if statement).

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