我正在交叉发布这个,因为它可能是一个 Pd 问题,但也许这里有人知道为什么会发生这样的事情。
简而言之,我有一个 dll,我在名为 Pd(puredata,用于计算机音乐)的程序中使用它。我创建了一个名为 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");
}