开发者

Why am I getting this error in C?

I have been getting a strange error when I use gcc to compile my c code.

This is my error: http://pastebin.com/dN4xXbQZ

This is my code:

// MemroyAllocationTester.c
// 2.20.11

// calloc() function allocates a group of objects.
// rather than malloc() which allocates a group of bytes.

#include <stdio.h>
#include <stdlib.h> // for calloc and free

main()
{
    unsigned num;
    int *pointer;

    printf("Enter the number of type int to allocate: ");
    scanf("%d", &num);

    pointer = (int*)calloc(num, sizeof(int));

    //if (pointer == NULL)
    // 开发者_JS百科 puts("Memory allocation failed.");
    //else
    //  puts("Memory allocation was successful.");

    return(0);
}


Your problem is that your source file is Unicode (UTF-16), but your compiler is expecting ASCII (or UTF-8). You need to save your source file as ASCII (or UTF-8).

You could try compiling with gcc -finput-charset=UTF-16 but I suspect that won't work because then it might try to interpret the header files as UTF-16 also, which they aren't.


Your program has a unicode byte order mark (0xFEFF) at the start of the file. I expect you don't want that. Get rid of it and compile boring old fashioned plaintext. Likely your file is UTF-16 for some reason. It looks like your compiler can't handle that - change it to UTF-8 or ASCII.


Don't think this has to do with calloc, you probably managed to have some non-ascii characters in your sourcefile.

The only thing that I see you shouldn't do in C is to cast the return from calloc or malloc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