|
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
|
|
|
|
|
|
I will check it.
Thank you
|
|
|
|
|
|
Thank you so much!
I will check it
|
|
|
|
|
You can use a product such as Code Jock which is a layer above MFC or .NET
- Michael Haephrati מיכאל האפרתי
|
|
|
|
|
Is there a way to tell the C++ runtime which heap to use to allocate objects with "new"? Can it be changed on the fly, or must the program be restarted?
I'd like to create custom heaps to hold objects, and then destroy the heaps when no longer needed, then create new heaps to take their places.
I have some legacy classes that leak memory, and I'm hoping to be able to mitigate the leaks by creating the objects in separate heaps, and then destroying the heaps after X many hours of run time.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
|
The "canonical" way to do this is with class-specific new / delete:
class Foo
{
public:
void* operator new(size_t blockSize)
{
}
void operator delete(void* p)
{
}
};
As long as you use the same heap for both allocation and deletion, things should work fine. As Richard MacCutchan says, a quick-and-dirty way to do this in Windows is by using the Windows heap APIs.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
Thank you Daniel. Does this solution mean that I must call the constructor myself?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
No. When constructing an object using new (e.g. Foo* p = new Foo()), the compiler chooses the new operator to be called as follows:
- A class-specific operator new() (if present)
- The global operator new
It is the same for the delete operator. You may also replace the global new and delete operators with your own code:
void* operator new(size_t blockSize)
{
}
void operator delete(void* p)
{
}
Note that operators new and delete deal only with memory allocation/release. The compiler still calls the class' constructor to turn the raw memory into a class instance, and the destructor in order to turn a class instance back into raw memory.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|