c

Unit tests for interrupt-heavy code

I am writing C code for an AVR chip. The code is heavy on interrupt serivce routines which wait on serial ports, ADCs and timers. The ISRs write to buffers which the main loop examines when it gets to them. I design the buffers so that ISRs can update them while the main loop is reading them. I want to verify that this works. I have uni...

fgets from stdin problems [C]

I'm writing a program that works with files. I need to be able to input data as structures, and eventually read it out. The problem i have at the moment is with this code: typedef struct { char* name; ..... }employeeRecord; employeeRecord record; char name[50]; if(choice == 1) { /*Name*/ printf("\nEnter the...

Trying to compile with cl.exe

Sorry if this is a simple question, I don't do much programming in Windows. I have Visual Studio installed but when I try to execute "cl" from the command line it tells me it's not recognized as a command! How do I get cl? ...

Trouble with dangling pointers and character arrays in C

main(){ char *cmd1[20] = {NULL}; int x = parse_command(cmd1); printf("%s\ ",cmd1[0]); } parse_command(char *inTempString){ char tempString[256]; (call to function that assigns a string to tempString) cmd1[0] = tempString; } There is a problem when printing out the cmd1[0] within main. I am pretty sure that it is a da...

How to pass an array of struct using pointer in c/c++ ?

Hi, in C code I'm stuck to pass an array of struct to a function, here's the code that resembles my problem: typedef struct { int x; int y; char *str1; char *str2; }Struct1; void processFromStruct1(Struct1 *content[]); int main() { Struct1 mydata[]= { {1,1,"black","cat"}, {4,5,"red","bird"}, {6,7,"brown...

How is the lagged fibonacci generator random?

I dont get. If it has a fixed length, choosing the lags and the mod over and over again will give the same number, no? ...

Why malloc always return NULL

Hi, guys, My dev environment is VS2008, DX9, Windows XP. I try to add protection handling to the out of memory case. When malloc return NULL, I would page some resource to disk, and release the resources in the memory. But sometimes, malloc always return NULL, even if I release most of resources and process memory usage and VM size is o...

How to get a file's size which is greater than 4 Gb?

Hi! I made a simple function, which get's a file's size. int file_size( char * filename ) { int size; struct stat st; stat( filename, &st ); size = st.st_size; return size; }//file_size It is working fine, but if i have a file which size is bigger than 4Gb than i get a negativ number back, and of course th...

Refactoring an id type

Hi all, My application uses an int as id to identify objects in both in-memory and persistent registry. Functions in the application receive the id as argument, retrieve the object from the registry and do their job, e.g. void print( int id, int bar, double zip, int boo ) { Object *obj = get_obj_by_id( id ); obj->print( bar, z...

what is the initial value of a pointer in c file

In following code, is it possible cause some memory leaks? reference-ril.c static void requestRadioPower(void *data, size_t datalen, RIL_Token t) { .... ATResponse *p_response = NULL; .... err = at_send_command(cmd, &p_response); // it's a memory leakage or not ? .... at_response_free(p_response); .... } ...

Pointer to [-1]th index of array.

How does a pointer points to [-1]th index of the array produce legal output everytime. What is actually happening in the pointer assignment? #include<stdio.h> int main() { int realarray[10]; int *array = &realarray[-1]; printf("%p\n", (void *)array); return 0; } Code output: manav@workstation:~/knr$ g...

Implementing a parallel algorithm to compute pi

I would like to implement a parallel version of the code below using threads in OpenMP,is there any better way to do this? /* Program to compute Pi using Monte Carlo methods */ #include <stdlib.h> #include <stdio.h> #include <math.h> #include <string.h> #include <time.h> #define SEED 35791246 int main(int argc, char* argv) { int ni...

Confused about recv()

Pardon if this question has been answered but I couldn't find it. I'm kinda confused about recv() and recvfrom(). Once the server binds the address (or accepts connection for TCP), recv() is called. Does recv() constantly check for messages that has been sent or does it wait until a message is received? If it does wait, how long is the ...

How can I send e-mail in C?

I'm just wondering how can I send email using C? I Googled it a little bit, but could not find anything proper. ...

Gtk Draw Bitmap

I want to draw an image on a window using Cairo. How can I load a bmp or png from disk and create a brush from it? The code below shows where drawing should be made. The expose signal is attached to the window. gboolean OnExpose(GtkWidget *widget, GdkEventExpose *event, gpointer data) { cairo_t *cr; cr = gdk_cairo_create(wi...

Pushing a 1-D array onto a 2-D array in C

I am working on a queue data structure. The structure is: struct queue { char array[MAX_LENGTH][8]; int back; }; It is designed to store a list of MAX_LENGTH strings that are 7 chars long. I wish to push a 1D array of 8 chars (well, 7 chars and \0, just like the array in the struct). I have this push code: void push (struct queue ...

fnmatch for windows in C?

Is there a version of fnmatch for Windows? I'm trying to have the same functionality (basically using *'s from the command line for the filename) in C without having to call FindFirst() etc. Code is appreciated. EDIT: I need to accept wild cards from the command line for filenames, for example *.txt and be able to open each .txt file....

Receiving response(s) from N number of clients in reply to a broadcast request over UDP

Hi All, I am implementing a kind of IP finder for a particular type of network multimedia device. I want to find out all the alive devices of that type in the LAN, with their IP address and other details. The device has its own way of device discovery. It works as follows: A client sends a broadcast request over the LAN via UDP. Th...

String arrays in C

I have an array of strings which when I iterate through and print its elements gives me unexpected results. char currencies[][3] = {"EUR", "GBP", "USD", "JPY", "CNY"}; void show_currencies() { int i; for(i=0; i<5; i++) { printf("%s - ", currencies[i]); } } when I call show_currencies() I get this on output. E...

Socket connect() always succeeds (TCP over ActiveSync)

I'm using TCP/IP over ActiveSync to connect from Windows CE device to Windows XP desktop. The WinSock connect() function always succeeds, no matter whether desktop server application is actually running. The following simplified code demonstrates this issue: #include "stdafx.h" #include <Winsock2.h> int _tmain(int argc, _TCHAR* argv[]...