c

why optimization does not happen?

hi. I have C/C++ code, that looks like this: static int function(double *I) { int n = 0; // more instructions, loops, for (int i; ...; ++i) n += fabs(I[i] > tolerance); return n; } function(I); // return value is not used. compiler inlines function, however it does not optimize out n manipulations. I would ex...

# pragma directive and its uses in c

Hello , can anyone tell my what does the #pragma can do in the c language . what are its uses and why the above program is not giving the output 'inside v1'& 'inside v2' in the following program ... # include<stdio.h> void v1(); void v2(); # pragma startup v1 # pragma exit v2 int main() { printf("inside main\n"); return 0; } void v1(...

Recording/Reading C doubles in the IEEE 754 interchange format

So I'm serializing a C data structure for cross-platform use, and I want to make sure I'm recording my floating point numbers in a cross-platform manner. I had been planning on just doing char * pos; /*...*/ *((double*) pos) = dataStructureInstance->fieldWithOfTypeDouble; pos += sizeof(double); But I wasn't sure that the bytes wo...

String Formatting Tricks/Docs

Was reading the response by Shaggy Frog to this post and was intrigued by the following line of code: NSLog(@"%@", [NSString stringWithFormat:@"%@:%*s%5.2f", key, padding, " ", [object floatValue]]); I know string formatting is an age old art but I'm kinda doing the end around into Cocoa/Obj-C programming and skipped a few grades alo...

Crash the program with cmd line args

Lets us consider the following program : #include <stdlib.h> int main(int argc, char **argv){ int a,b; if (argc != 3) return -1; a = atoi(argv[1]); b = atoi(argv[2]); a = b ? a/b : 0; return a; } The task is to crash the program by providing arguments in command-line. ...

Quick strlen question

Hi again. I've come to bother you all with another probably really simple C question. Using the following code: int get_len(char *string){ printf("len: %lu\n", strlen(string)); return 0; } int main(){ char *x = "test"; char y[4] = {'t','e','s','t'}; get_len(x); // len: 4 get_len(y); // len: 6 return 0;...

Foraward SNMP requests from Agentx Master to Agentx Subagent

I am running an agentx master and an agentx subagent on linux. When I run snmpget on a default MIB i.e. sysdescr.0 it returns fine, but when I request for a MIB that was registered through the agentx subagent it timesout. It appears that the master receives the GET request but does not forward on to the agentx subagent. The MIB is regis...

Integrating C++ libraries in C for ARToolKit - ARToolkit+

I am working on a Augmented reality Project where I plan to use ARToolkit for Image Capturing and rendering where as ARToolKitPlus for marker detection. The problem is ARToolKit is a C library where as ARToolKit+ is written in C++. Can some one please sugegst me a way to integrate them both. ARToolKit Library ARToolkitPlus ...

What is the difference between the * and the & operators in c programming?

I am just making sure I understand this concept correctly. With the * operator, I make a new variable, which is allocated a place in memory. So as to not unnecessarily duplicate variables and their values, the & operator is used in passing values to methods and such and it actually points to the original instance of the variable, as oppo...

C programming - Print the 3rd line of a txt

Hi...Imagine that i have on a txt this: Hello SLB 3 1324 how can i get the 3rd line? fgets or fscanf? and imagine on a txt this: 8;9;10;12 how can i print the numbers separeted? ...

Function equivalent to sprintf() with maximum number of characters to be copied?

function(char* name) { char sql[50]; sprintf(sql, "select %s;", name); } What's the best way to make sure only 50 chars of name are copied to sql in the case name is larger than what sql can hold? (sprintf with a N parameter?) Thank You. ...

Direct access to harddrive?

I was wondering how hard disk access works. Ex, how could I view/modify sectors? Im targeting Windows if that helps. Thanks ...

Is a signal sent with kill to a parent thread guaranteed to be processed before the next statement?

Okay, so if I'm running in a child thread on linux (using pthreads if that matters), and I run the following command kill(getpid(), someSignal); it will send the given signal to the parent of the current thread. My question: Is it guaranteed that the parent will then immediately get the CPU and process the signal (killing the app if ...

Returning a struct pointer

Suppose I have the following struct and function returning a pointer: typedef struct { int num; void *nums; int size; } Mystruct; Mystruct *mystruct(int num, int size) { //Is the following correct? Is there a more efficient way? Mystruct mystruct; mystruct.num = num; mystruct.size = size; mystruct.nums = malloc(nu...

How to hijack all local http request and extract the url using c?

What direction should I go in(libraries, documents)? UPDATE Can someone illustrate how to use winpcap to do the job? UPDATE 2 How do I verify whether a packet is an HTTP one? ...

What's the point of LEA EAX, [EAX]?

LEA EAX, [EAX] I encountered this instruction in a binary compiled with the Microsoft C compiler. It clearly can't change the value of EAX. Then why is it there at all? ...

Access block level storage via kernel

How to access block level storage via the kernel (w/o using scsi libraries)? My intent is to implement a block level storage protocol over network for learning purpose, almost the same way SCSI works. Requests will be generated by initiator and sent to target (both userspace program) which makes call to kernel module and returns the ...

How do you perform macro expansion within #ifdef?

Hi all, I have some fairly generic code which uses preprocessor macros to add a certain prefix onto other macros. This is a much simplified example of what happens: #define MY_VAR(x) prefix_##x "prefix_" is actually defined elsewhere, so it will be different each time the file is included. It works well, but now I have some code I ...

Is there any memory leak in the normal routine of sqlite3_*()?

A normal routine of sqlite3_prepare_v2() + sqlite3_step() + sqlite3_finalize() could contain leak. It sound ridiculous. But the test code seems to say it. Or I used the sqlite3_*() wrongly. Appreciate for any reply. #include <stdio.h> #include <unistd.h> // for usleep() #include <sqlite3.h> int multi_write (int j); sqlite...

why do i have to open a file in binary mode to write in it in c?

fprintf(file, "%d %d %d", array[0], array[1], array[2]); for this statement to work i have to open the file in 'wb' mode rather than 'w' mode . How does a binary mode make the syntax work? ...