|
Something is definitely wrong here. This is the most simplest, succinct explanation that could be offered, so why in the crap is it not being taught/understood in the school system? Is it the students or the instructors that are to blame?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Well said.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
I suspect it's a mixture. Some teachers are not so good, some students lack the ability, some are lazy. But I suspect we never hear from the ones who do have good teachers, and ability, and are prepared to try things for themselves.
|
|
|
|
|
I created a socket_raw, the custom protocol type is 0x2328, and I send 0x2328 type data on another machine. The capture packet can be captured, but recvfrom cannot receive it. What should I do? Here is the code received.
struct sockaddr_ll sll;
struct ifreq ifr;
if ((sd = socket (PF_PACKET, SOCK_RAW, htons (0x2328))) < 0) {
perror ("socket() failed to get socket descriptor for using ioctl() ");
exit (EXIT_FAILURE);
}
memset (&ifr, 0, sizeof (ifr));
sprintf (ifr.ifr_name, "eth1");
if (ioctl (sd, SIOCGIFINDEX, &ifr) < 0) {
perror ("ioctl() failed");
return (EXIT_FAILURE);
}
memset (&sll, 0, sizeof (sll));
int sll_len;
sll.sll_family = AF_PACKET;
sll.sll_ifindex = ifr.ifr_ifindex;
sll_len = sizeof(sll);
char buffer[1024];
recvfrom(sd, buffer, sizeof(buffer), 0, (struct sockaddr *)&sll, &sll_len);
|
|
|
|
|
|
recvfrom has no return value, because there is no data, so it is blocking. And if I change 0x2328 to 0x0003, I can receive it.
|
|
|
|
|
Try making it a non-blocking socket. You should then get a status return.
|
|
|
|
|
After I set it to non-blocking, I always returned error code 11, but I didn't find the error message corresponding to 11.
|
|
|
|
|
Could you show the actual code used to obtain the error?
|
|
|
|
|
|
It won't help you with your problem but error code 11 is EGAIN defined in errno.h. It indicates that you should execute the function again until you get data or an error, or give up (time out). It is returned by non-blocking functions when the corresponding blocking function would block.
|
|
|
|
|
int main(int argv, char *argc[])
{
struct ifreq ifr;
struct sockaddr_ll sll;
int sd, sll_len;
sll_len = sizeof(sll);
if ((sd = socket(PF_PACKET, SOCK_RAW, htons(0x2328))) < 0)
printf("create socket failed!\n");
sll.sll_halen = ETH_ALEN;
strcpy(ifr.ifr_name, "ens33");
ioctl(sd, SIOCGIFFLAGS, &ifr);
ifr.ifr_flags |= IFF_PROMISC;
if(fcntl(sd, F_SETFL, O_NONBLOCK) == -1) {
perror("fcntl");
exit(errno);
}
char recvbuf[2048];
sleep(5);
int n_read = recvfrom(sd, recvbuf, 2048, 0, (struct sockaddr *)&sll, &sll_len);
if (n_read <= 0)
{
printf("%d\n", errno);
}
}
I started to cycle data before I hibernate, but I still can't receive it
|
|
|
|
|
I found the reason. Previously, because send and recv were sent and received on the same network card, it could not be received.
|
|
|
|
|
Fine to hear that.
I do know that it does not work on the same interface. But you wrote in your initial post that you are sending from another machine so that I thought it must be something else.
|
|
|
|
|
I am trying to calculate sum of fibonacci series.
Struct arg is defined in header file as follows
struct args {
int number;
int result;
};
args user_arguments = { 10, 0 };
void fibonacci (void* arguments)
{
args* local_args = (args*) arguments;
printf("local_args->number = %d\n", local_args->number);
if (local_args->number <= 1) {
local_args->result= 1;
return;
}
int a = (local_args->number)-1;
int b = (local_args->number)-2;
local_args->result =fibonacci(&a) + fibonacci(&b)
return local_args->result;
Above function is called from the function below. createPackage is a function provided by another library which gonna execute fabonacci function.
void WorkPackage(void*) {
createPackage(fibonacci, &user_arguments);
printf("Sum of fabonacci sequence is %d ", args->result) }
While compiling, It gives the following error
invalid operands of types ‘void’ and ‘void’ to binary ‘operator+’
Please help in pointing what am i doing wrong here??
|
|
|
|
|
You are trying to add two operands of the type void (fibonacci(&a)and fibonacci(&b)) :
meerokh wrote: local_args->result =fibonacci(&a) + fibonacci(&b)
and the compiler doesn't know how to do it!
|
|
|
|
|
How can i calculate the sum and add it back to struct variable??
|
|
|
|
|
Have you tried having the fibonacci() function return a value, or if the function's signature is unchangeable, removing the return statement?
You might also consider terminating the local_args->result = fibonacci(&a) + fibonacci(&b) statement with a semicolon.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Your definition of the fibonacci function is incorrect. You cannot return a value from a function defined as void , i.e. not returning anything. You also declare the arguments parameter as a void* but pass the address of an int variable to it. You then cast that to the address of an args structure, which will likely cause a segmentation fault. You need to rethink this code completely.
|
|
|
|
|
You have to comply with the assigned interface, but such interface would probably make your Fibonacci series computation rather clumsy. I suggest you to separate the tasks: write a fibonacci function complying with the required interface wich, in turn, calls a trivial recursive implementation (say myfib ) of the series computation:
#include <stdio.h>
struct args
{
int number;
int result;
};
static int myfib( int n )
{
if ( n < 3 )
return 1;
else
return myfib(n-1) + myfib(n-2);
}
void fibonacci( void * arguments )
{
struct args * pargs = (struct args *) arguments;
pargs->result = myfib( pargs->number); }
int main()
{
struct args a = { 10, 0 };
fibonacci( &a );
printf("fibonacci(%d)=%d\n", a.number, a.result);
return 0;
}
|
|
|
|
|
The idea is to process array from left to right. While processing, find the first out of place element in the remaining unprocessed array. An element is out of place if it is negative and at odd index, or it is positive and at even index. Once we find an out of place element, we find the first element after it with opposite sign. We right rotate the sub-array between these two elements (including these two).
#include <iostream>
#include <assert.h>
using namespace std;
void printArray(int arr[], int n);
void rightrotate(int arr[], int n, int outofplace, int cur)
{
char tmp = arr[cur];
for (int i = cur; i > outofplace; i--)
arr[i] = arr[i-1];
arr[outofplace] = tmp;
}
void rearrange(int arr[], int n)
{
int outofplace = -1;
for (int index = 0; index < n; index ++)
{
if (outofplace >= 0)
{
if (((arr[index] >= 0) && (arr[outofplace] < 0))
|| ((arr[index] < 0) && (arr[outofplace] >= 0)))
{
rightrotate(arr, n, outofplace, index);
printArray(arr, n);
if (index - outofplace > 2)
outofplace = outofplace + 2;
else
outofplace = -1;
}
}
if (outofplace == -1)
{
if (((arr[index] >= 0) && (!(index & 0x01)))
|| ((arr[index] < 0) && (index & 0x01)))
{
outofplace = index;
}
}
}
}
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
int main()
{
int arr[] = {-5, -2, 5, 2, 4, 7, 1, 8, 0, -8};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Given array is \n";
printArray(arr, n);
cout << "\n\n";
rearrange(arr, n);
cout << "\n\nRearranged array is \n";
printArray(arr, n);
return 0;
}
Quote: // check if current entry is out-of-place
if (((arr[index] >= 0) && (!(index & 0x01))) // what does (index & 0x01) means ??
|| ((arr[index] < 0) && (index & 0x01)))
{
outofplace = index;
}
what does the above statement do ?
|
|
|
|
|
index & 0x01
It is a test on last bit of the index variable: it is 1 when index is odd (0 when index is even).
|
|
|
|
|
Hello, everybody!
I wanna to make my application with dark theme GUI like as Photoshop.
If there is any good library, can you please suggest me?
Thank you!
Best Regards.
|
|
|
|
|
|
Hello, dear!
Thanks for your reply
I just need good library for theme.
I wanna to make my own video player with dark theme...
I will check it more 
|
|
|
|
|