Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

[C] explode()

Name: Anonymous 2015-07-29 1:02

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>

struct v {
void *p;
size_t n;
};

struct v **explode(const void *p, const void *q, size_t pn, size_t qn, size_t size);
void *memmem(const void *p, const void *q, size_t pn, size_t qn, size_t size);
void *memdup(const void *p, size_t n);

int main(void) {
struct v **p = explode("a b", " ", 3, 1, 1);
struct v **q = p;
if(p == NULL) {
perror(NULL);
return EXIT_FAILURE;
}
while(*p) {
fwrite((*p)->p, 1, (*p)->n, stdout);
putchar(' ');
p++;
}
putchar('\n');
p = q;
while(*p != NULL) {
free((*p)->p);
free(*p++);
}
free(q);
return 0;
}

struct v **explode(const void *p, const void *q, size_t pn, size_t qn, size_t size) {
void *tmp;
struct v **rv = NULL;
size_t i;
ptrdiff_t j;
pn *= size;
qn *= size;
for(i = 0;; i++) {
while(pn >= qn && memcmp(p, q, qn) == 0) {
p = (char *)p + qn;
pn -= qn;
}
if((tmp = realloc(rv, (i + 1) * sizeof *rv)) == NULL) {
while(i--) {
free(rv[i]->p);
free(rv[i]);
}
free(rv);
return NULL;
}
rv = tmp;
rv[i] = NULL;
if(pn == 0) break;
if((rv[i] = malloc(sizeof **rv)) == NULL) {
while(i--) {
free(rv[i]->p);
free(rv[i]);
}
free(rv);
return NULL;
}
if((tmp = memmem(p, q, pn, qn, size)) == NULL) {
if((rv[i]->p = memdup(p, pn)) == NULL) {
while(i--) {
free(rv[i]->p);
free(rv[i]);
}
free(rv);
return NULL;
}
rv[i]->n = pn;
break;
}
else {
j = (char *)tmp - (char *)p;
if((tmp = memdup(p, j)) == NULL) {
while(i--) {
free(rv[i]->p);
free(rv[i]);
}
free(rv);
return NULL;
}
rv[i]->p = tmp;
rv[i]->n = j;
p = (char *)p + j;
pn -= j;
}
}
if(rv[i] != NULL) {
if((tmp = realloc(rv, (i + 2) * sizeof *rv)) == NULL) {
i++;
while(i--) {
free(rv[i]->p);
free(rv[i]);
}
free(rv);
return NULL;
}
rv[i + 1] = NULL;
}
return rv;
}

void *memdup(const void *p, size_t n) {
void *rv = NULL;
if((rv = malloc(n)) != NULL) memcpy(rv, p, n);
return rv;
}

void *memmem(const void *p, const void *q, size_t pn, size_t qn, size_t size) {
for(; pn >= qn; p = (unsigned char *)p + 1, pn -= size)
if(memcmp(p, q, qn) == 0) return p;
return NULL;
}

Name: Anonymous 2015-07-31 22:47

>>38
In terms of absolute numbers, I certainly agree that embedded systems and lower-powered computers have far wider deployment numbers than high performance PCs. However, it only takes a small number of programmers to write programs for these kinds of systems, there are small numbers of specialist programmers who do special-purpose programming work. The vast majority of programmers do not do any programming apart from application software running on high performance machines.

Name: Anonymous 2015-08-01 0:01

Name: Cudder !cXCudderUE 2015-08-01 5:02

>>35
Yes it is. If if was a piece of code that would be run at most once or twice by yourself and likely no one else, then it doesn't make sense to spend much time optimising it beyond getting the job done in a reasonable amount of time.

But if you're working on software that will be used by tens of thousands if not more users, every day, then you'd better be taking care to make sure it's efficient.

It's amusingly hypocritical when developers complain about how slow/bloated the tools they use are, then do the same thing themselves - maybe even in writing software for other developers to use! Stop being a selfish inconsiderate bastard and "do to others as you would have them do to you."

Name: Cudder !cXCudderUE 2015-08-01 5:03

See also: https://xkcd.com/1205/ (multiplied by how many users you have or expect to have.)

Name: Anonymous 2015-08-01 5:09

>>43,44
What? Fuck other people's time. Most don't pay me, so don't get to treat me like a bitch, and the ones that do often want me to do the job as quickly as possible, even if it is unoptimized and buggy, so they can stop paying me as soon as possible.

Name: Anonymous 2015-08-01 5:19

>>45
Back to >>>/JavaScript/. ``They `pay us`. Dumb fucks.''

Name: Anonymous 2015-08-02 11:59

Up, up and away, programming-related.

Name: Anonymous 2015-08-02 14:04

If the total gain of a change in your code is less than ten times the time you needed to implement it, it is not worth it.

Name: Anonymous 2015-08-02 15:50

>>43,44
Holy shit that comic is fucking stupid. What is he even trying to say here? Is he just going to ignore the vast futures of times I am going to do the ``task'', where I easily make up for the ``time spent'' which will not occur every again? FUCK IHBT. FUK U CUDDER.

Name: Anonymous 2015-08-02 21:07

>>49
I agree. And besides, wasting your own time and wasting someone else's time are two different things.

Name: Anonymous 2015-08-02 21:09

>>44
someone post the goatse version of this

Name: Anonymous 2015-08-02 21:37


Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List