数组拷贝的长度问题

shitianhen 2002-05-22 02:53:16
我现在定义一个数组
a[255];
然后对它进行赋指,拷贝
前面都很好,由于我的字符串很长,又要一次性处理
所以当我拷到后面的时候
strcat(a,"sadgas");
无论你再怎么处理,a这个字符数组就连不下去了

请各位好手多多执教,不胜感激!!!!!!!!1
...全文
80 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
coyj 2002-05-22
  • 打赏
  • 举报
回复
vc中的strcat的c实现如下:
char * strcat (char * dst, char * src)
{
char * cp = dst;

while( *cp )
++cp; /* Find end of dst */
while( *cp++ = *src++ )
; /* Copy src to end of dst */
return( dst );
}
汇编实现如下:
strcat proc

.FPO ( 0, 2, 0, 0, 0, 0 )

mov ecx,[esp+4] ; ecx -> dest string
push edi ; preserve edi
test ecx,3 ; test if string is aligned on 32 bits
je short find_end_of_dest_string_loop

dest_misaligned: ; simple byte loop until string is aligned
mov al,byte ptr [ecx]
inc ecx
test al,al
je short start_byte_3
test ecx,3
jne short dest_misaligned

align 4

find_end_of_dest_string_loop:
mov eax,dword ptr [ecx] ; read 4 bytes
mov edx,7efefeffh
add edx,eax
xor eax,-1
xor eax,edx
add ecx,4
test eax,81010100h
je short find_end_of_dest_string_loop
; found zero byte in the loop
mov eax,[ecx - 4]
test al,al ; is it byte 0
je short start_byte_0
test ah,ah ; is it byte 1
je short start_byte_1
test eax,00ff0000h ; is it byte 2
je short start_byte_2
test eax,0ff000000h ; is it byte 3
je short start_byte_3
jmp short find_end_of_dest_string_loop
; taken if bits 24-30 are clear and bit
; 31 is set
start_byte_3:
lea edi,[ecx - 1]
jmp short copy_start
start_byte_2:
lea edi,[ecx - 2]
jmp short copy_start
start_byte_1:
lea edi,[ecx - 3]
jmp short copy_start
start_byte_0:
lea edi,[ecx - 4]
; jmp short copy_start

; edi points to the end of dest string.
copy_start::
mov ecx,[esp+0ch] ; ecx -> sorc string
test ecx,3 ; test if string is aligned on 32 bits
je short main_loop_entrance

src_misaligned: ; simple byte loop until string is aligned
mov dl,byte ptr [ecx]
inc ecx
test dl,dl
je short byte_0
mov [edi],dl
inc edi
test ecx,3
jne short src_misaligned
jmp short main_loop_entrance

main_loop: ; edx contains first dword of sorc string
mov [edi],edx ; store one more dword
add edi,4 ; kick dest pointer
main_loop_entrance:
mov edx,7efefeffh
mov eax,dword ptr [ecx] ; read 4 bytes

add edx,eax
xor eax,-1

xor eax,edx
mov edx,[ecx] ; it's in cache now

add ecx,4 ; kick dest pointer
test eax,81010100h

je short main_loop
; found zero byte in the loop
; main_loop_end:
test dl,dl ; is it byte 0
je short byte_0
test dh,dh ; is it byte 1
je short byte_1
test edx,00ff0000h ; is it byte 2
je short byte_2
test edx,0ff000000h ; is it byte 3
je short byte_3
jmp short main_loop ; taken if bits 24-30 are clear and bit
; 31 is set
byte_3:
mov [edi],edx
mov eax,[esp+8] ; return in eax pointer to dest string
pop edi
ret
byte_2:
mov [edi],dx
mov eax,[esp+8] ; return in eax pointer to dest string
mov byte ptr [edi+2],0
pop edi
ret
byte_1:
mov [edi],dx
mov eax,[esp+8] ; return in eax pointer to dest string
pop edi
ret
byte_0:
mov [edi],dl
mov eax,[esp+8] ; return in eax pointer to dest string
pop edi
ret

