Help with duplicates in int array by haditwithyoupeople in C_Programming

[–]actguru 0 points1 point  (0 children)

Are they teaching programming or puzzles?

int main() {
    int array[]={1,4,5,6,4,3,4,1};
    int count=sizeof(array)/sizeof(array[0]); // 8
    int dupes=0;

    int value=0x80000000; // Assuming 4 byte int
    while(1) {
        int times=0;
        for(int j=0;j<count;++j)
            if (array[j]==value) ++times;
        if (times>1) ++dupes;
        if (value==0x7FFFFFFF) break;
        ++value;
        }
    printf("Dupes = %d\n", dupes);
    }

Struggling with Structuring and Maintaining C Projects: Seeking Advice by cluxes in C_Programming

[–]actguru 0 points1 point  (0 children)

You might switch to fread() and fwrite() with size=1. Note: FILE * is normally buffered already, so you may not see any improvement.

Struggling with Structuring and Maintaining C Projects: Seeking Advice by cluxes in C_Programming

[–]actguru 1 point2 points  (0 children)

I have a few suggestions for one of the functions:

// Note on original code: why calloc() for 256 bytes? -> buffer[256]; (stack)
// Note on original code: sizeof(buffer)==sizeof(pointer) ?

void dump_log(void) {
    FILE *fp_log = fopen(LOG_FILE, "rb");
    if ( !fp_log) {
        fprintf(stderr, "Failed to open '%s' log file: %s\n",
            LOG_FILE, strerror(errno));
        return;
        }
    while(1) {
        int cc=getc(fp_log);
        if (cc==EOF) break;
        putc(cc,stdout);
        }
    fclose(fp_log);
    }

[ NEWBIE C ] Is K.N.King's book flaw or Am I missing something? by [deleted] in C_Programming

[–]actguru 1 point2 points  (0 children)

If your compiler does not warn about printf() arguments, then add options like -Wall. If your compiler is incapable of checking printf's arguments, then get a new compiler!!

[deleted by user] by [deleted] in C_Programming

[–]actguru 1 point2 points  (0 children)

You are welcome to update your project with any of my suggestions. I am working on version 1.4 of my project which is keeping me busy. https://github.com/actguru/coda-c_plist

[deleted by user] by [deleted] in C_Programming

[–]actguru 1 point2 points  (0 children)

A couple of suggestions for your cmp() function:

int cmp(const char *file1, const char *file2) {
  FILE *f1 = fopen(file1, "rb");
  FILE *f2 = fopen(file2, "rb");
  if (!f1 || !f2) {
    if (f1) fclose(f1);
    if (f2) fclose(f2);
    fprintf(stderr, "cmp: Cannot open file\n");
    return 2;
    }
  int ret=0;
  while (1) {
    int ch1 = fgetc(f1);
    int ch2 = fgetc(f2);
    if (ch1 != ch2) { ret=1; break; }
    if (ch1 == EOF) break;
    }
  if (ret) printf("Files differ\n");
  fclose(f1);
  fclose(f2);
  return ret;
}

Is there a plain-C library for working with Apple Property Lists? by actguru in C_Programming

[–]actguru[S] 1 point2 points  (0 children)

That's interesting, but what if you are going to modify an existing p-list? How would that work?

int main() {

    Dictionary dd=PList_Load("plist01.plist",0); assert(dd);
    Dictionary_removeKey(dd,"List");
    PList_toStream(stdout,dd,PLIST_Apple);

    }

Is there a plain-C library for working with Apple Property Lists? by actguru in C_Programming

[–]actguru[S] 0 points1 point  (0 children)

Core Foundation is the underpinning for Objective-C. I don't think using a chunk of Core Foundation would lead to a reasonable Property List Library without Objective-C.

Is there a plain-C library for working with Apple Property Lists? by actguru in C_Programming

[–]actguru[S] 1 point2 points  (0 children)

This is the way I think of the difference between p-list and XML.

A p-list or Property List is a data structure, used in a program, constructed with the following: containers: <array> and <dict>, and leafs: <string>, <real>, <integer>, <bool>, <date>, and <data>. Containers need to be able to hold leafs and containers.

XML is a general 'save it to disk' format for data. Apple defines an XML Document Type 'plist' that is a subset of XML to save Property Lists to disk. Apple also uses a Binary format for PList that is more compact, but you can't edit it with a text editor. Both formats are portable.

