讨论/题目交流/求助,请问这是为什么 (已找到问题)/
求助,请问这是为什么 (已找到问题)

已找到问题,replace后会使原来的迭代器失效

使用下标可以解决,但是在最后一个案例会超时,这种方法用时太多

class Solution {
public:
	string replaceSpaces(string S, int length) {
		int count = 0;
		for (int i = 0; i != S.length();) {
			if (S[i] == ' ') {
				S.replace(i, 1, "%20");
				count++;
				i = i + 2;
			}
			else {
				i++;
				count++;
			}
			if (count == length) {
				S[i + 2] = '\0';
				break;
			}
		}
		return S;
	}
};

image.png

这道题目, 我用下面这个解法在自己电脑上用VS可以运行,这边就报错,而且看不懂啊

class Solution {
public:
	string replaceSpaces(string S, int length) {
		int count = 0;
		for (auto iter = S.begin(); iter != S.end();) {
			if (*iter == ' ') {
				auto n_iter = iter;
				while (*iter == ' ') {
					iter++;
				}
				S.replace(n_iter, iter, "%20");
				count++;
                    iter = iter + 2;
			}
			else {
				count++;
				iter++;
			}
			if (count == length) break;
		}
		return S;
	}
};

这是报错信息

=================================================================
==45==ERROR: AddressSanitizer: heap-use-after-free on address 0x6030000000d3 at pc 0x00000038ef33 bp 0x7fffcb526730 sp 0x7fffcb526728
READ of size 1 at 0x6030000000d3 thread T0
    #3 0x7f1021ed982f  (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
0x6030000000d3 is located 3 bytes inside of 18-byte region [0x6030000000d0,0x6030000000e2)
freed by thread T0 here:
    #10 0x7f1021ed982f  (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
previously allocated by thread T0 here:
    #5 0x7f1021ed982f  (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
Shadow bytes around the buggy address:
  0x0c067fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c067fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c067fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c067fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c067fff8000: fa fa 00 00 00 07 fa fa fd fd fd fa fa fa 00 00
=>0x0c067fff8010: 02 fa fa fa 00 00 02 fa fa fa[fd]fd fd fa fa fa
  0x0c067fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c067fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c067fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c067fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c067fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
  Shadow gap:              cc
==45==ABORTING

请问这是为什么啊

展开讨论
道友丨请留步发起于 2020-05-15
最近编辑于 2020-05-16

越界了,vs能跑应该是vs底层帮你处理越界的问题了,但是leetcode上没有

展开全部 3 讨论