我在程序里对一个cstring变量赋了值,为什么另一个变量也被赋了同样的值?请大侠们指教
解决方案 »
- 请教IE导出导入Cookie 如果编程实现
- 键盘钩子,如何 将 Shift、Ctrl键 修改为 小键盘数字键?
- 请ydfivy(送外卖的兄弟)兄弟进来看看!!!
- 为什么我用CDC::DrawIcon画出来的都是32X32的icon,怎样画其他尺寸的?
- 但文档程序里面MainFrame里怎么得到Doc类的指针?
- 初学者的小问题,大家看看,先谢谢了!
- 为什么我的bind总是出错?
- 从哪能找到所有API函数的详细说明(包括参数、用法、应用场合和注意事项)
- 如何着手windows编程
- CreateDesktop 提示“可用的存储区不足,无法执行该命令”
- 很简单很简单。。。。。。。
- 调用excel时如何设置栏宽度
CString tempinfo;
tempinfo=ServerName;
tempservername="";
后,tempinfo的值也被清空了,真不知道是为什么
int32 BufSize=100;
int32 NodeId=62085;
int32 PortNum=5450;
CString Node="";
CString PortNumStr="";
CMyPIAPI mypiapi;
CString UserName="";
CString tempservername;
CString tempPassWord;
unsigned short UserLen;
CString PassWord;
unsigned short PassLen;
int32 valid;
result=pilg_getconnectedserver(ServerName.GetBuffer(0),&BufSize,&NodeId,&PortNum,lSeq1);
PortNumStr.Format("%d",PortNum);
CString tempinfo;
tempinfo=ServerName;
tempservername="";
tempinfo="PI,"+tempinfo;
运行到这里,tempinfo的值应该是“PI,128.0.0.1”,但是实际却是“PI,”
ServerName是变量
ServerName=“128.0.0.1”
问题在这句话
ServerName作为指针传入, 并只开辟了0字节缓冲内存? 这样在函数内部被赋值, 是很危险的, 因为没有分配内存给ServerName, 所以, ServerName出来后仍然只是指向一个临时内存块而已, tempinfo=ServerName; // 对,是可以赋给它, 但CString之间的赋值,仅仅是一个引用赋值,因为ServerName指向临时内存块,所以,看下面一句:
tempservername=""; // 当tempservername赋值为空后, 系统将ServerName临时内存块分配给tempservername, 这样临时内存块当然是'\0'喽.解决方法
Servername作为引用传入函数!
谢谢大家的关注