Bug 13691 - malloc returns eroneous pointer
Summary: malloc returns eroneous pointer
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 3.3.1
: P1 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-01-14 23:55 UTC by Jan Kare Bording
Modified: 2005-07-23 22:49 UTC (History)
1 user (show)

See Also:
Host: x86_64-pc-linux-gnu
Target: x86_64-pc-linux-gnu
Build: x86_64-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jan Kare Bording 2004-01-14 23:55:51 UTC
~>cat test.c
#include <stdio.h>
main()
{
  int n;
  double **A;
  for (n=8000;n<100000;n*=2) {
    A = (double **)malloc(n*sizeof(double *));
    printf("%d %d \n",n,A);
  }
}

~>gcc -o test test.c -lm
test.c: In function `main':
test.c:9: warning: cast to pointer from integer of different size
~>test 
8000 5301488
16000 5365504
32000 -1789435888
64000 -1789177840
Comment 1 Andrew Pinski 2004-01-14 23:59:46 UTC
invalid as pointers on x86_64 are 64bit, not 32bit which are the sizeof of an int, you want to print 
out using %ld.
Comment 2 bording 2004-01-15 00:10:43 UTC
Subject: Re:  malloc returns eroneous pointer

Replacing %d with %ld did not change anything ! Besides, I'm not concerned
with the output - my real program craches when writing to the allocated 
memory...

double **A,**B;
A = (double **)malloc(16000*sizeof(double *));
B = (double **)malloc(32000*sizeof(double *));
A[3] = 1.234;  // works fine
B[3] = 1.234;  //  gives p0_23074:  p4_error: interrupt SIGSEGV: 11

Jan

pinskia at gcc dot gnu dot org wrote:

>------- Additional Comments From pinskia at gcc dot gnu dot org  2004-01-14 23:59 -------
>invalid as pointers on x86_64 are 64bit, not 32bit which are the sizeof of an int, you want to print 
>out using %ld.
>
>  
>

Comment 3 Andrew Pinski 2004-01-15 00:17:01 UTC
You cannot do this:
A = (double **)malloc(16000*sizeof(double *));
B = (double **)malloc(32000*sizeof(double *));
A[3] = 1.234;  // works fine
B[3] = 1.234;  //  gives p0_23074:  p4_error: interrupt SIGSEGV: 11

That will not compile anyways.  But if you change it to:
A[3][3] = 1.234;
this will compile but it will not work as you have not allocated what A[3] points to, go read a book 
about C memory and pointers, no I do not know of one.
Comment 4 bording 2004-01-15 00:26:57 UTC
Subject: Re:  malloc returns eroneous pointer

Sorry - I was careless - obviously it's a pointer. It should have been:

  double **A,**B;

  A = (double **)malloc(16000*sizeof(double *));
  B = (double **)malloc(32000*sizeof(double *));

  A[3] = (double *)malloc(1*sizeof(double));  // works fine
  B[3] = (double *)malloc(1*sizeof(double));  // crashes with a 
segmentation fault on x86_64

Jan



pinskia at gcc dot gnu dot org wrote:

>------- Additional Comments From pinskia at gcc dot gnu dot org  2004-01-15 00:17 -------
>You cannot do this:
>A = (double **)malloc(16000*sizeof(double *));
>B = (double **)malloc(32000*sizeof(double *));
>A[3] = 1.234;  // works fine
>B[3] = 1.234;  //  gives p0_23074:  p4_error: interrupt SIGSEGV: 11
>
>That will not compile anyways.  But if you change it to:
>A[3][3] = 1.234;
>this will compile but it will not work as you have not allocated what A[3] points to, go read a book 
>about C memory and pointers, no I do not know of one.
>
>  
>

Comment 5 Andrew Pinski 2004-01-15 00:29:02 UTC
Not a GCC bug, report it to glibc.
Comment 6 marcus 2004-01-15 06:40:36 UTC
You are also missing #include <stdlib.h> which might be the cause of the 
warning. 
 
listen to the compiler, it is your friend ;)