« first day (1032 days earlier)      last day (2594 days later) » 

6:40 AM
int i = getc();
if (i != EOF) {
    char c = i;
}
conditional undefined behaviour
 
6:52 AM
@AnttiHaapala Why?
 
7:15 AM
just joking :P
 
I always wondered about the 'implied' limitations/assumptions the standard library introduces
 
in a IEEE implementation, what is the output of the following program:
 
I never seen a direct DR or a direct committee response to actually say what they think of it
 
float f = 0.1;
if (f > 0.1) {
    printf("Greater");
}
else if (f < 0.1) {
    printf("Less");
}
else {
    printf("Equal or NaN?");
}
what's the output of the program
 
Less?
 
7:20 AM
greater
this was from so docs actually :D
so they're not completely useless.
 
I'm not sure what it's supposed to demonstrate?
 
printf("%.60f\n%.60f\n", 0.1, 0.1f);
0.100000000000000005551115123125782702118158340454101562500000
0.100000001490116119384765625000000000000000000000000000000000
well just that floating point constants are doubles by default
 
So the 'obvious' answer people assuming is that it's Equal or NaN?
 
and that floats undergo promotions to doubles
yes...
or then the second guess is that it is "less", but floats are not floored but rounded.
 
Oh, I always select the average selection. So given 3 options, that would be "Less" :)
Average/middle
Given 4 options- and I'm stumped!
 
7:26 AM
4 options -> the closest to average? :P
 
The closest to average from the middle
 
 
2 hours later…
9:28 AM
Hi all
how do I do this right ?
static char buf[5] = { 0 };
....
buf="Err!";

while still maintaining the buff static definition as is
 
@louigi600 what's "right"?
 
this works but is probabbly not the neatest way to do it:
buf[0]='E';
buf[1]='r';
buf[2]='r';
buf[3]='!';
The first one :
buf="Err!";
produces
t.c:15:8: error: incompatible types when assigning to type 'char[5]' from type 'char *'
buf="Err!";
 
@louigi600 strcpy_s(buf, sizeof(buf)/sizeof(buf[0]), "Err!");
 
string.h will burden a little ... is there a way without it ?
 
@louigi600 will what?
 
9:31 AM
it's going to run on an arduino
 
so? it has string linked in anyway
 
32 kb of program space and 2kb of ram
 
yes I do code on arduino as well
and I'm not sure what the problem is with using strcpy. I code in C++ on it and it runs just fine.
 
it will runn but it will increase the footprint of the program that gets uploaded ...
I did say it wil burden != will fail :P
 
@louigi600 what's the problem with the increased footprint?
 
9:34 AM
I want more space for my code
 
@louigi600 and you're running out currently?
 
@louigi600 Using a header does not actually increase the size of the program. Linking stuff in will.
 
what is the binary size for the file that has strcpy linked in?
 
not yet ... but I just would like not to use string functions
 
@louigi600 why?
 
9:35 AM
it's against my religion on microcontrollers :D
 
They provide the means to perform tasks that would be difficult / impossible to perform in portable manner otherwise, so why would you not use them?
 
@louigi600 good luck looking for a solution to your problem then. You'll reinvent strcpy anyway.
probably in a worse way
 
on a microcontroller you may not have a display ... and in my case I'll only have a 4 digit display ...
 
@louigi600 so what?
 
would you use atomic bomb to kill an ant ?
 
9:37 AM
@louigi600 false premise. strcpy is not an "atomic bomb"
#include <string.h>

char p[10];

void setup() {
  // put your setup code here, to run once:
  strcpy(p, "test");
}

Sketch uses 498 bytes (1%) of program storage space. Maximum is 32,256 bytes.
go figure
 
crist I know how to use strcpy
 
@louigi600 look at the last line
 
that's 1/2 kb for 1 line
that's the burden I'l talking about
 
void setup() {
  // put your setup code here, to run once:
  p[0] = 't';
  p[1] = 'e';
  p[2] = 's';
  p[3] = 't';
  p[4] = 0;
}


Sketch uses 444 bytes (1%) of program storage space. Maximum is 32,256 bytes.
@louigi600 or is it?
 
instead of arguing with me can anybody show me a an alternative w\o string functions ?
 
9:40 AM
@louigi600 no, because every alternative involves reimplementing said string functions
 
@BartekBanachewicz ... yes I did that ... is there a neater way ?
 
@louigi600 yes, using strcpy
 
so only neater way is with string functions ?
 
Or something that will essentially do exactly the same thing as them, yes. I mean, strcpy is as primitive as it gets
@louigi600 if you hand-roll your own strcpy implementation you can save like 10 bytes
which I am sure is a matter of life and death and religion now
 
for a display that only fits at most 4 characters I still think that using string functions is like using an atomic bomb to kill an ant
had I an lcd with 80 or more characters I'd be happy with using string functions
 
9:46 AM
@louigi600 well, you're wrong
 