strcat endp
此函数有bug,即缓冲区溢出,但不会出现你这样的情况,问题不在strcat。
Riemann 2002-05-22
  • 打赏
  • 举报
回复
是你的数组的长度不够吧
shitianhen 2002-05-22
  • 打赏
  • 举报
回复
char send_buf[400];
send_buf[0] = '\0';

strcpy(send_buf,"VccsLine_Ex.Send(CMEx,0x35,0x08,");
strcat(send_buf,"0x04,0x81,");
strcat(send_buf,this->SlaveVersionData);//this->SlaveVersionData 是 “0x12,0x13”,之类的,以下同
strcat(send_buf,",0x06,0x82,");
strcat(send_buf,this->TimeStampRecord);
strcat(send_buf,",0x06,0x83,");
strcat(send_buf,this->CoinAcceptRateData);
strcat(send_buf,",0x06,0x84,");
strcat(send_buf,this->FakeCoinAlarmData);
strcat(send_buf,",0x02,0x85,");
strcat(send_buf,this->CheckSWOnData);
strcat(send_buf,",0x06,0x86,");
strcat(send_buf,this->AcceptAccuracyStatusData);
strcat(send_buf,",0x02,0x87,");
strcat(send_buf,this->PayoutRetryData);
fang_jb 2002-05-22
  • 打赏
  • 举报
回复
你确定在拷不下去的时候你的字符串还有剩余空间吗?

如果都没有毛病的话,呵呵,只有贴代码出来大家看看了
shitianhen 2002-05-22
  • 打赏
  • 举报
回复
O ,那我看一下,
但如果没有呢
fang_jb 2002-05-22
  • 打赏
  • 举报
回复
strcat一般不会出问题,只要你的初始长度足够,
看看是不是strcat了一个非法字符,例如'\0'之类的东西:)
coyj 2002-05-22
  • 打赏
  • 举报
回复
你用的编译器vc6吧,我反汇编看看strcat,建议自己编一个字符串连接函数解决
fang_jb 2002-05-22
  • 打赏
  • 举报
回复
strcat了一个非法字符,
比如有一个字符可能是'\0',
你直接strcat上去了,后面的东西你怎么strcat也不会到串里了,
呵呵,我只是说可能。

strcat一般不会有问题,只要你初始的长度足够
shitianhen 2002-05-22
  • 打赏
  • 举报
回复
好象没用哦
Rebuild都没用,写也写过了
就你看,有没有内存溢出或什么方面的原因
kof99th 2002-05-22
  • 打赏
  • 举报
回复
把原来的空间删掉,从新分配内存,再拷过来.
rester214 2002-05-22
  • 打赏
  • 举报
回复
char send_buf[400];
send_buf[0] = '\0';

strcpy(send_buf,"VccsLine_Ex.Send(CMEx,0x35,0x08,");
strcat(send_buf,"0x04,0x81,");
strcat(send_buf,this->SlaveVersionData);
strcat(send_buf,",0x06,0x82,");
strcat(send_buf,this->TimeStampRecord);
strcat(send_buf,",0x06,0x83,");
strcat(send_buf,this->CoinAcceptRateData);
strcat(send_buf,",0x06,0x84,");
strcat(send_buf,this->FakeCoinAlarmData);
strcat(send_buf,",0x02,0x85,");
strcat(send_buf,this->CheckSWOnData);
strcat(send_buf,",0x06,0x86,");
strcat(send_buf,this->AcceptAccuracyStatusData);
strcat(send_buf,",0x02,0x87,");
strcat(send_buf,this->PayoutRetryData);

其中this->SlaveVersionData等为字符串。
coyj 2002-05-22
  • 打赏
  • 举报
回复
贴一段较为关键的原码,首先你的a足够大

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