c

What are the valid characters for macro names?

Are C-style macro names subject to the same naming rules as identifiers? After a compiler upgrade, it is now emitting this warning for a legacy application: warning #3649-D: white space is required between the macro name "CHAR_" and its replacement text #define CHAR_& 38 This line of code is defining an ASCII value c...

Program for documenting a C struct?

If you have a binary file format (or packet format) which is described as a C structure, are there any programs which will parse the structure and turn it into neat documentation on your protocol? The struct would of course contain arrays, other structures, etc., as necessary to describe the format. The documentation would probably nee...

When and why will an OS initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete?

I know that the OS will sometimes initialise memory with certain patterns such as 0xCD and 0xDD. What I want to know is when and why this happens. When Is this specific to the compiler used? Do malloc/new and free/delete work in the same way with regard to this? Is it platform specific? Will it occur on other operating systems, suc...

C/C++ comparison syntax

Hi all, I noticed for a while now the following syntax in some of our code: if( NULL == var){ //... } or if( 0 == var){ //... } and similar things. Can someone please explain why did the person who wrote this choose this notation? (instead of the common var == 0 way) Is it a matter of style, or does it somehow affect perfor...

Proper Memory allocation

I have the following construction: typedef struct bucket { char *key; ENTRY *data; struct bucket *next; } bucket; typedef struct { size_t size; bucket **table; } hash_table; But I have no idea how to allocate memory for that. I tried: hash_table* ht = malloc(sizeof(hash_table)*101); in order to create a hashta...

Create modified HFONT from HFONT

I using the Win32 API and C/C++. I have a HFONT and want to use it to create a new HFONT. The new font should use the exact same font metrics except that it should be bold. Something like: HFONT CreateBoldFont(HFONT hFont) { LOGFONT lf; GetLogicalFont(hFont, &lf); lf.lfWeight = FW_BOLD; return CreateFontIndirect(&lf); } ...

Serialize Data Structures in C

I'd like a C library that can serialize my data structures to disk, and then load them again later. It should accept arbitrarily nested structures, possibly with circular references. I presume that this tool would need a configuration file describing my data structures. The library is allowed to use code generation, although I'm fairly ...

C typedef: parameter has incomplete type

GCC 3.4.5 (MinGW version) produces a warning: parameter has incomplete type for line 2 of the following C code: struct s; typedef void (* func_t)(struct s _this); struct s { func_t method; int dummy_member; }; Is there a way to fix this (or at least hide the warning) without changing the method argument's signature to (struct s *)? N...

Why is "array" a reserved word in C/C++?

Visual Studio syntax highlighting colors this word blue as if it were a keyword or reserved word. I tried searching online for it but the word "array" throws the search off, I get mostly pages explaining what an array is. What is it used for? ...

Do you use the TR 24731 'safe' functions in your C code?

The ISO C committee (ISO/IEC JTC1/SC21/WG14) has published TR 24731-1 and is working on TR 24731-2 (the second part is still under development). From the web site: TR 24731-1: Extensions to the C Library Part I: Bounds-checking interfaces WG14 is working on a TR on safer C library functions. This TR is oriented towards modifying exi...

"C" sizeof with a type or variable

Recently saw someone commending another user on their use of sizeof var instead of sizeof(type). I always thought that was just a style choice. Is there any significant difference? As an example, the lines with f and ff were considered better than the lines with g and gg: typedef struct _foo {} foo; foo *f = malloc(count * sizeof f);...

What is the origin of the term "baller" which means "pointer"?

I've seen the term "baller" used in a couple of C++ interview tests. It means "pointer" as best as I can tell from questions like "Describe the difference between a baller and a reference." I thought that perhaps it was an Indian term due to some Google deduction, but a couple of Indian co-workers who went to school in India said they've...

Find PID of a Process by Name without Using popen() or system()

I've a process name and I've to send a kill() signal to that process but I need its PID to call kill(). I would only like to use: popen("pidof process_name"); as the last thing. Is there any other way to find out the process' PID? One way I could think of is to send a socket request to that process and ask to its PID. The other way i...

Changing the value of stdout in a C++ program

I have a Windows C++ program that is doing something like: FILE* pf = ...; *stdout = *pf; // stdout is defined in stdio.h I'm looking for an explanation about what happens when you change the value of the stdout file handle. Is this just a way of redirecting stdout? -cr ...

Parameter evaluation order before a function calling in C

Hi all, Can it be assumed a evaluation order of the function parameters when calling it in C ? According to the following program, it seems that there is not a particular order when I executed it. #include <stdio.h> int main() { int a[] = {1, 2, 3}; int * pa; pa = &a[0]; printf("a[0] = %d\ta[1] = %d\ta[2] = %d\n",*(pa),...

Enforce strong type checking in C (type strictness for typedefs)

Is there a way to enforce explicit cast for typedefs of the same type? I've to deal with utf8 and sometimes I get confused with the indices for the character count and the byte count. So it be nice to have some typedefs: typedef unsigned int char_idx_t; typedef unsigned int byte_idx_t; With the addition that you need an explicit cast...

Using C/C++ static libraries from iPhone ObjectiveC Apps

Is it possible to have a C static library API, which uses C++ internally and hide this from users of the library? I have writen a portable C++ library I wish to statically link to an iPhone application. I have created an Xcode project using the Max OS X 'static library' template, and copied the source across, as well as writing a C wap...

What's your preferred pointer declaration style, and why?

I know this is about as bad as it gets for "religious" issues, as Jeff calls them. But I want to know why the people who disagree with me on this do so, and hear their justification for their horrific style. I googled for a while and couldn't find a style guide talking about this. So here's how I feel pointers (and references) should be...

How to debug the entry-point of fork-exec process?

I have a C linux application (A) that spawns another process (P) when it is started. When I want to debug P I start A as usual and I connect with ddd/gdb to P. Problems appear when I want to debug the entry-point (start of main) of P. If I follow the usual approach when I connect the debugger to P is already to late. The solution I've f...

Macro which prints an expression and evaluates it (with __STRING)

For learning and demonstrating, I need a macro which prints its parameter and evaluates it. I suspect it is a very common case, may be even a FAQ but I cannot find actual references. My current code is: #define PRINT(expr) (fprintf(stdout, "%s -> %d\n", __STRING(expr), (expr))) and then: PRINT(x & 0x01); It works fine but I am not...