@BartekBanachewicz
buf[0]='E';
buf[1]='r';
buf[2]='r';
buf[3]='!';
buf="Err!";
is a lot smaller the 498 bytes
nad that would be the only place I use string functions
 
@louigi600 yes, it's exactly 444 bytes
so you're saving 54 bytes by doing it this way, and losing readability, maintainability, and you open yourself to bugs
 
hum ..... then there is a lot of other libs that get compiled ....
ok string funcrions don't impact 1/2 kb
 
@louigi600 you pay for what you use. Only the functions you use get linked in
 
or they are pulled in by some other arduino libs ... so might aswell use them
 
9:49 AM
I am pretty sure 400 bytes is the bootstrapping code for libc and the device itself
it needs to initialize memory manager, registers, stack, BSS etc
 
yeah .... 44 against 98 ... that's using spary to kill a single ant ... which is more appropriate then nuclear weapon :P
and is probabbly more appropriate then attempting to use a pin to prick the single ant
 
@louigi600 I use C++ standard library with vector, unique_ptr and friends and it fits just fine in 32kB :P
 
@BartekBanachewicz ... not on the arduino nano
 
@BartekBanachewicz that's what, AVR or ARM? Which toolchain?
 
nano is awr at328x
 
9:53 AM
@louigi600 Nano has exactly the same CPU as Uno, with 32kB flash and 2kB RAM. And yes, it fits C++ just fine.
 
32kb program space and 2kb ram
ARM arduino has like 64Mb of ram .....
 
@Kamiccolo AVR. I used official arduino tools then I switched to something else
I think simply avr-g++-none-eabi
but I'm still planning to build Terra against LLVM 4.0 and try it out on arduino
 
once I'll get my thing done I'll go about using this :balau82.wordpress.com/2011/03/29/…
 
this might be a really better language than C or C++
@louigi600 really not much to gain by writing registers directly
I mean, typical C APIs for embedded devices are pretty shitty, but they at least work
Arduino's hardware SPI lib is pretty solid, for example
 
you can actually still write and read whole registers in arduino language
 
9:58 AM
"arduino language" is basically C++ with some trivial preprocessing
 
you can use any awr-libc call
 
anyway, it's not that hard to get a standalone compiler to build matching binaries nowadays
and then you can use C++14 and all the niceties
and I guess static-if in C++17 is gonna be useful on embedded
 
I'm reading a whole prt register to see if one of maby buttons is pressed ... much faster then checking every single one separately
 
vOv typically irrelevant anyway
 
:P
I guess so ...
 
10:01 AM
Arduino APIs are targetted at beginners and they do a great job of introducing people to embedded programming
but they are also really nice for prototyping
 
but thank;s for the hints on the C relavent stuff
 
@louigi600 you should drop the suppositions and religions, that's the main hint. Optimize when you have to, not when you can.
Focus on correctness and readability.
 
:)
 
@BartekBanachewicz A star for the latter part.
 
10:36 AM
@louigi600 I don't really see the problem. If you can't/won't afford the extra 'cost'... why don't you simply stick with your "affordable" solution?
 
@DrorK. ?
there were many messages after the question
 
If you're looking for an adventure, I suppose that you could write a wrapper function to unroll such char-by-char assignments, I never seen such a thing- but that's your prerogative
 
I think you could use a template for that
template<int i, int n> void assign(char* dest, const char* buf) {
    dest[i] = buf[i];
    if (i < n)
        assign<i+1, n>(dest, buf);
}
you can do the if part using regular SFINAE
 
