http://llvm.org/bugs/show_bug.cgi?id=21596

            Bug ID: 21596
           Summary: Pointer to an address returned from function does not
                    correlate to pointer to address in function. This
                    causes access to different place in memory, than was
                    expected.
           Product: clang
           Version: 3.5
          Hardware: Macintosh
                OS: MacOS X
            Status: NEW
          Severity: release blocker
          Priority: P
         Component: -New Bugs
          Assignee: [email protected]
          Reporter: [email protected]
                CC: [email protected]
    Classification: Unclassified

Version of llvm:
Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn)
Code that shows the error:
#include <stdio.h>
#include <stdlib.h>

int ** create_2d_array (int size) {
    int ** array = (int **)malloc(sizeof(int)*size);
    printf("Address: 0x%llx\n", (long long) &array);
    for (int i = 0; i < size; i++) {
        array[i] = (int *)malloc(sizeof(int)*size);
        for (int j = 0; j < size; j++) {
            array[i][j] = 100;
            printf("%5d ", array[i][j]);
        }
        printf("\n");
    }
    return array;
}
void print_2d_array(int ** array, int size) {

    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            printf("%5d ", array[i][j]);
        }
        printf("\n");
    }
}
int main(int argc, const char * argv[]) {
    int ** array = create_2d_array(3);
    printf("Address: 0x%llx\n", (long long) &array);
    print_2d_array(array, 3);
    return 0;
}

Expected output is:

Address: something
  100   100   100 
  100   100   100 
  100   100   100 
Address: something
  100   100   100 
  100   100   100 
  100   100   100 

I've got:

Address: 0x7fff5fbff760
  100   100   100 
  100   100   100 
  100   100   100 
Address: 0x7fff5fbff788
1070432     1   100 
  100   100   100 
  100   100   100

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
LLVMbugs mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/llvmbugs

Reply via email to