Skip to content

Instantly share code, notes, and snippets.

@ear7h
Created March 17, 2019 00:32
Show Gist options
  • Save ear7h/7a8c80c59584dd840eaf7b28ebbf6367 to your computer and use it in GitHub Desktop.
Save ear7h/7a8c80c59584dd840eaf7b28ebbf6367 to your computer and use it in GitHub Desktop.
String literal allocation in C
#include <stdio.h>
char * mkstr() {
return "some data";
}
char * mkstr1() {
return "data some";
}
int main() {
char arr[] = "asdf";
//arr = mkstr();
char * str = mkstr();
mkstr1();
printf("%s %s %lu", str, arr, sizeof arr);
}
Apple LLVM version 10.0.0 (clang-1000.10.44.4)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
cc (Debian 8.3.0-2) 8.3.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
carr.o: file format elf64-x86-64
Contents of section .text:
0000 554889e5 488d0500 0000005d c3554889 UH..H......].UH.
0010 e5488d05 00000000 5dc35548 89e54883 .H......].UH..H.
0020 ec10c745 f3617364 66c645f7 00b80000 ...E.asdf.E.....
0030 0000e800 00000048 8945f8b8 00000000 .......H.E......
0040 e8000000 00488d55 f3488b45 f8b90500 .....H.U.H.E....
0050 00004889 c6488d3d 00000000 b8000000 ..H..H.=........
0060 00e80000 0000b800 000000c9 c3 .............
Contents of section .rodata:
0000 736f6d65 20646174 61006461 74612073 some data.data s
0010 6f6d6500 25732025 7320256c 7500 ome.%s %s %lu.
Contents of section .comment:
0000 00474343 3a202844 65626961 6e20382e .GCC: (Debian 8.
0010 332e302d 32292038 2e332e30 00 3.0-2) 8.3.0.
Contents of section .eh_frame:
0000 14000000 00000000 017a5200 01781001 .........zR..x..
0010 1b0c0708 90010000 1c000000 1c000000 ................
0020 00000000 0d000000 00410e10 8602430d .........A....C.
0030 06480c07 08000000 1c000000 3c000000 .H..........<...
0040 00000000 0d000000 00410e10 8602430d .........A....C.
0050 06480c07 08000000 1c000000 5c000000 .H..........\...
0060 00000000 53000000 00410e10 8602430d ....S....A....C.
0070 06024e0c 07080000 ..N.....
carr.o: file format Mach-O 64-bit x86-64
Contents of section __text:
0000 554889e5 488d056d 0000005d c30f1f00 UH..H..m...]....
0010 554889e5 488d0567 0000005d c30f1f00 UH..H..g...]....
0020 554889e5 4883ec20 8b055e00 00008945 UH..H.. ..^....E
0030 fb8a0d04 00000088 4dffe800 00000048 ........M......H
0040 8945f0e8 00000000 488d3d42 000000ba .E......H.=B....
0050 05000000 89d1488d 55fb488b 75f04889 ......H.U.H.u.H.
0060 45e8b000 e8000000 004531c0 8945e444 E........E1..E.D
0070 89c04883 c4205dc3 ..H.. ].
Contents of section __cstring:
0078 736f6d65 20646174 61006461 74612073 some data.data s
0088 6f6d6500 61736466 00257320 25732025 ome.asdf.%s %s %
0098 6c7500 lu.
Contents of section __compact_unwind:
00a0 00000000 00000000 0d000000 00000001 ................
00b0 00000000 00000000 00000000 00000000 ................
00c0 10000000 00000000 0d000000 00000001 ................
00d0 00000000 00000000 00000000 00000000 ................
00e0 20000000 00000000 58000000 00000001 .......X.......
00f0 00000000 00000000 00000000 00000000 ................
Contents of section __eh_frame:
0100 14000000 00000000 017a5200 01781001 .........zR..x..
0110 100c0708 90010000 24000000 1c000000 ........$.......
0120 e0feffff ffffffff 0d000000 00000000 ................
0130 00410e10 8602430d 06000000 00000000 .A....C.........
0140 24000000 44000000 c8feffff ffffffff $...D...........
0150 0d000000 00000000 00410e10 8602430d .........A....C.
0160 06000000 00000000 24000000 6c000000 ........$...l...
0170 b0feffff ffffffff 58000000 00000000 ........X.......
0180 00410e10 8602430d 06000000 00000000 .A....C.........
@ear7h
Copy link
Author

ear7h commented Mar 17, 2019

In response to the question, "where is the following string allocated, stack or heap?"

char arr[] = "asdf";

This so response leads one to think it's a static section of the binary.
https://stackoverflow.com/a/9970305

This the outputs from cc -c carr.c; objdump -s carr.o confirms this. But also note that cc optimizes(?) the literal in main. It can be allocated in the stack as it never escapes main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment