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

Pages: 1-4041-

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).

Name: Anonymous 2012-01-21 4:05

>>39
What if num is 0?
implementation defined

the rest is undefined

Name: Anonymous 2012-01-21 8:38

printf("%zoom %zoom %zoom",mazda3,mazda5,mazda6);

Name: Anonymous 2012-01-22 4:21

>>44
Nice dubz bro.

Name: Anonymous 2012-01-22 4:29

>>43
thanks bro

Name: Anonymous 2012-01-22 5:39

Well for one thing arr + max is clearly out of bounds.

Name: Anonymous 2012-01-22 9:05

Can anyone help me to get passed this :  int[] array = new int[verk2_tala1.Value, verk2_tala2.Value, verk2_tala3.Value];

Error    3    Cannot implicitly convert type 'decimal' to 'int'.

Name: Anonymous 2012-01-22 9:11

>>46
Nigga what the fuck are you doing?


int array[] = { v1,v2,v3 };

Name: Anonymous 2012-01-22 11:31

>>45
nope, num > max

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-22 11:49

>>48
And I'm sure you're also aware of the fact that unsigned arithmetic in C doesn't follow regular addition and subtraction rules.

Name: kodak_galllery_programmer !!kCq+A64Losi56ze 2012-01-22 11:59

>>49
So in other words, could it be....

no way....


wait for it...













YOU MIGHT NOT ONLY HAVE UNDEFINED BEHAVIOR, BUT YOU WILL MIGHT ALSO HAVE A MEMORY LEAK.




WELCOME TO C. THE LANGUAGE THE PROVIDES YOU WITH ENOUGH TOOLS TO FUCK YOURSELF.

Name: Anonymous 2012-01-22 12:10

>>49
>>50
you seem really tense these last few days with your posts.

Did they announce when you're officially going to be a jobless_programmer!!kCq+A64Losi56ze?

Name: Anonymous 2012-01-22 12:19

>>51
I was just trying to help this foolholio by pointing out something that isn't obvious.

Name: Anonymous 2012-01-22 12:22

>>52
I'm always here for you kodak-san when you need a shoulder to cry on.

perl girl is dead

Name: Anonymous 2012-01-22 12:49

>>53
Is Uri still fagging up the perl newsgroups? Oh wait, never mind, I can just go wander over there right now.

Name: Anonymous 2012-01-22 14:08

>>51
The good side of him being fired is cosmic justice, the bad side is he'll have even more time to waste here. Oh well, we can always hope for an endless spiral of unemployment, alcoholism and homelessness.

Name: Anonymous 2012-01-22 14:12

>>55
Well, if all else fails, I'll just go pursue my Ph.D in Chemistry.

Name: Anonymous 2012-01-22 14:15

>>56
Why not pursue a meth addiction instead?

Name: Anonymous 2012-01-22 14:16

>>57
Been there, done that.

Name: Anonymous 2012-01-22 14:19

But seriously, ever since Kodak filed chapter 11, I've given a lot of thought about possibly pursuing my Ph.D in Computational Fluid Dynamics at Caltech

Name: Anonymous 2012-01-22 14:20

>>57
I have meth sores.


Never again will i do meth.

Name: Anonymous 2012-01-22 14:20

>>59
Whatever, as long as you fuck off.

Name: Anonymous 2012-01-22 14:22

>>61
You're just mad because you can't make it out of tech support.

Name: Anonymous 2012-01-22 14:25

>>59
Says the guy who has not a clue about elementary mathematics.

Name: Anonymous 2012-01-22 14:26

>>63
Believe it or not, my undegrad degree is in Mechanical Engineering from the University of Wisconsin.

Name: Anonymous 2012-01-22 14:31

>>62
No, I'm annoyed because you're an insufferable cunt.

Name: Anonymous 2012-01-22 14:55

Check my dubs

Name: Anonymous 2012-01-22 21:11

Nobody is even close yet.

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