if anybody is curious this is the whole context of the original question I posed:
char* itoa(int val, int base)
{ static char buf[5] = { 0 };
  int i = 3;
  int sign;

  val >= 0 ? sign = 1 : ( sign = -1);
  if ( val >= (base * base * base * base) || val <= -base * base * base || base > 16 || base <= 1 )
  { buf[0]='E';
    buf[1]='r';
    buf[2]='r';
    buf[3]='!';
    return &buf[0];
  }

  if ( val == 0 )
  { buf[3]='0';
    return &buf[3];
  }

  if (sign < 0 ) val = -val ;
  for(; val > 0 && i >= 0 ; --i, val /= base)
 
please hit Ctrl-K on this
@louigi600 um, itoa is also in the standard library
 
10:45 AM
atoi is
 
@louigi600 itoa is as well
 
I'm not sure that itoa is
the equivalent implemented in vprintf
 
and vprintf is called by *printf
 
@louigi600 oh wait, you're right actually
> This function is not defined in ANSI-C and is not part of C++, but is supported by some compilers.
@louigi600 Arduino IDE has it, anyway
@louigi600 well so why don't you use sprintf
 
10:49 AM
sprintf burdens 2.3kb and that's a lot on the 32Kb
 
void my_itoa(char* dest, int num) {
  if (num < 10000)
    itoa(num, dest, 10);
  else
    strcpy(dest, "Err!");
}
564 bytes
 
yes but arduino's itoa does not check that the stuff will fit on a 4 digit display
 
@louigi600 Read my message again? Just the one above.
I didn't do base, but obviously it's easy enough to include if you need different bases
 
on my code if I used sprintf it woyld increase code size by 4%
 
4% doesn't sound that much
 
10:52 AM
^
also again, see above. 564 bytes with both itoa and strcpy
 
Especially if you happen to need it more than 1 time
It'd still seem like you're fighting the wrong battles.
 
maybe your arduino ide is different to mine .... or it's set up for to target a different instruction set/cpu ....
my IDE tell's me that by using sprintf code grew 4% sith respect to what I had before I included the sprintf
ok 4% is not 2.3 Kb ... but it's close to 1.3Kb on the 32Kb nano
 
No, mine's a UNO, basically the same as Nano. And 4% is not that much, escpecially if you need the functionality it brought in more than 1 place
 
I'm not using sprintf anywhere else
 
It's prolly also pulling in other standard library functions used by sprintf, that you might end up using yourself. Still the wrong battle.
And as has been pointed out already, if you want to do this as an exercise, go ahead. But in real programs, stick with the standard functions until you really cannot.
 
11:00 AM
so I decided that foe just once I could do without using avr-libc's vprintf ...
glibc's vprintf is over 2000 lines of code ... not sure on avr-libc's implementation
surely 25 linse is better then 2000 lines if I'm going to use it just once
 
No, 25 lines could be a lot worse, but that'd depend on the situation.
 
sure ... whatever you say ... on your arduino you can upload whatever you want
 
I do. The standard C++ library stuff, to be exact.
The point is that your 25-line home remedy might work for you just fine, especially if you're not handling values from some uncontrolled source and can be sure that no odd corner cases will have to be handled.
 
well mycode checks that the input values are within the capabilities of the display I'm targeting ... so I think it can deal with reading crap from analogue pins
and I check that the previous statement holds true for any base my converter can handle
 
This argument is pointless. You've decided that the libc functions are too big for you, so don't use them.
And yes, sprintf could be overblown for your simple max-4-digit-num-to-string. @BartekBanachewicz provided you with a good solution for that.
 
11:15 AM
I used to play with pic micro-controller and I used it's native assembly language ... so I'm used to doing my own stuff. ...
I'm not saying that my converter is better then vprintf ... but for my application it is
in any case my dayjob is unix sysadmin .... I do the microcontroller and programming as a hobby ... for me personal satisfaction comes before other stuff that would not hold true if I were to do it for a living
I'm sure my code is horrific to the eyes of experienced programmers ... maybe I'll get better at writing neater code over time
 
 
2 hours later…
1:06 PM
@louigi600 one way to do that is to listen to feedback
 
1:51 PM
my fault : I do appreciate the advice and I do learn from it even if I still think that for my specific need my horrific code is better suited for what I'm targeting.
But I do agree with you that if I were to get paid for this I'd have a different approach.
I'd have never learned about doing something like this
buf[i] = "0123456789abcdef"[val % base];
and also loops with multiple looped variables
for(; val > 0 && i >= 0 ; --i, val /= base)
had I not looked into doing this myself
 
2:02 PM
but those things are clever hacks
most of your code shouldn't look like that
 
in fact that's not my code .... but I learned about it
and I learned from it too
had I just used sprintf I'd have never learned about/from it ;)
 
2:22 PM
@louigi600 you'd have learned different things
 
clever hacks are interesting things to learn about ...
 
@louigi600 I dunno, really. C isn't a very interesting language for me
most of its "hacks" is just pointer arithmetic of some sort
but the things you can do with that in the end are rather primitive
compared to e.g. code generation possibilities in C++ templates...
 
I've another C question specific for arduino:
this snipet initializes digital pins 2 to 13
  for (int i=2;i<=13;i++)
    pinMode(i,OUTPUT);
can I do the same for the analogue pins ?
well not all as output just 0 to 5
6 and 7 cannot be used as digital outputs
 
2:57 PM
will something like this work ?
static const uint8_t analog_pins[] = {A0,A1,A2,A3,A4};
for (int i = 0; i < 5; i++)
pinMode(analog_pins[i]);
 
3:26 PM
got to go .... I'll give it a try at home
 
3:43 PM
@louigi600 why not?
 
4:12 PM
@louigi600 except if you're using Arduino IDE, then use C++!
static const uint8_t analog_pins[] = {A0,A1,A2,A3,A4};
for (uint8_t pin : analog_pins)
    pinMode(pin, ANALOG);
much cleaner than your loop and more robust
 
 
2 hours later…
5:48 PM
I'm having a problem trying to replicate buffer overflow using custom shellcode, here's a link to my question in full. If anybody could take a look and explain to me what i might be doing wrong i'd appreciate it a lot.
http://stackoverflow.com/questions/42934121/trying-to-craft-a-shellcode-exploit-can-anybody-spot-what-might-be-wrong-with-t
 

« first day (1032 days earlier)      last day (2594 days later) »