c

UNICODE_STRING to Null terminated

I need to convert a UNICODE_STRING structure to a simple NULL TERMINATED STRING. typedef struct _UNICODE_STRING { USHORT Length; USHORT MaximumLength; PWSTR Buffer; } UNICODE_STRING, *PUNICODE_STRING; I can't find a clean sollution on MSDN about it. Anyone been there? I am not using .net so I need a native API s...

Function pointers in C - address operator "unnecessary"

Hello, Using qsort in C we pass in a comparison function e.g. int cmp(const void*, const void*); the protoype of qsort expects a int (* )(const void* , const void*) so we call: qsort(..., cmp); but it is equally valid to call: qsort(..., &cmp); and this is what we would have to do if we passed in a static member-function in C++...

How do I test the current version of GCC ?

I would like to include a different file depending on the version of GCC. More precisely I want to write: #if GCC_VERSION >= 4.2 # include <unordered_map> # define EXT std #elif GCC_VERSION >= 4 # include <tr1/unordered_map> # define EXT std #else # include <ext/hash_map> # define unordered_map __gnu_cxx::hash_map # define EXT __...

How can you flush a write using a file descriptor?

It turns out this whole misunderstanding of the open() versus fopen() stems from a buggy I2C driver in the Linux 2.6.14 kernel on an ARM. Backporting a working bit bashed driver solved the root cause of the problem I was trying to address here. I'm trying to figure out an issue with a serial device driver in Linux (I2C). It appear...

mtrace for a fortran program

I'm trying to use mtrace to detect memory leaks in a fortran program. I'm using the gfortran compiler. See the wikipedia entry for a (working) C example of mtrace: http://en.wikipedia.org/wiki/Mtrace I tried both ways, i.e. wrapping the mtrace() and muntrace() and call them from the fortran program, as well as create a C program which ...

Why is my simple C program displaying garbage to stdout?

Consider the following simple C program that read a file into a buffer and displays that buffer to the console: #include<stdio.h> main() { FILE *file; char *buffer; unsigned long fileLen; //Open file file = fopen("HelloWorld.txt", "rb"); if (!file) { fprintf(stderr, "Unable to open file %s", "HelloWorl...

How do I compile Perl code within a C program?

I have a C program with an embedded Perl interpreter. I want to be able to precompile some Perl code from within the program. How do I do that? Rationale (if anyone is interested) is to be able to compile it once, store the parse tree, and execute many times (as long as the compiled code does not change). Thanks! Madhu PS: I am using ...

Russian Peasant Multiplication

Here is my short implementation of Russian Peasant Multiplication. How can it be improved? Restrictions : only works when a>0,b>0 for(p=0;p+=(a&1)*b,a!=1;a>>=1,b<<=1); ...

Dynamic array of structs in C

I know how to create an array of structs but with a predefined size. However is there a way to create a dynamic array of structs such that the array could get bigger? For example: typedef struct { char *str; } words; main() { words x[100]; // I do not want to use this, I want to dynamic increase the...

What is the advantage of strlmove vs strmove in C?

Hi, To trim the leading spaces we are using strmove. But we were advised to use strlmove instead of strmove. I have read and used strlcpy and strlcat. Whether strlmove does the similar functionality and what all are its advantages? Edit 1: Thank you Mike B and Chris Young. This is how we use strlcpy. size_t strlcpy(char *dst, const ch...

what is the difference between re-entrant function and recursive function in C?

In C I know about the recursive function but I heard about the re-entrant function.What is that? And whats the difference between them? ...

ARM to C calling convention, registers to save

It's been a while since I last coded arm assembler and I'm a little rusty on the details. If I call a C function from arm, I only have to worry about saving r0-r3 and lr, right? If the C function uses any other registers, is it responsible for saving those on the stack and restoring them? In other words, the compiler would generate code...

How to find the length of unsigned char* in C

I have a variable unsigned char* data = MyFunction(); how to find the length of data? ...

How to build Python C extension modules with autotools

Most of the documentation available for building Python extension modules uses distutils, but I would like to achieve this by using the appropriate python autoconf & automake macros instead. I'd like to know if there is an open source project out there that does exactly this. Most of the ones I've found end up relying on a setup.py file...

IPC with Message Passing

Hi, I'm looking for suggestions on possible IPC mechanisms that I can implement in my self-made OS for an AVR32 board. My current choice is implementing the massage passing mechanism described in the books written by Tanenbaum. Is this a good choice? Are there easier way to implement IPC? Thanks ...

What's the best way to build variants of the same C/C++ application.

I have three closely related applications that are build from the same source code - let's say APP_A, APP_B, and APP_C. APP_C is a superset of APP_B which in turn is a superset of APP_A. So far I've been using a preprocessor define to specify the application being built, which has worked like this. // File: app_defines.h #define APP_A ...

When is char* safe for strict pointer aliasing?

I've been trying to understand the strict aliasing rules as they apply to the char pointer. Here this is stated: It is always presumed that a char* may refer to an alias of any object. Ok so in the context of socket code, I can do this: struct SocketMsg { int a; int b; }; int main() { ... SocketMsg msgToSend; msgT...

Create a wrapper function for malloc and free in C

Hey, I am trying to create wrapper functions for free and malloc in C to help notify me of memory leaks. Does anyone know how to declare these functions so when I call malloc() and free() it will call my custom functions and not the standards lib functions? ...

iostream linker error

I have some old C code that I would like to combine with some C++ code. The C code used to have has the following includes: #include <windows.h> #include <stdio.h> #include <string.h> #include "mysql.h" Now I'm trying to make it use C++ with iostream like this: #include <windows.h> #include <stdio.h> #include <string> #include <iost...

What is a good reference documenting patterns of use of X-Macros in C (or possibly C++)?

A basic definition and example and a few references for "X-Macros" is given in this wikipedia entry on the C pre-processor: An X-Macro is a header file (commonly using a ".def" extension instead of the traditional ".h") that contains a list of similar macro calls (which can be referred to as "component macros"). What are s...