<< < 1 2 3 4 5 6 7 8 9 > >>   Sort: Date

How can I shut off the warning: possible pointer alignment problem ...
How can I shut off the ``warning: possible pointer alignment problem'' message which lint gives me for each call to malloc? A modern lint shouldn't be complaining about this. Once upon a time, lint did not and could not know that malloc ``returns a pointer to space suitably aligned for storage of an...
2016-06-03, 1758👍, 0💬

Return a null pointer or a pointer to 0 bytes? .....
What should malloc(0) do? Return a null pointer or a pointer to 0 bytes? The ANSI/ISO Standard says that it may do either; the behavior is implementation-defined ortable code must either take care not to call malloc(0), or be prepared for the possibility of a null return.
2016-04-21, 1739👍, 0💬

How can I print numbers with commas separating the thousands? What about currency formatted numbers?
How can I print numbers with commas separating the thousands? What about currency formatted numbers? The functions in &lt;locale.h> begin to provide some support for these operations, but there is no standard C function for doing either task. (In Standard C, the only thing printf does in respons...
2015-11-02, 1736👍, 0💬

What's the best way to write a multi-statement macro?
What's the best way to write a multi-statement macro? The usual goal is to be able to invoke the macro as if it were an expression statement consisting of a function call: MACRO(arg1, arg2); This means that the ``caller'' will be supplying the final semicolon, so the macro body should not. The macro...
2016-02-22, 1652👍, 0💬

How can I read a single character from the keyboard without waiting for the RETURN key?
How can I read a single character from the keyboard without waiting for the RETURN key? How can I stop characters from being echoed on the screen as they're typed? Alas, there is no standard or portable way to do these things in C. Concepts such as screens and keyboards are not even mentioned in the...
2015-04-29, 1636👍, 0💬

I need a random number generator.
I need a random number generator. The Standard C library has one: rand. The implementation on your system may not be perfect, but writing a better one isn't necessarily easy, either. Here is a portable C implementation of the ``minimal standard'' generator : #define a 16807 #define m 2147483647 #def...
2015-08-03, 1574👍, 0💬

My application depends heavily on dynamic allocation of nodes for data structures ......
My application depends heavily on dynamic allocation of nodes for data structures, and malloc/free overhead is becoming a bottleneck. What can I do ? One improvement, which is particularly attractive if all nodes are the same size, is to place unused nodes on your own free list, rather than actually...
2016-04-12, 1539👍, 0💬

What is the difference between ......
Q: What's the difference between const MAXSIZE = 100; and #define MAXSIZE 100 A preprocessor #define gives you a true compile-time constant. In C, const gives you a run-time object which you're not supposed to try to modify; ``const'' really means ``readonly''.
2015-12-22, 1535👍, 0💬

What is the correct declaration of main?
What is the correct declaration of main? There are two valid declarations: int main(void) int main(int argc, char **argv) although they can be written in a variety of ways. The second parameter may be declared char *argv[] , you can use any names for the two parameters, and you can use old-style syn...
2015-12-22, 1503👍, 0💬

Does anyone have a tool for converting old-style C programs to ANSI C?
Does anyone have a tool for converting old-style C programs to ANSI C, or vice versa, or for automatically generating prototypes? Two programs, protoize and unprotoize, convert back and forth between prototyped and ``old style'' function definitions and declarations. (These programs do not handle fu...
2015-11-25, 1467👍, 0💬

Why can I only seem to malloc 640K or so?. .....
I've got 8 meg of memory in my PC. Why can I only seem to malloc 640K or so?. Under the segmented architecture of PC compatibles, it can be difficult to use more than 640K with any degree of transparency, especially under MS-DOS.
2016-04-12, 1410👍, 0💬

What is Duff's Device?
What is Duff's Device? It's a devastatingly devious way of unrolling a loop, devised by Tom Duff while he was at Lucasfilm. In its ``classic'' form, it was used to copy bytes, and looked like this: register n = (count + 7) / 8; /* count > 0 assumed */ switch (count % 8) { case 0: do { *to = *from++;...
2015-01-02, 1409👍, 0💬

How do I swap bytes?
How do I swap bytes? V7 Unix had a swab function, but it seems to have been forgotten. A problem with explicit byte-swapping code is that you have to decide whether to call it or not, based on the byte order of the data and the byte order of the machine in use. A better solution is to define functio...
2015-02-16, 1400👍, 0💬

I im dynamically allocating an array, like this ...
I'm dynamically allocating an array, like this: int *iarray = (int *)malloc(nints); malloc isn't returning NULL, but the code isn't working. A:malloc is a low-level, typeless allocator. It doesn't know how you're going to use the memory; all it does is to allocate as many bytes of memory as you ask ...
2016-04-06, 1395👍, 0💬

I am allocating a large array for some numeric work ...
I'm allocating a large array for some numeric work, using the line double *array = malloc(300 * 300 * sizeof(double)); malloc isn't returning null, but the program is acting strangely, as if it's overwriting memory, or malloc isn't allocating as much as I asked for, or something.. Notice that 300 x ...
2016-04-15, 1394👍, 0💬

What is the best way of making my program efficient?
What is the best way of making my program efficient? By picking good algorithms, implementing them carefully, and making sure that your program isn't doing any extra work. For example, the most microoptimized character-copying loop in the world will be beat by code which avoids having to copy charac...
2015-02-11, 1381👍, 0💬

How can I read one character at a time, without waiting for the RETURN key
How can I read one character at a time, without waiting for the RETURN key Alas, there is no standard or portable way to do these things in C. Concepts such as screens and keyboards are not even mentioned in the Standard, which deals only with simple I/O ``streams'' of characters. Input to a compute...
2015-11-11, 1369👍, 0💬

What is a good way to implement complex numbers in C?
What is a good way to implement complex numbers in C? It is straightforward to define a simple structure and some arithmetic functions to manipulate them. C99 supports complex as a standard type. Here is a tiny example, to give you a feel for it: typedef struct { double real; double imag; } complex;...
2015-06-19, 1358👍, 0💬

I have seen function declarations that look like this
I've seen function declarations that look like this: extern int func __((int, int)); What are those extra parentheses and underscores for? They're part of a trick which allows the prototype part of the function declaration to be turned off for a pre-ANSI compiler. Somewhere else is a conditional def...
2015-05-23, 1349👍, 0💬

You cant use dynamically-allocated memory after you free it?
You cant use dynamically-allocated memory after you free it? No. Some early documentation for malloc stated that the contents of freed memory were ``left undisturbed,'' but this ill-advised guarantee was never universal and is not required by the C Standard. Few programmers would use the contents of...
2016-04-04, 1345👍, 0💬

I am allocating structures which contain pointers to other dynamically ....
I'm allocating structures which contain pointers to other dynamically-allocated objects. When I free a structure, do I also have to free each subsidiary pointer? Yes. malloc knows nothing about structure declarations or about the contents of allocated memory; it especially does not know whether allo...
2016-03-31, 1345👍, 0💬

How can I determine whether a machines byte order is big-endian or little-endian?
How can I determine whether a machines byte order is big-endian or little-endian? The usual techniques are to use a pointer: int x = 1; if(*(char *)&amp;x == 1) printf("little-endian\n"); else printf("big-endian\n"); or a union: union { int i; char c[sizeof(int)]; } x; x.i = 1; if(x.c[0] == 1) p...
2015-02-16, 1336👍, 0💬

How do I round numbers?
How do I round numbers? The simplest and most straightforward way is with code like (int)(x + 0.5) C's floating to integer conversion truncates (discards) the fractional part, so adding 0.5 before truncating arranges that fractions &gt;= 0.5 will be rounded up. (This technique won't work properl...
2015-06-26, 1333👍, 0💬

How does free know how many bytes to free?
How does free know how many bytes to free? The malloc/free implementation remembers the size of each block as it is allocated, so it is not necessary to remind it of the size when freeing. (Typically, the size is stored adjacent to the allocated block, which is why things usually break badly if the ...
2016-03-24, 1329👍, 0💬

<< < 1 2 3 4 5 6 7 8 9 > >>   Sort: Date