lundi 30 mars 2015

Unsolved Quiz Shortest code



That s a test, follows code,you have to insert the shortest correction into the space without touching anything away from it /* TO BE DONE START / / TO BE DONE END */ that makes those two files give the same output


The shortest code has to be less than 80 characters counting the "/* TO BE DONE START // TO BE DONE END */" string


hello0.c



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

my_printf(char*p){
char s[strlen(p)+1];
strcpy(s,p);

/*** TO BE DONE START ***//*** TO BE DONE END ***/

}

main(){
my_printf("hello world!");
my_printf("How are you?");
my_printf("i\'m OK, and you?");
my_printf("1, 2, 3, testing ...");
my_printf("bye bye!");
exit(0);
}


hello1.c



#include <stdio.h>
#include <stdlib.h>
main(){
printf("Hello world!\n");
printf("How are you?\n");
printf("I\'m OK, and you?\n"
"1, 2, 3, testing ...\n"
"Bye bye!\n");
exit(0);
}



Datatype of pthread_t in pthreads?



I have a code in 32 bit in which int is used in place of pthread_t ? so while porting do i need to change to long in LP64 (64 bit). What is the datatype of pthread_t ?




DB2, to check size on C procedures



When defining a procedure like this:



create or replace procedure xxx.name
(in table_name varchar(6)
,in key_columns varchar(3000)
)
external name 'lib.so-VERSION!LIBNNN'
language c
parameter style sql
modifies sql data
commit on return no
program type sub;


Do I need to check the size of the input inside the C implementation of the procedure (so that I dont't risk buffer overflows)? ...Is there an error before the C code is invoked? I cannot run my own tests so any help is appreciated!




Good C-coding style for multiple lines if conditions



I am programming a project in C and have a problem: I've got a lot of if conditions and it might get really hard for other people to read. I haven't yet found a similar question on the internet.


Do you have an idea or example how to make my code more readable?


Here is the C Code:



if( ((g_cycle_cnt == uartTxSecondaryMsg[3][msgPos[3]].sliceNo) || //correct slicenumber...
(uartTxSecondaryMsg[3][msgPos[3]].sliceNo == -1) || // or as fast as possible...

( (uartTxSecondaryMsg[3][msgPos[3]].sliceNo == -2) &&
((uartTxSecondaryMsg[3][msgPos[3]].timeFrameBegin>=g_uptime_cnt) &&
(uartTxSecondaryMsg[3][msgPos[3]].timeFrameEnd<=g_uptime_cnt)))) &&

((dataProcessingFlag & SECONDARY_MSG_ANNOUNCED_CH4) == SECONDARY_MSG_ANNOUNCED_CH4) )



c - fix unknown register name ‘%xmm1’ in ‘asm’?



i am trying to build a project for ubuntu 14.04 X86 and I've got the following error:



error: unknown register name ‘%xmm1’ in ‘asm’
asm volatile (
^
error: unknown register name ‘%xmm0’ in ‘asm’
error: unknown register name ‘%mm1’ in ‘asm’
error: unknown register name ‘%mm0’ in ‘asm’
error: unknown register name ‘%xmm0’ in ‘asm’
asm volatile (
^
error: unknown register name ‘%mm0’ in ‘asm’
asm volatile (


in function :



static inline void
hev_bytes_xor_sse (guint8 *data, gsize size, guint8 byte)
{
gsize i = 0, c = 0, p128 = 0, p64 = 0;
guint64 w = (byte << 8) | byte;

asm volatile (
"movq %0, %%mm0\t\n"
"pshufw $0x00, %%mm0, %%mm1\t\n"
"movq2dq %%mm1, %%xmm0\t\n"
"pshufd $0x00, %%xmm0, %%xmm1\t\n"
::"m"(w)
:"%mm0", "%mm1", "%xmm0", "%xmm1"
);


gcc version 4.8.2




ncurses mvaddch() cursor movement not as expected



Code:



#include <ncurses.h>

int main(int argc, char **argv)
{
initscr();
noecho();
cbreak();


mvprintw(0, 0, curses_version());
mvprintw(1, 0, "Hello World");
mvaddch(2, 0, mvinch(1, 4)); // Why doesn't this work?

getch();
endwin();

return 0;


}


Output:



ncurses 5.9.20130608
Hello World


with pointer blinking (waiting for getch ) just after o of Hello.


Question:

As in C, arguments passed to a function are evaluated first before calling that function, mvinch() will be called first and when it'll return the character o the call to mvaddch() will be made.

But then why character o is not printed on line 2 (just below Hello World)? Instead mvaddch prints o at current cursor position (thanks to winch which is 1,4). Here mvaddch() behaves just like addch paying no respect to the mv prefix and the explicit movement coordinates given to it. Why?


Is this a possible bug in mvaddch() or am I missing something?




Xorshift pseudorandom number generator with shiftvalues in progmem



I started implementing a xor shift generator. To safe some RAM I tried to put the shiftvalues into the PROGMEM but this actually does make some "strange" results and i dont know why. It seems that it returns the same value for a long time, than get back to regular expected results.


I hope you can help me and show me what I have done wrong.



const uint8_t shift[] PROGMEM =
{
0x01, 0x01, 0x02, 0x01, 0x01, 0x03, 0x01, 0x07, 0x03, 0x01, 0x07, 0x06, 0x01, 0x07, 0x07, 0x02, 0x01, 0x01,
0x02, 0x05, 0x05, 0x03, 0x01, 0x01, 0x03, 0x01, 0x05, 0x03, 0x05, 0x04, 0x03, 0x05, 0x05, 0x03, 0x05, 0x07,
0x03, 0x07, 0x01, 0x04, 0x05, 0x03, 0x05, 0x01, 0x03, 0x05, 0x03, 0x06, 0x05, 0x03, 0x07, 0x05, 0x05, 0x02,
0x05, 0x05, 0x03, 0x06, 0x03, 0x05, 0x06, 0x07, 0x01, 0x07, 0x03, 0x05, 0x07, 0x05, 0x03, 0x07, 0x07, 0x01
}; //needed as hex

static uint8_t y8 = 13;
static uint8_t cur_shift = 0, request_count = 0;

uint8_t rnd()
{
//shift the shifting numbers
request_count++;
if(request_count == 255)
{
request_count = 0;
cur_shift++;
if (cur_shift == 24)
{
cur_shift = 0;
}
}
//shift something around
y8 ^= (y8 << pgm_read_byte(&(shift[cur_shift * 3])));
y8 ^= (y8 >> pgm_read_byte(&(shift[cur_shift * 3 + 1])));
return y8 ^= pgm_read_byte(&(shift[cur_shift * 3 + 2]));
}


Reference to the Numbers for the shift