これは、私が今書いた関数の簡略化されたバージョンです。
int foobar(char * foo) {
puts(type);
struct node * ptr = (struct node *) malloc (sizeof(struct node));
puts(type);
memset(ptr, 0, sizeof(ptr));
ptr=head;
return head->id;
}
これは、 linklistnode
内のノードとして宣言された単なる構造体でありchar *
、次のノードへの およびポインターを含んでいます。ただし、malloc()
ここで入力が破損していることに気付きましたchar * foo
。
malloc()
入力 char ポインターが破損するのはなぜですか? また、ここで問題を解決するにはどうすればよいですか? 今、私はそのポインターの内容をローカル配列にコピーしているだけですが、これは私の好みでもハッキーすぎます (これは最善ではありません)。
ご意見ありがとうございます。
編集:まあ、ここにもっと実際のコードがあります:
void foobar(char * type) {
puts(type); <-- here it's a long string about 30 char
struct node * ptr = (struct node *) malloc (sizeof(struct node));
puts(type); <- chopped of, 10 left with some random thing at the end
}
問題が今明らかになることを願っています!ありがとう!
編集:
type
初期化の方法は次のとおりです。
type = strdup("Some ");
tempType = strdup("things");
sprintf(type + strlen(type), "%s", tempType);
ありがとう!