Thread: Strange warnings

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Norway
    Posts
    11

    Strange warnings

    Hello. I am trying to implement a recursive merge sort, and I've just barely begun. I am getting some warnings:

    merge_sort.c:15: warning: ISO C90 forbids variable-size array 'left'
    merge_sort.c:15: warning: ISO C90 forbids mixed declarations and code

    Code:
    #include <stdio.h>
    #include <string.h>
    
    static void
    merge_sort(int *a, int n)
    {
        int mid;
        int *y;
    
        if (n > 1)
            mid = n / 2;
    
        y = a + mid;
    
        int left[mid];
        memcpy(left, a, mid);
    }
    
    int
    main(int argc, char *argv[])
    {
        int uo[] = {2, 65, 23, 65, 734, 23, 45, 23, 67};
        int n = sizeof uo / sizeof uo[0];
    
        merge_sort(uo, n);
    
        return 0;
    }
    I am using gcc 4.2.1 and I am compiling with -Wall and -pedantic.

    Any ideas?

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    You have to initialize mid with some value.
    You can't just say int mid; than an array of size mid.
    Especially if n is 1 or less.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Your compiler is telling you that Variable Length Arrays are not allowed. So the following is incorrect.
    Code:
    merge_sort(int *a, int n)
    {
        int mid;
    ...
        int left[mid];
    Array sizes must be compile time constants.

    Also you must declare all your variables before you can start using them. So the following is incorrect.

    Code:
        y = a + mid;
     
        int left[mid];

    Jim

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    GCC by default implements the C89/C90 standard (i.e. the one from 1989/1990), which did not allow VLAs. C99 and C11 do allow VLAs, however. All you have to do is tell GCC that you want to use that standard, like so:
    -std=c99
    There is preliminary support for C11 (and a flag for it), but it's still in the works, and you aren't using C11-specific features, so C99 is fine for your case.

    Note, there is also a bug that can cause a serious problem.

    mid is not initialized. If n is <= 1, you never set mid, and thus it has some arbitrary garbage value. That can cause problems by making y point to some strange place, problems with your VLA once you enable C99 (imagine mid is negative), and can also cause a seg fault in the memcpy, should mid be a very large number. Make sure it always has a good value before using it.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    With the version of the compiler the OP is using I wouldn't recommend trying to use VLA, they are listed as broken on the gcc C status page. With that version 4.2.1 I'd stick with C90.

    I would really recommend trying to update your compiler to a more recent version.

    Jim

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by jimblumberg View Post
    With the version of the compiler the OP is using I wouldn't recommend trying to use VLA, they are listed as broken on the gcc C status page. With that version 4.2.1 I'd stick with C90.
    Good to know. They still broken in 4.4, but fixed in 4.6.
    Quote Originally Posted by jimblumberg View Post
    I would really recommend trying to update your compiler to a more recent version.
    Yes, I agree.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    This:
    Code:
    memcpy(left, a, mid);
    ...only copies mid bytes (not whole integers) from address a to address left.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why i get these warnings..
    By transgalactic2 in forum C Programming
    Replies: 4
    Last Post: 03-25-2009, 10:31 AM
  2. for/while Warnings
    By reaperman in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2008, 04:14 PM
  3. GCC Warnings
    By 00Sven in forum C Programming
    Replies: 12
    Last Post: 05-03-2006, 04:40 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. what do these warnings mean
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 02-08-2002, 04:23 PM