1

This very simple code runs fine when compiled with GNU, Intel, or PGI (which is really GNU, I think?). Under Cray, it never makes it to "debug6"; it fails in the "calloc" call, returning an "Illegal instruction" error. Anyone see a problem? EDIT: This is a run time error, not a compile time error.

FURTHER EDIT: If anyone wants another clue, malloc() works where calloc() does not.

AND ANOTHER EDIT: If you want yet another clue, I can calloc() up to 15 chars, no prob. It fails for 16 or more.

#include <stdio.h>
#include <stdlib.h>


int attempt_something() {

   char *needed_file_name = NULL;

   printf("debug2\n");
   needed_file_name = calloc(16, sizeof(char));

   printf("debug6\n");
   fflush(stdout);

   free(needed_file_name);

   return 0;

}



int main() {

   int rst_type;

   printf("debug1\n");
   rst_type = attempt_something();

}
bob.sacamento
  • 6,283
  • 10
  • 56
  • 115
  • 1
    Are you sure you are using the proper compiler? Or the proper compiler flags? If you are, than it could be just broken. It happens. See which instruction is emitted and report a bug. – Eugene Sh. May 25 '17 at 17:14
  • 2
    Fails during compilation, or compiles OK but fails at run-time ? – Paul R May 25 '17 at 17:16
  • 3
    It does not exactly sound like the compiler doesn't like it, this is a runtime error. At least try malloc() to compare notes. – Hans Passant May 25 '17 at 17:17
  • 1
    @PaulR "*it never makes it to "debug6"; it fails in the "calloc" call*" - sounds like runtime. – Eugene Sh. May 25 '17 at 17:18
  • It probably is not related to the *compiler* but to the C standard library (and the operating system, computer and environment). Use `errno` (e.g. thru `perror`) when `malloc` or `calloc` is failing. Also, compile with all warnings & debug info (`gcc -Wall -Wextra -g`) and use the debugger `gdb` – Basile Starynkevitch May 25 '17 at 17:19
  • @EugeneSh.: yes, I thought that, but the title says '*compiler* doesn't like "calloc"', which made me wonder... – Paul R May 25 '17 at 17:20
  • Were compiler warnings enabled and given? – Weather Vane May 25 '17 at 17:22
  • @BasileStarynkevitch If `calloc` implementation has an illegal instuction in it, it could mean that compiler (well, the linker) is not picking the correct library for the architecture. Or the library is broken. I don't think it will run far enough to analyze `errno`. – Eugene Sh. May 25 '17 at 17:22
  • What is `sizeof(char)`? Should be 1, but I seem to recall something being weird about Cray systems… –  May 25 '17 at 18:17
  • 1
    And, as an aside: What vintage of Cray systems are you working with? –  May 25 '17 at 18:18
  • 2
    @duskwuff: The C standard guarantees that `sizeof(char)` is 1. IIRC, the classic Cray computers had 8-bit `char`, but `short`, `int`, and `long` all took 64 bits of memory, and pointers to `char` and `void` had a different representation from other pointer types. – Dietrich Epp May 25 '17 at 18:26
  • 1
    @DietrichEpp The C standard guarantees that… but not all vintage C compilers were compliant with the standard. :) –  May 25 '17 at 18:27
  • What Cray compiler and what Cray system & machine ?? What exact compilation command ? What C library ? Could be some [XY problem](http://xyproblem.info) so a lot more details and motivations should be given by **improving the question** (so edit it); did you try to statically link your `libc` ? – Basile Starynkevitch May 25 '17 at 18:46
  • Anything at all to do with a null terminator? – Peter May 25 '17 at 19:09
  • @Peter Thanks for the thought, but I don't see how, since I can't even get to the point of populating the memory. – bob.sacamento May 25 '17 at 19:28
  • @duskwuff: Really? Can you give an example? I'm curious, since it seems like it would break so many things. – Dietrich Epp May 25 '17 at 19:33
  • if one uses malloc and then uses memset and one sets each byte 0, what happens? – Peter May 25 '17 at 19:35
  • @DietrichEpp I just mean in general — old compilers were often weird. I've heard a *rumor* that some old systems had `sizeof(char)=sizeof(int)`, but no confirmation. –  May 25 '17 at 19:36
  • @duskwuff: That's true, but in those cases `sizeof(int)` is 1. It happens on some modern DSP chips. – Dietrich Epp May 25 '17 at 19:37
  • @duskwuff: To put it another way, `sizeof` means "how large is this compared to `char`". – Dietrich Epp May 25 '17 at 19:40
  • @DietrichEpp Again… that's how it's _supposed_ to work. Whether all compiler implementations - especially really old ones! - behaved that way is a separate matter. –  May 25 '17 at 19:41
  • I would suggest to grab an instruction-level debugger and see what's going on under the hood. – Eugene Sh. May 25 '17 at 20:01

0 Answers0