X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f X-Recipient: djgpp AT delorie DOT com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type:content-transfer-encoding; bh=wynd7LheVJnXZskLptdi/zSDQ1OMf48k7nYqSZygDOU=; b=oK7i9j+GJ/3WiQKdwpDELHT4MdwlgykWAI70swYiRFbik93T43A8KpetUJWA+PJLiZ SLuwHkDRTLQWdpmh2kGEibuvV+KFg+yitdt0+jhT0LR0XpmJ2qHqaCAYYwG1S357+QRO C2WG3aSUXpun9gPOTXMhYLYpWcO6UiZfqcx2U= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=sxK8EDSdLt7KIpQ0n4IL+obzXRdAoKldeBydwxoDsgsm1BEmZm/I/1+q0KpNauN0vo zD5ah4+6JjSXV2+I8urH3Zg0BdGFmmQFzhxqFA494sKV17a8l4qMkZHWAr4mdSNQLrD5 nDJe603DSwxqGiHenXgOwj6ut5d5b+M+w8tsE= MIME-Version: 1.0 In-Reply-To: References: Date: Sat, 4 Jun 2011 16:20:52 +0300 Message-ID: Subject: Re: Different memory allocations were to the same location From: Ozkan Sezer To: djgpp AT delorie DOT com Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id p54DlHWT023829 Reply-To: djgpp AT delorie DOT com Errors-To: nobody AT delorie DOT com X-Mailing-List: djgpp AT delorie DOT com X-Unsubscribes-To: listserv AT delorie DOT com Precedence: bulk On Sat, Jun 4, 2011 at 4:02 PM, Mok-Kong Shen wrote: > I wrote a function to allocate memory to arrays of integers. But > it turns out that the the momory allocated was always at the same > location. What had I done improperly and how could I get around > the problem? (I use version 2 of the compiler.) > > Thanks in advance. > > M. K. Shen > ------------------------------------------------------------------- > > #include > #include > > void generateintvec(int *uh, int len) > { free(uh); >  uh=calloc(len,sizeof(int)); >  if (uh==NULL) printf("memmory allocation unsuccessful !!!\n"); > } > This must change into something like: void generateintvec(int **uh, int len) { free(*uh); *uh=calloc(len,sizeof(int)); if (*uh==NULL) printf("memmory allocation unsuccessful !!!\n"); } > int *vec1, *vec2; > > int main() > { generateintvec(vec1,30); >  generateintvec(vec2,30); ... And these calls must change accordingly to: { generateintvec(&vec1,30); generateintvec(&vec2,30); >  if (vec1==vec2) printf("allocations were to the same location !!!\n"); >  return(0); > } > Hope these help. -- O.S.