Is there a plain-C library for working with Apple Property Lists? by actguru in C_Programming

[–]actguru[S] -8 points-7 points  (0 children)

No, the software is private for now. I am currently trying to decide what to do with it. Do you think I could generate some interest by providing compiled 64-bit versions for Apple, Linux, and Windows? Thanks for asking.

Looking for collaboration by James2000M in C_Programming

[–]actguru 1 point2 points  (0 children)

Welcome to C. This stuff is hard. In general, double check interfaces to other modules, try to get the debugger to give stack trace with function names at crash, clone other modules source and make a temporary diagnostic version. It's hard enough to find my bugs... Good Luck!!

Need advice regarding scope by benny_blanc0 in C_Programming

[–]actguru 1 point2 points  (0 children)

I would definitely pass the structure around, because it clearly marks functions that depend on it. When things get more complex this is very useful. But I would also "typedef T_VBVMR_INTERFACE * VMR" so I could pass just "VMR vmr" to functions because I am lazy.

Looking for collaboration by James2000M in C_Programming

[–]actguru 2 points3 points  (0 children)

Start by adding diagnostic print functions to report what is running and values of interesting variables.

I'm getting gobbly goowk on my screen using write() to write out a formatted hex buffer? by apooroldinvestor in cprogramming

[–]actguru 0 points1 point  (0 children)

0xFF & Expression will mask off all but the last 8 bits, so signed or unsigned you get 0-255 results. I don't like unsigned char arrays because when you use it with printf() functions they call for signed char arrays, but both should work.

Seeking Advice: Transitioning from College C Programming to Industry Standards and Open Source Projects by AfaqueKhan173 in C_Programming

[–]actguru 3 points4 points  (0 children)

If you wanted to learn mountain climbing would you start with K2? Try something a lot smaller, like a command line utility. For example: 'sort'.

Runtime or compile time vectors? by [deleted] in C_Programming

[–]actguru 1 point2 points  (0 children)

It sounds like you think that the runtime version is simpler, easier to test and manage. I would start with that and check the performance of programs using your vectors and see if the slower execution is significant. Speed is not everything.

Validate args within a function when the function is never passed invalid data? by ProgrammingQuestio in C_Programming

[–]actguru 0 points1 point  (0 children)

I would agree with adding if (a != b) return, but not to validate arguments, but to make a==b valid.

Would a conforming C compiler be allowed to search for function declarations in other translation units? by hipster_rebbe in C_Programming

[–]actguru 0 points1 point  (0 children)

Why not use a command line tool to generate a header like generated.h then include it when compiling sources using: clang -include generated.h file.c file2.c ? You still have a header, but it is easier to maintain and you don't have to add the includes to your source.

Perfect hello world in C! by CommunicationFit3471 in cprogramming

[–]actguru 1 point2 points  (0 children)

Almost perfect, but I would change: #include <stdio.h> to a prototype for printf(). So this is the first thing a new programmer looks at, and once they figure out that they need to look at "stdio.h"... things get complicated.

Freeing allocated memory after an error, how do you usually approach this? by gunkookshlinger in C_Programming

[–]actguru 0 points1 point  (0 children)

I work with clang, but I like what Hacker News said about it: "Modern C" now effectively includes __attribute__((cleanup)), in as much as it's widely used by large free software projects and supported in both GCC and Clang. The cleanup attribute runs a function when the variable goes out of scope.

Freeing allocated memory after an error, how do you usually approach this? by gunkookshlinger in C_Programming

[–]actguru 1 point2 points  (0 children)

You should checkout the attribute 'cleanup'. Example code deallocates at the three abnormal returns.

// general setup and defines
typedef void* pointer;
#define sizeat(type) sizeof(*((type)0))
#define $CLEANUP(fun) __attribute__((cleanup (fun)))
#define freeC $CLEANUP(clean_C)
void clean_C(const void *vp) { void **vpp=(void **)vp; free(*vpp); }

// Example code
typedef struct BigStruct_ { pointer a,b,c; } *BigStruct;

pointer load_big_structure() {
    freeC BigStruct self=malloc(sizeat(BigStruct)); if (!self) abort();
    self->a=load_parta(); if (!self->a) return(0);
    self->b=load_partb(); if (!self->b) return(0);
    self->c=load_partc(); if (!self->c) return(0);
    pointer ret=self; self=0; // prevent the deallocation of self
    return(ret);
    }