c++ - "Disallowed system call: SYS_socketcall" when I try to solve the "sum the largest n integers in the array" programming challenge -


i'm trying come solution

sum n largest integers in array of integers every integer between 0 , 9

int sumnlargest(int* andata, int size, int n) 

programming challenge prompt, solution other obvious 1 of sorting copy of array , returning sum of last 9 elements.so tried writing linear solution below

#include <iostream>  int sumnlargest(int* andata, int size, int n) { // sum n largest integers in array of integers every integer between 0 , 9     int cntarr [] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};     (int = 0; < size; ++i) ++cntarr[andata[i]];     int sum = 0;     (int = size - 1; >= 0; --i)     {         sum += (n - cntarr[i]) >= 0 ?  cntarr[i] * : n * i;         --n;         if (n <= 0) break;     }        return sum; }   int main() {     int myarray [] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 11, 12, 15};     std::cout << sumnlargest(myarray, sizeof(myarray)/sizeof(int), 2);     return 0; } 

but i'm getting error

disallowed system call: sys_socketcall

see: http://codepad.org/uilgxdzq

is problem logic? if so, where? also, there more elegant linear solution should've done instead?

(and finally, realize solution assumes n >= size >= 1, think typical programming interview allows me make assumption don't have waste time writing bunch of error handling unexpected input)

your code invokes undefined behavior at

 (int = 0; < size; ++i)          ++cntarr[andata[i]];  

when access eleventh element (i = 10) because cntarr has 10 elements.
memory checker such addresssanitizer indicate immediately.

you're not calling system call create new socket but, result of undefined behavior, happen.


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -