[C]文字列の配列を返す関数って - ちょっと練習 2008/11/05

C言語です。
はじめてEclipseのC/C++版を使ってみたりしてます。
ctrl + spaceで関数を補完してくれるんですね!!

文字列の配列を返す関数ってどう書くのだろうと、記憶をたどりつつ。
あやうい。
Orz...

char **ss = (char **) malloc(sizeof(char *) * len);


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

char** b(int len);

int main(void) {
int cnt = 0;
while (cnt++ < 900000) {
int len = 100;
char** ss = b(len);
int i = 0;
for (i = 0; i < len; i++) {
fprintf(stderr, "%d %s\n", i, ss[i]);
free(ss[i]);
}
free(ss);
}

return EXIT_SUCCESS;
}

char** b(int len) {
char **ss = (char **) malloc(sizeof(char *) * len);
int i = 0;
// char buf[1000];
// memset(buf, 0x0, 1000);
for (i = 0; i < len; i++) {
char* buf = malloc(sizeof(char) * 1000);
memset(buf, 0x0, 1000);
sprintf(buf, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa%d", i);
ss[i] = buf;
fprintf(stdout, "%s\n", ss[i]);
}
//free(ss);
return ss;
}

void a(int len) {
char **ss = (char **) malloc(sizeof(char *) * len);
int i = 0;
char buf[1000];
for (i = 0; i < len; i++) {
sprintf(buf, "aaaaaaaaa%d", i);
ss[i] = buf;
fprintf(stdout, "%s\n", ss[i]);
}
free(ss);
}

: