Bloomberg LP Interview Question
Financial Software DevelopersCountry: United States
Interview Type: In-Person
Also worth noting you should get at least a warning in most compilers for implementation 2 for the conversion from a const to non const value.
Decided to verify:
populate.cpp: In function 'void populate(char**)':
populate.cpp:23:11: warning: deprecated conversion from string constant to 'char *' [-Wwrite-strings]
The correct answer is :
error is coming at the point when u are trying to free the memory.
in the 1st implementation, memory is allocated dynamically using malloc and so u can free that using free().
where as in the 2nd case memory is allocated on the stack. and u cannot use free() to deallocate the memory on stack.
free() should only be called on memory you allocated (or on NULL).
In 2nd case memory is not allocated on the stack. It is allocated on space (generally in the read-only static data section) for the string literals
First implementation is correct. There is nothing wrong in it.
But for the second one compilation error as "Prasad" returns a const char* but *str is just char*.
It doesn't give any compilation error. But it throws an runtime error while trying to execute this statement.
free(s)
Free expects a void *
The correct answer is :
error is coming at the point when u are trying to free the memory.
in the 1st implementation, memory is allocated dynamically using malloc and so u can free that using free().
where as in the 2nd case memory is allocated on the stack. and u cannot use free() to deallocate the memory on stack.
free() should only be called on memory you allocated (or on NULL).
// In this it prints first string and now free the memory here it is given that how to free memory
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
void populate(char **s);
int main() {
char *s;
populate(&s);
printf("%s\n", s); // should print "Prasad"
s = NULL;
free(s);
printf("%s\n", s);
return 0;
}
void populate(char **str) {
// 1. The next two lines is one implementation
*str = (char *)malloc(sizeof(char) * 7);
strcpy(*str, "Prasad");
// 2. This line seperately is another implementation
*str = "Prasad";
}
Terrible style for C++. Better would be
void populate(char*& str)
The problem is with
*str = "Prasad";
"Prasad" refers to memory in the initialised part of the .data segment. free() should generate an error (but doesn't have to - behaviour is undefined) and the compiler should complain of a assignment of a const char [] to a char*.
*str is a pointer, a variable , why it cannot be changed to point to another const string ?
I changed "Prasad" to "ppp",but it said:
*** glibc detected *** ./a.out: munmap_chunk(): invalid pointer: 0x08048587 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x721a2)[0x9321a2]
/lib/i386-linux-gnu/libc.so.6(+0x723e8)[0x9323e8]
./a.out[0x8048461]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0x8d9113]
./a.out[0x80483a1]
======= Memory map: ========
001ff000-00200000 r-xp 00000000 00:00 0 [vdso]
0027d000-00299000 r-xp 00000000 08:01 6423482 /lib/i386-linux-gnu/libgcc_s.so.1
00299000-0029a000 r--p 0001b000 08:01 6423482 /lib/i386-linux-gnu/libgcc_s.so.1
0029a000-0029b000 rw-p 0001c000 08:01 6423482 /lib/i386-linux-gnu/libgcc_s.so.1
0044d000-0046b000 r-xp 00000000 08:01 6430185 /lib/i386-linux-gnu/ld-2.13.so
0046b000-0046c000 r--p 0001d000 08:01 6430185 /lib/i386-linux-gnu/ld-2.13.so
0046c000-0046d000 rw-p 0001e000 08:01 6430185 /lib/i386-linux-gnu/ld-2.13.so
008c0000-00a3c000 r-xp 00000000 08:01 6430166 /lib/i386-linux-gnu/libc-2.13.so
00a3c000-00a3e000 r--p 0017c000 08:01 6430166 /lib/i386-linux-gnu/libc-2.13.so
00a3e000-00a3f000 rw-p 0017e000 08:01 6430166 /lib/i386-linux-gnu/libc-2.13.so
00a3f000-00a42000 rw-p 00000000 00:00 0
08048000-08049000 r-xp 00000000 08:01 6030435 /home/xieying/interview/a.out
08049000-0804a000 r--p 00000000 08:01 6030435 /home/xieying/interview/a.out
0804a000-0804b000 rw-p 00001000 08:01 6030435 /home/xieying/interview/a.out
0a006000-0a027000 rw-p 00000000 00:00 0 [heap]
b777a000-b777b000 rw-p 00000000 00:00 0
b778a000-b778d000 rw-p 00000000 00:00 0
bfab2000-bfad3000 rw-p 00000000 00:00 0 [stack]
已放弃
There will be no compilation error in any of the implementation.
First implementation copies the data to the allocated memory. The second implementation causes a memory leak as str is allocated some memory but is over-written by some other address. There is no way to free the memory allocated by malloc.
1)There will be no compilation error in any of the implementation.
- RAM February 19, 20132) The first implementation there won't be any issues in memory allocation or free.
3)second implementation you can't free(S) because s is pointing to constant memory you can modify s to another pointer but you can't free the current memory allocated.