UPD2: The algoritm generates bytes out of allocated range. So it's a logical mistake.
UPD: modifyed printf to
printf("%d\n", bytes);
But the result is same - SIGSEGV.
I am new in pure c coding. This is simple near "hello world" app:
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
void encode2(int src){
unsigned char * result =(unsigned char *) malloc(sizeof(unsigned char) *10);
int bytes = 0;
bool More;
do{
uint8_t Byte = src & 0x7f;
src >> 7;
More = !((((src == 0) && ((Byte & 0x40) == 0)) ||
((src == -1) && ((Byte & 0x40) != 0))));
if (More)
Byte |= 0x80;
result[bytes] = Byte;
bytes++;
} while(More);
printf(bytes);
unsigned char * str = (unsigned char *) malloc(sizeof(unsigned char) * bytes);
int i;
for (i=0; i<bytes; i++){
str[i] = result[i];
}
}
int main(int argc, char *argv[]){
encode2(10);
/*printf("%s\n",p);*/
return 0;
}
Job 1, './leb128' terminated by signal SIGSEGV (Address boundary error)
Tried to google SIGSEGV but couldn't found whats wrong?