Pdの問題である可能性があるため、これをクロスポストしていますが、おそらく誰かがこのようなことが起こる理由を知っています.
手短に言えば、Pd (コンピューター音楽のピュアデータ) という名前のプログラムで使用している dll があります。recordString というオブジェクトを作成しました。これを基本的な Pd API と組み合わせて使用し、pd が読み取る dll を作成します。Pd がアクセスする必要があるのは、いくつかの単純な関数だけです。Pd API を使用するコードの一部として recordString を作成しました。
とにかく、recordString オブジェクトには、new を使用して作成している char* が含まれています (malloc も試しました)。char* の値を設定すると、正しく HELLOWORLD に設定されます。アドレスを出力して、あるべき場所に留まることを確認しています。
後で関数を呼び出して char* の値を取得する場合を除いて、値が本来あるべき値であることを確認しましたが、何らかの理由で 1 バイトシフトされています。
ポインターが値 1 だけアドレスを変更する状況を聞いたことがありますか? 言われなくても?
とにかく、ここに出力があり、それに続くのはdatarecord.cppのコードです
文字列: HELLOWORLD 長さ: 10 アドレス: 32774028 アドレス-2: 32578488 postString の呼び出し 文字列: ELLOWORLD 長さ: 9 アドレス: 32774028 アドレス-2: 32578489
#include "m_pd.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "datarecord.h"
using namespace std;
recordString::recordString()
:name(NULL)
{
};
recordString::recordString(const char* sourceString)
{
this->name = new char[strlen(sourceString)+1];
post("-------- in string constructor -----------");
post("address: %d", &this->name);
strcpy(this->name, sourceString );
post("copy: %s", this->name);
};
recordString::~recordString()
{
//delete(this->name);
delete[] name;
name = NULL;
//free(this->name);
};
recordString::recordString(const recordString & rhs)
{
post("-------- in copy constructor -----------");
post("source: %s", rhs.name);
post("length: %d", strlen(rhs.name));
post("\n");
this->name = new char[strlen(rhs.name)+1];
strcpy(this->name, rhs.name);
post("copy: %s", this->name);
post("length: %d", strlen(this->name));
post("address: %d", &name);
post("address-2: %d", name);
post("\n");
}
recordString & recordString::operator=(const recordString &rhs)
{
post("-------- in operator= -----------");
post("source: %s", rhs.name);
post("length: %d", strlen(rhs.name));
post("\n");
if(name!=NULL)
{
delete[] name;
}
//this->name = (char*) malloc((strlen(rhs.name)));
this->name = new char[strlen(rhs.name)+1];
strcpy(this->name, rhs.name);
post("copy: %s", this->name);
post("length: %d", strlen(this->name));
post("address: %d", &name);
post("address-2: %d", name);
post("\n");
}
int recordString::setString(const char * sourceString)
{
post("-------- in setString -----------");
post("source: %s", sourceString);
post("length: %d", strlen(sourceString));
post("\n");
this->name = new char[strlen(sourceString)];
strcpy(this->name, sourceString);
post("copy: %s", this->name);
post("length: %d", strlen(this->name));
post("address: %d", &name);
post("address-2: %d", name);
post("\n");
return (this->name == NULL);
}
void recordString::postString()
{
post("string: %s", this->name);
post("length: %d", strlen(this->name));
post("address: %d", &name);
post("address-2: %d", name);
post("\n");
}