Logo

dev-resources.site

for different kinds of informations.

How many asterisks can be put?

Published at
7/29/2022
Categories
c
clang
gcc
Author
kaityo256
Categories
3 categories in total
c
open
clang
open
gcc
open
Author
9 person written this
kaityo256
open
How many asterisks can be put?

In C and C++, the asterisk (*) is used to declare or dereference a pointer variable.

By the way, did you know that you can prefix a function call with as many asterisks as you like?

Here is the simple and valid C program.

#include <stdio.h>

int main(){
  printf("Hello world!\n");
}
Enter fullscreen mode Exit fullscreen mode

Here is a valid C program too.

#include <stdio.h>

int main(){
  (*printf)("Hello world!\n");
}
Enter fullscreen mode Exit fullscreen mode

Here is also a valid one.

#include <stdio.h>

int main(){
  (******printf)("Hello world!\n");
}
Enter fullscreen mode Exit fullscreen mode

This program with 100 asterisks is also a perfect C program.

#include <stdio.h>

int main(){
  (
   **********
   **********
   **********
   **********
   **********
   **********
   **********
   **********
   **********
   **********
   printf)("Hello world!\n");
}
Enter fullscreen mode Exit fullscreen mode

You see this and you're wondering how many asterisks you can add? Let's find out.

This is a Ruby script that prepends the specified number of asterisks to the printf function.

def check(n)
  s = "*"*n
  open("test.c","w") do |f|
  f.puts <<EOS
#include <stdio.h>
int main(){
(#{s}printf)("Hello World!\\n");
}
EOS
end
  return system("clang test.c")
end

check(ARGV[0].to_i)
Enter fullscreen mode Exit fullscreen mode

You can use like this.

ruby check.rb 10
Enter fullscreen mode Exit fullscreen mode

It will generate a following source codes.

#include <stdio.h>
int main(){
(**********printf)("Hello World!\n");
}
Enter fullscreen mode Exit fullscreen mode

Let's try it with a thousand asterisks.

ruby check.rb 1000
Enter fullscreen mode Exit fullscreen mode

No problem. Then try with ten thousand.

ruby check.rb 10000
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
(snip)
clang-12: error: unable to execute command: Segmentation fault (core dumped)
clang-12: error: clang frontend command failed due to signal (use -v to see invocation)
Enter fullscreen mode Exit fullscreen mode

Clang died with SIGSEGV.

Clang was ok with 1000 asterisks and died with 10,000 asterisks. So there must be a limit somewhere in there. Let's check it out with a binary search.

def check(n)
  s = "*"*n
  open("test.c","w") do |f|
  f.puts <<EOS
#include <stdio.h>
int main(){
(#{s}printf)("Hello World!\\n");
}
EOS
end
  if system("clang test.c 2> /dev/null")
    puts "#{n} OK"
    false
  else
    puts "#{n} NG"
    true
  end
end

(1000..10000).bsearch do |n|
  check(n)
end
Enter fullscreen mode Exit fullscreen mode

Here is the execution result.

$ ruby search.rb
5500 NG
3250 NG
2125 NG
1562 OK
1844 NG
1703 NG
1633 OK
1668 NG
1651 OK
1660 NG
1656 OK
1658 NG
1657 OK
Enter fullscreen mode Exit fullscreen mode

Now, we have new knowledge. We can put up to 1657 asterisks (environment dependent).

By the way, let's try it with GCC. Here is a check script.

def check(n)
  s = "*"*n
  open("test.c","w") do |f|
  f.puts <<EOS
#include <stdio.h>
int main(){
(#{s}printf)("Hello World!\\n");
}
EOS
end
  return system("gcc test.c")
end

check(ARGV[0].to_i)
Enter fullscreen mode Exit fullscreen mode

Let's try from 1000.

ruby check_gcc.rb 1000
Enter fullscreen mode Exit fullscreen mode

No problem. Next, 10,000.

ruby check_gcc.rb 10000
Enter fullscreen mode Exit fullscreen mode

Well, how about 100,000?

ruby check_gcc.rb 100000
Enter fullscreen mode Exit fullscreen mode

Seriously? Then, 1,000,000.

$ ruby check_gcc.rb 1000000
gcc: internal compiler error: Segmentation fault signal terminated program cc1
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://bugzilla.redhat.com/bugzilla> for instructions.
Enter fullscreen mode Exit fullscreen mode

Oops, gcc dided with a million of asterisks.

So, If you want to use ten thousand asterisks, use GCC.

clang Article's
27 articles in total
Favicon
Tester c'est tricher, compiler c'est douter
Favicon
Pointers in Modern C
Favicon
Creating a Robust Logging System in C
Favicon
The unspoken question: "Why do pointers exist?"
Favicon
Pointers : what are they pointing to?
Favicon
Setting up linters in Gitlab CI for C++ and Groovy / Jenkins code
Favicon
SORRY, RUST & DEVS !
Favicon
Cycle don&amp;#39;t want to write to the array
Favicon
cách xử lý lỗi không chạy được code C++ trong Code::Blocks "Tried to run compiler enumerable `C:\MinGW/bin/gcc.exe` but failed"
Favicon
Building an XDP eBPF Program with C and Golang: A Step-by-Step Guide
Favicon
Should I Learn Math First, Then Rust or C, or Can I Learn Mathematics and Rust, C Simultaneously?
Favicon
The C Programming Language by Brian W. Kernighan & Dennis M. Ritchie.
Favicon
The C Programming Language by Brian W. Kernighan & Dennis M. Ritchie.
Favicon
Dynamic Linker Hijacking Experiments - Evasive Techniques (Part 1)
Favicon
How To Include ‘bits/stdc++.h’ Header File With Clang Compiler on macOS
Favicon
How many asterisks can be put?
Favicon
A common pitfall when using sizeof() with pointers
Favicon
Platform detection in C&C++
Favicon
Lint Lint Boom
Favicon
Behind C++ Lambda Functions
Favicon
C++ Levitation: Looking for contributors
Favicon
Type qualifier: register, volatile and restrict - C Programming
Favicon
Configuring Oni as a C / C++ IDE on Ubuntu 18.04
Favicon
Use `bool` in C program
Favicon
Clang vs GCC
Favicon
Working on Object Lifetime Analysis for C++
Favicon
Writing safer C with Clang address sanitizer

Featured ones: