Sum of Two Numbers TWO SUM
Title: Given an array of integers nums and a target value, you can find the two integers in the array that are sum to the target value and return their array subscripts.
Example:
Given nums = [2, 7, 11, 15], target = 9
Because nums[0] + nums[1] = 2 + 7 = 9
So go back [0, 1]
Initial solution:
int* twoSum(int* nums, int numsSize, int target, int* returnSize){ int i=0,j=0; while(nums[i]+nums[j]!=target){ for(i=0;i<numsSize;i++){ for(j=i+1;j<numsSize;j++){ if(nums[i] + nums[j] == target){ printf("[%d,%d]",i,j); } } } } return 0; }
Run result:At first I didn't understand why this error was. The output should be correct (even after the middle brackets are removed). The error message is as follows (seems to be a heap overflow?):
=================================================================
29ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000020 at pc 0x0000004019ab bp 0x7ffcb39f9e20 sp 0x7ffcb39f9e18
READ of size 4 at 0x602000000020 thread T0
#2 0x7fa483b9f2e0 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x202e0)
0x602000000020 is located 0 bytes to the right of 16-byte region [0x602000000010,0x602000000020)
allocated by thread T0 here:
#0 0x7fa48544e2b0 in malloc (/usr/local/lib64/libasan.so.5+0xe82b0)
#3 0x7fa483b9f2e0 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x202e0)
Shadow bytes around the buggy address:
0x0c047fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c047fff8000: fa fa 00 00[fa]fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8010: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8050: 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
29ABORTING
First fix code:
int* twoSum(int* nums, int numsSize, int target, int* returnSize){ int i=0,j=0; for(i=0;i<numsSize;i++){ for(j=i+1;j<numsSize;j++){ if(nums[i] + nums[j] == target){ printf("[%d,%d]",i,j); } } } return 0; }
The reason for the first error is that the stack overflowed due to the addition of while statements. It does not seem necessary to think about it carefully. The two for loops are solved, and I need to check the specific reason sometime.
Run again and there was an error:
Line 207: Char 3: runtime error: load of null pointer of type 'int' (Serializer.c)
This time I understand, because the title requires that the function return type be int*, and my return type is integer int, which returns an error.
Second code modification:
int* twoSum(int* nums, int numsSize, int target, int* returnSize){ int *a = (int *)malloc(sizeof(int) * 2); int i, j; for (i = 0; i < numsSize - 1; i++) { for (j = i + 1; j < numsSize; j++) { if (nums[i] + nums[j] == target) { a[0] = i; a[1] = j; *returnSize = 2; return a; } } } *returnSize = 0; return a; }
get!
Postnote
Now is January 13, 2020. I plan to practice doing algorithmic exercises every day from now on, remember my mistakes, and make fewer mistakes in the future. I hope I can stick to it, and strive to improve myself whether or not I will become a program ape in the future.
One small step a day, one big step over time!