trouble with getelementptr

Hello,
I have next code:
;begin
; ModuleID = ‘sample.lz’
@.str1 = internal constant [20 x i8] c"\22hello, cruel world\22" ; <[20 x i8]> [#uses=1]
@.str4 = internal constant [9 x i8] c"\22hello, \22" ; <[9 x i8]
> [#uses=1]
@.str7 = internal constant [7 x i8] c"\22heya!\22" ; <[7 x i8]*> [#uses=1]

declare i32 @puts(i8*)

declare i8 @strcat(i8*, i8*)

define i32 @main() {
mainBlock:
%tmp2 = getelementptr [20 x i8]* @.str1, i64 0, i64 0 ; <i8*> [#uses=1]
%tmp3 = call i32 @puts( i8* %tmp2 ) ; [#uses=0]
%tmp5 = getelementptr [9 x i8]* @.str4, i64 0, i64 0 ; <i8*> [#uses=1]
%tmp6 = call i32 @puts( i8* %tmp5 ) ; [#uses=0]
%tmp8 = getelementptr [7 x i8]* @.str7, i64 0, i64 0 ; <i8*> [#uses=1]
%tmp9 = call i32 @puts( i8* %tmp8 ) ; [#uses=0]
ret i32 0
}

;end

llvm-as test.ll -o test.ll.bc
llc test.ll.bc -o test.ll.s
gcc test.ll.s -o test.ll.native

After running a see next:

whiter4bbit@LiZa /home/whiter4bbit/programming
/study/yapis/liza $ ./test.ll.native
“hello, cruel world”"hello, "“heya!”
"hello, "“heya!”
“heya!”

It isn’t a target result:) target is:

whiter4bbit@LiZa /home/whiter4bbit/programming/study/yapis/liza $ ./test.ll.native
“hello, cruel world”
"hello, "
“heya!”

what is the trouble here?
I think about freeing memory after using getelementptr, is it right?(and if it right can anybody answer ‘how?’)
Help me please…

Best regards, Zalunin Pavel

Hello,
  I have next code:
;begin
; ModuleID = 'sample.lz'
@.str1 = internal constant [20 x i8] c"\22hello, cruel world\22" ; <[20 x i8]*> [#uses=1]
@.str4 = internal constant [9 x i8] c"\22hello, \22" ; <[9 x i8]*> [#uses=1]
@.str7 = internal constant [7 x i8] c"\22heya!\22" ; <[7 x i8]*> [#uses=1]

declare i32 @puts(i8*)

puts expects strings to be null-terminated. Compare output of llvm-gcc -emit-llvm -S -O0.

Hi Zalunin,

Try including a NULL. :slight_smile: The c"foo" notation does not implicitly add one for you. Besides that, your use of getelementptr looks correct.

   c"\22hello, cruel world\22\00"

As for freeing memory, don't free global strings. They're deliberately put in a read-only memory area initialized when the binary starts up. You can free memory that you malloc, using the instructions of the same name.

Nick

Zalunin Pavel wrote: