F1e3ab214a976a39cfd713bc93deb10f

What is the least inexpensive way to have the functionality of below? i am not sure what the best conventions are. global static buffer? malloc'd buffer? locally scoped static buffer like below?

char *
Method_to_s(Method *self) {
  static char buf[1024];
  sprintf(buf, "__send__(%s, %s, %s)", self->name, self->receiver, self->args);
  return strdup(buf);
}

Refactorings

No refactoring yet !

F9a9ba6663645458aa8630157ed5e71e

Ants

September 18, 2009, September 18, 2009 02:21, permalink

No rating. Login to rate!

Least in expensive in terms of CPU, memory, or readability?

F1e3ab214a976a39cfd713bc93deb10f

Tj Holowaychuk

September 18, 2009, September 18, 2009 20:14, permalink

No rating. Login to rate!

well a bit of both. just wondering if this is way off. it is just to compile one language to another so it does not need to be really fast

D41d8cd98f00b204e9800998ecf8427e

bob

September 19, 2009, September 19, 2009 07:14, permalink

No rating. Login to rate!

if you are using the GNU C library:

char *Method_to_s(Method *self) {
  char *result;
  if (asprintf(&result, "__send__(%s, %s, %s)", self->name, self->receiver, self->args))
    return NULL; // error
  return result;
}
F1e3ab214a976a39cfd713bc93deb10f

Tj Holowaychuk

September 19, 2009, September 19, 2009 16:56, permalink

No rating. Login to rate!

does that not produce an error due to returning a local pointer?

F1e3ab214a976a39cfd713bc93deb10f

Tj Holowaychuk

September 19, 2009, September 19, 2009 16:57, permalink

No rating. Login to rate!

nvm! just read the description for it

A07f8d0f843a8ec4dad62bc9e87511ba

kulp

October 29, 2009, October 29, 2009 20:46, permalink

No rating. Login to rate!

However, bob's solution needs to check asprintf's return value for being less than zero, not non-zero. *s*printf() calls generally return the number of bytes printed, which is not often zero. :)

Your refactoring





Format Copy from initial code

or Cancel