« first day (1031 days earlier)      last day (2595 days later) » 

10:18 AM
in Lounge<C++>, 2 mins ago, by R. Martinho Fernandes
@ProblemSlover Which low-level pointer details help with understanding high-level topics like ownership semantics?
incidentally
 
10:31 AM
argh
I like C better.
it is painless :D compared to C++
I like painless languages like C, Java and Python :P
before, I used to be a masochist and like languages like Basic, Perl and C++ :D
 
@AnttiHaapala really.
@AnttiHaapala really
that's a very meh troll
1.5/10
 
I am not going to write anything in C that I could write more efficiently in Python.
 
I liked the first part of that sentence
 
hmm comment linking times out :(
idk what's happening here
it seems that you've not noticed yet, but this room is a C room, and C stands for circle j...
4
 
 
3 hours later…
1:44 PM
Hi all ... can someone explain this to me:
for(; val && i ; --i, val /= base)
buf[i] = "0123456789abcdef"[val % base];
I know what it does but I just don't understand how
 
and val is initialized with what?
there is just a for loop...
and an index going down
same as the code we discussed yesterday.
 
@louigi600 it keeps dividing the number by base which effectively is a crude way of converting to string representation
 
with a for loop.
 
every iteration results with one digit being printed
 
@BartekBanachewicz it keeps dividing by base...
works for any integer base up to 16.
 
1:48 PM
right
 
the for loop : why no init values ?
the buff array assignment ... that's wierd
buf[i] = "0123456789abcdef"[val % base];
does that mean put in buf[i] the (val % base)'th element of the string array "0123456789abcdef"
?
 
@louigi600 yes
 
also the for loop: does that mean keep on doing --i and val /= base
untill (val && i) is true
 
static char buf[32] = {0 };
is that a 32 element array of "\0" characters ?
or is that just the first element that is "\0" ?
 
1:58 PM
@louigi600 the latter
I mean all those are basics of C really
 
hum ... I did the basics of C a while ago and I never sau those notations
and I'm still puzzeled how the buff array is actually composed
#include <stdio.h>
char* itoa(int val, int base){
  static char buf[32] = {
    0  };

  int i = 30;

  for(; val && i ; --i, val /= base)
  { printf("%i:%i , ",val,i);
    buf[i] = "0123456789abcdef"[val % base];
  }
  printf("\n");
  return &buf[i+1];
}

int main ()
{ printf("%s\n",itoa(1234, 10));
}
i did this to try and understand better ...
# ./a.out
1234:30 , 123:29 , 12:28 , 1:27 ,
1234
now if buff[0] is "\0"
and then elements 27, 28,29 and 30 are "1234"
the array is null terminated at the start .... why is it printing 1234 ?
 
@louigi600 array of 32 0 characters
@BartekBanachewicz and not this one
unless by the latter you mean the former
 
21 If there are fewer initializers in a brace-enclosed list than there are elements or members
of an aggregate, or fewer characters in a string literal used to initialize an array of known
size than there are elements in the array, the remainder of the aggregate shall be
initialized implicitly the same as objects that have static storage duration.
static char buf[32] = {0}; is the same as static char buf[32] = "";, it will initialize every single character to 0; however, static char buf[32]; would also be initialized to contain all 0.
@AnttiHaapala You did not read my answer fully. Undefined behaviour does not mean completely unpredictable behaviour for any particular implementation. Try running the first example with your compiler (you'll need to ensure stdout is flushed after calling Test) and it will most likely print "world". — JeremyP 22 mins ago
hardheaded are we
 
ok the bud array has null terminators in front of the text ... how is it printing anything at all ?
 
2:12 PM
return &buf[i+1];
or return buf + i + 1
 
@AnttiHaapala oh that's why
so yeah, { 1 } would initialize first to 1 and rest to 0s
 
2:49 PM
I am getting pissed off by that JeremyP
 
@AnttiHaapala this guy is something else...
 
exactly the kind of person who annoys people by actually reasoning how compilers must compile the code - when they don't do it that way.
Guy's top tag is
 
> even the str == NULL will have undefined
 
I wonder how many different Objective-C implementations there are
 
I think you meant != ?
 
2:51 PM
yes :D
but, well, both of them are undefined
 
yes
 
ooops I missed the i+1 in the returned address to the buff string array
thanks for the patience ... now it's clearer
why did the author start out with a 32 element array ?
to allow for converting a 32bit integer into a 32 character array ?
but then there's no space left for the null terminator !
 
3:13 PM
@louigi600 pebkac error :P
it should be 33 for binary :D
it is also using a static buffer, that should probably be thread-local
 
3:30 PM
I was surprized gcc didn not complain about returning an address to a local variable unknown to main. .... but I'm no developer (just sysadmin) so I just said to myself that it;s odd
 
3:45 PM
it is because it is static
it continues to exist
if you remove static gcc will warn
warning: function returns address of local variable [-Wreturn-local-addr]
   return &buf[i+1];
          ^~~~~~~~~
 
@louigi600 wihihi. I would have guessed about Your sysadmin'ish soul because of this: for(; val && i ; --i, val /= base) :D
 
lol
 
4:33 PM
do any of you use register variables?
 
@AnttiHaapala Although this Jeremy doesn't present much of a case... technically speaking you still ignore the idea of "might be" undefined
 
@DrorK. might be undefined... meaning what :D
 
That whether something is UB is conditional
 
unfortunately, indeterminate values are no longer conditional :(
 
Why they are no longer conditional? Because an opinion?
 
5:35 PM
because the committee response and because how pretty much all implementations behave now.
 
The committee is entitled to their own opinion, and even if all the implementations behave the same, the question of whether something is UB or not, is still conditional
 
9
Q: C - is an indeterminate value indeterminable?

dtechAccording to this post an indeterminate value is: 3.17.2 1 indeterminate value either an unspecified value or a trap representation According to google, the definition of indeterminate is: Not certain, known, or established Left doubtful; vague. According to thefreedictionary, determinable...

 
Just as we wouldn't accept the idea of something "not" being UB because all implementations chose to behave in this or that manner
 
the reading of the standard is that indeterminate value means that any value can be chosen at any instant.
not that it needs to settle in any one value.
 
Did I say otherwise?
 
5:37 PM
so if a compiler decides it will store a ptr in a register - after free, it can mark it as indeterminate
 
The standard didn't specify such a liberty, but even if this or that implementation had chosen to do such a thing- then it would become UB
 
and so if you access it after it has been spilled
 
Otherwise it wouldn't
 
1 indeterminate value
either an unspecified value or a trap representation
1 unspecified value
valid value of the relevant type where this International Standard imposes no
requirements on which value is chosen in any instance
 
I was referring to the 'register' suggestion ^
 
5:39 PM
what do you mean?
the standard says: the value of a pointer is indeterminate after the lifetime of the object it points to
I am just telling how it could work in practice
(or how it would work)
 
And how that makes it undefined behavior for all implementations?
 
it doesn't do :D
 
Not required to work on all implementations, is not the same as being undefined behavior for all implementations
 
however, in practice, if you look at Jeremy's profile
his best tag is
3500 score.
 
I have a feeling he could have a field day with my SO reputation :)
 
5:42 PM
there is only one substantial Objective-C implementation currently in use... the one Mac OS X and IOS use
and they use the Apple's clang compiler
and I assure you... they're optimizing as aggressively as GCC, and they certainly do not guarantee this behaviour that Jeremy takes for granted.
 
Again, I'm not making his case, I'm simply saying the above ^
Not required to work on all implementations, is not the same as being undefined behavior for all implementations
 
sure.
but in this particular case his argument is: "it works in my compiler, therefore not UB"
anyway the whole question is utterly awful and if they asked that in an interview... and the correct answer was that.
though I guess it is that they asked it in the interview but then the OP tried at home and thought that those were the correct answers.
when the correct answer would have been "UB" and "UB" :D
 
Should've been: might-be UB, and UB
 
Because the first UB is conditional
The second is not
 
5:50 PM
why is the first ub not conditional?
 
The first is, you're asking about the latter?
Err
I was referring to your comment about the if statement
I didn't even notice the block following it in the OP
I was reading just the thing you linked: "even the str == NULL will have undefined behaviour. "
 
void Test(void){
  char *str = (char *) malloc(100);
  strcpy(str, “hello”);
  free(str);
  if(str != NULL){
    strcpy(str, “world”);
    printf(str);
  }
}
 
Technically speaking, even that is "technically" conditional
 
this is the code I am talking about - I incorrectly wrote str == NULL, should have been str != NULL
 
Because it depends on whether the could-be unspecified value being null or not
 
5:53 PM
yes, but he didn't make that case :D
 
But I guess that's too technical :)
 
he said this will almost certainly print "world"
but what was more interesting is that for me it didn't
 
That's because you're doing it wrong!
 
it was just a crash.
 
It reminds me of an old debate in ##c
 
5:55 PM
% ./a.out
zsh: segmentation fault (core dumped) ./a.out
I even used puts so it should flush outright :)
it is because the stdio in Glibc uses malloc the first time it runs.
so it will overwrite the terminator / or the malloc will fail, because the free linked list is contained within the memory
 
It reminds me of: char c; strlen(&c); ... (C99) ... need not be UB!
 
well, I wouldn't count on it either :D
 
you're using the wrong compiler, duh ;)
 
6:24 PM
5.1.2.3 rule 3. Probably not important, but what is that rule stating?
evaluation as in, a = 1 + 2?
if thats the case, how can you A not be sequenced before or after B, wouldn't it not exist?
oh, i guess i should look at annex C -_-
> Accesses to volatile objects are evaluated strictly according to the rules of the abstract
machine.
where are the rules of the abstract machine 0_o
 
7:25 PM
@Kevin in the standard
1 The semantic descriptions in this International Standard describe the behavior of an
abstract machine in which issues of optimization are irrelevant.
what the standard says is that the behaviour needs to be the same as it would be if it ran in the abstract machine.
but the reads and writes to non-volatile variables need not occur, if it can be guaranteed that they're not needed - one can optimize them out.
but reads and writes to volatile variables must be strictly writes to and from memory.
 
8:17 PM
gotcha. thanks.
 
8:52 PM
@AnttiHaapala If I remember correctly the implementation-defined nature dropped the ball on this thing too ^
So implementations have taken the liberty to optimize things, and that's why "memset_s" considered to be the only 'safe' method for such a requirement
 
9:22 PM
hmm :)
hmm
memset_s not in glibc
@DrorK. because memset args are not volatile-qualified :P
 
I didn't say it was portable :)
 
9:52 PM
Why is memset_s better than memset?
 
> Unlike memset, any call to the memset_s function shall be evaluated strictly according to the rules of the abstract machine as described in (5.1.2.3). That is, any call to the memset_s function shall assume that the memory indicated by s and n may be accessible in the future and thus must contain the values indicated by c.
Optimizers may look at a memset call and deduce that the object in the call will never be accessed after, and remove the call to memset.
 

« first day (1031 days earlier)      last day (2595 days later) »