🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gcc.help
Article #41896

float *func() messes up memory??

#41896
From: Andre Schilling
Date: Mon, 12 Feb 1996 00:00
30 lines
1774 bytes
Writing a function that returns a pointer to an array of real numbers I always get the proper pointer but - for some unknown reason - the values in the array pointed to are incorrect. Examining the function itself neither the pointervalue nor the actual numbers in the array are wrong. But trying to call them after they have been returned from the function always results in some kind of random numbers in the array. Important: the pointer returned by the function and the pointer received by the calling function are the same. Only the values in the array have changed which has given me some thought for the past days. Does anyone know the reason for that??
Here s the sample program:

int *DomainSize(void)
{
int *PDomainS ;
int i,DomainS[5] ;

 PDomainS = DomainS ;

 for (i=0;i<5;i++) {
  DomainS[i] = i+11 ;
 }
 return(PDomainS) ;
}


void main(void)
{
int *PG ;    
int i ;

 PG = DomainSize() ;
 for (i=0;i<5;i++)
  printf("%i \n",*(PG+i)) ;
}

the pointer PDomainS in DomainSize() points to the first element in DomainS[]. This value is passed to PG (in main) correctly. But printing the values of *(PG+i) results in arbitrary numbers. Once you ve got the right pointer you should find the proper numbers in the memory, shouldn t you??

Andre 
Technical Details
Message-ID: <4fnblm$b30@news.cs.tu-berlin.de>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 11318c,6915249633026d70,start
X-Google-Attributes: gid11318c,public
From: schilling@hpbio1.Biologie.HU-Berlin.de (Andre Schilling)
Subject: float *func() messes up memory??
Date: 1996/02/12
Message-ID: <4fnblm$b30@news.cs.tu-berlin.de>
X-Deja-AN: 139016567
distribution: world
content-type: text/plain; charset=iso-8859-1
organization: Technical University of Berlin, Germany
mime-version: 1.0
newsgroups: gnu.gcc.help