Reputation: 33
I try to convert a integer in a binary format and I want the result in a char*, so I have this code :
unsigned int_to_int(unsigned k) {
if (k == 0) return 0;
if (k == 1) return 1; /* optional */
return (k % 2) + 10 * int_to_int(k / 2);
}
char* tab_binaire(int nb){
char* str = (char*) malloc(sizeof(char)*8);
sprintf(str, "%d", int_to_int(nb));
int zeroManquant = 8-strlen(str);V
char* res= (char*) malloc(zeroManquant*sizeof(char));
for (int i = 0; i < zeroManquant; i++) {
res[i]='0';
}
strcat(res,str);
free(str);
str=NULL;
return res;
}
I already try to put the exit character at the end of the string but I always put the errors. Here are my errors :
==10789== Invalid read of size 1
==10789== at 0x483B924: strcat (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==10789== by 0x1097C3: tab_binaire (automate.c:145)
==10789== by 0x10983F: main (automate.c:161)
==10789== Address 0x4a54ae4 is 0 bytes after a block of size 4 alloc'd
==10789== at 0x483874F: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==10789== by 0x109787: tab_binaire (automate.c:140)
==10789== by 0x10983F: main (automate.c:161)
==10789==
==10789== Invalid write of size 1
==10789== at 0x483B940: strcat (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==10789== by 0x1097C3: tab_binaire (automate.c:145)
==10789== by 0x10983F: main (automate.c:161)
==10789== Address 0x4a54ae4 is 0 bytes after a block of size 4 alloc'd
==10789== at 0x483874F: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==10789== by 0x109787: tab_binaire (automate.c:140)
==10789== by 0x10983F: main (automate.c:161)
==10789==
==10789== Invalid write of size 1
==10789== at 0x483B94F: strcat (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==10789== by 0x1097C3: tab_binaire (automate.c:145)
==10789== by 0x10983F: main (automate.c:161)
==10789== Address 0x4a54ae8 is 4 bytes after a block of size 4 alloc'd
==10789== at 0x483874F: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==10789== by 0x109787: tab_binaire (automate.c:140)
==10789== by 0x10983F: main (automate.c:161)
Here is my main()...
int main(int argc, char const argv[]) {
char binaire = tab_binaire(12);
printf("%s\n", binaire );
liberer_binaire(binaire);
return 0;
}
Upvotes: 1
Views: 79
Reputation: 785
Try the following adjustments (You had some problems in your code):
unsigned int_to_int(unsigned k);
char* tab_binaire(int nb);
unsigned int_to_int(unsigned k) {
if (k == 0) return 0;
if (k == 1) return 1; /* optional */
return (k % 2) + 10 * int_to_int(k / 2);
}
char* tab_binaire(int nb){
char* str = (char*) malloc(sizeof(char) * 9);
sprintf(str, "%d", int_to_int(nb));
int zeroManquant = 8 - strlen(str);
char* res= (char*) malloc(9 * sizeof(char));
int i;
for (i = 0; i < zeroManquant; i++) {
res[i]='0';
}
res[i] = '\0';
strcat(res,str);
free(str);
str=NULL;
return res;
}
int main(int argc, char const argv[]) {
char *binaire = tab_binaire(12);
printf("%s\n", binaire );
free(binaire);
//liberer_binaire(binaire);
return 0;
}
The valgrind errors above means that you're trying to access memory (read or write) which is out of the area allocated for the variables.
After filling the start of the string res
by '0'
using the for
loop, you needed to put the null character '\0'
at the end so the strcat
copy the second string from this position.
At this point:
char binaire = tab_binaire(12);
You were assigning a pointer to char so you had types conflict, it's simply fixed as follows:
char *binaire = tab_binaire(12);
Upvotes: 1