C help needed! (19)

1 Name: #!/usr/bin/anonymous : 2008-02-15 13:47 ID:oCqqrrfi

Lint gives me a warning at the malloc in that first loop:

warning: improper pointer/integer combination: op "="

Here's the program...it has quite a few runtime bugs, but the main compiler problem is at that malloc line. Please help!

#include <assert.h>
#include <stdio.h>

typedef struct node *node_ref;
struct node {
char *word;
node_ref link;
};

int main (int argc, char **argv) {
node_ref head = NULL;
node_ref p;
int count;
for (count = 0; count < 5; ++count) {

  node_ref node = malloc (sizeof (struct node));
assert (node != NULL);
node->word = argv[count];
node->link = head;
head = node;

}
for (p = head; p->link != NULL; p = p->link) {

  printf ("%p->{%s, %p}\n", p, p->word, p->link);

}
return 0;
}

2 Name: #!/usr/bin/anonymous : 2008-02-15 15:27 ID:Heaven

Do your own homework.

3 Name: #!/usr/bin/anonymous : 2008-02-15 16:42 ID:Heaven

Don't use Lint and tools as such.
Your code is valid.

4 Name: >>3 : 2008-02-15 16:46 ID:Heaven

>>1
Disregard that, you are using malloc() but you have not included <stdlib.h>
The compiler (in our case, Lint actually) assumes malloc returns int instead of void *.
read mallocs man page (man 3 maloc)

5 Name: #!/usr/bin/anonymous : 2008-02-15 19:17 ID:Heaven

Before you run lint, try building with warnings enabled. Lint works way, way better as a supplementary tool once you've weeded out most or all of the warnings from your compiler.

6 Name: #!/usr/bin/anonymous : 2008-02-16 10:46 ID:Heaven

And anyway, a real implementation of lint for C would immediately say "Warning: source code is in C. Choose a cleaner language. To disable this warning, #define THIS_IS_A_COMPILER_AND_THUS_MAKES_SENSE_TO_WRITE_IN_C in your code."

7 Name: #!/usr/bin/anonymous : 2008-02-16 13:20 ID:Heaven

>>6 why would you write a compiler in C?

8 Name: >>3 : 2008-02-16 16:45 ID:Heaven

>>7
While >>6 made a retarded comment, your reply is lame too.
There are a lot of reasons why you'd want to write a c compiler in c.
The first C compiler was written in C.
read about pcc

9 Name: #!/usr/bin/anonymous : 2008-02-16 20:43 ID:Heaven

>>8
>>6 said nothing about writing a C compiler in C, but was either referring to generated code, or to writing a compiler for some unspecified language in C. I was challenging on the latter.

WRT PCC: it is a toy and a joke. In order to get decent performance, your C code needs to have knowledge of the hardware you're running the output on. Some of those operations occur very frequently, and an optimizing compiler can do a very good job refactoring those things in slightly different ways for different targets.

Optimizing compilers are very hard to write in C. Stackless python was written in a preprocessed pigin C (called lightweight-C++), and pypy is written in python. GCC and ZetaC both use LISP to model the optimizations. Perl6 is being written in Perl and in GHC.

10 Name: #!/usr/bin/anonymous : 2008-02-17 13:43 ID:Heaven

I can't believe people still care about optimisation when the cost of a faster CPU is so low.

11 Name: #!/usr/bin/anonymous : 2008-02-17 15:19 ID:Heaven

>>10
What, do you work for Intel or something?

12 Name: #!/usr/bin/anonymous : 2008-02-17 16:49 ID:eX6PlUui

People expect something like this:

while (*s++) { *d++ = *s++; }

to be automatically unrolled for them in a processor-specific way; some systems are faster doing it with machine-words- some have a way to do a bitwise and on four different masks at the same time (or very quickly). The naive unrolling (even with duff's device) is always slower than GCC's intelligent target-specific unrolling.

And yet, compilers like pcc encourage manual unrolling of loops because they don't do any processor-specific optimizations. pcc will simply make OpenBSD even slower, and harder to read.

13 Name: #!/usr/bin/anonymous : 2008-02-18 00:41 ID:Heaven

>>11

That joke needs some work. Intel wrote what is arguably the best optimizing compiler out there, at least for x86.

14 Name: #!/usr/bin/anonymous : 2008-02-18 00:50 ID:Heaven

>>13

x86 is a dying platform. Finally.

amd64 (aka x86_64 for Intel suckups) is the new x86 and everybody knows it.

15 Name: #!/usr/bin/anonymous : 2008-02-18 12:26 ID:ng7aBFn+

>>13 True, and I thought about that after I posted it.
Nevertheless Intel's compiler optimization has no bearing on how well the code itself is optimized. In other words, even if you're not doing crazy Duff's device unrolling and asm blocks, you still have to be cognizant of how the compiler works and understand how to best write the code in order to take full advantage of the compiler's optimization strategies.

16 Name: #!/usr/bin/anonymous : 2008-02-18 13:15 ID:Heaven

Unfortunately you can rarely guarantee that someone else with their hands on your code is going to use the exact same compiler.

17 Name: #!/usr/bin/anonymous : 2008-02-18 18:58 ID:Heaven

>>16
Yes you can.

#ifdef __GNUC__
#ifdef __BORLANDC__
#ifdef MSC_VER
#ifdef __INTEL_COMPILER

18 Name: #!/usr/bin/anonymous : 2008-02-19 12:11 ID:Heaven

Yeah that sounds like a nice and social to code.

#ifndef __INTEL_COMPILER
#error "Use the compiler I tell you to, fool."
#endif

Perhaps a better idea would be to write portable code in the first place. Plus you know what they say, make the code WORK first. If there is actually a major performance issue, THEN optimise.

19 Name: #!/usr/bin/anonymous : 2008-02-19 20:53 ID:t/4whVPg

Loop unrolling is generally not faster on modern CPUs. for loops are perfectly predicted on newer x86 so all you're doing is wasting I-cache space.

(also, most other things that introduce weird control flow/branching out of the cache/bad branch predicts are also bad, and this probably includes Duff's device)

This thread has been closed. You cannot post in this thread any longer.