ftp.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2011/06/04/09:47:19

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: <isdadm$amb$1@news.albasani.net>
References: <isdadm$amb$1 AT news DOT albasani DOT net>
Date: Sat, 4 Jun 2011 16:20:52 +0300
Message-ID: <BANLkTinFMnrih3Zr7k=ED6+Mq6cCVjU3cA@mail.gmail.com>
Subject: Re: Different memory allocations were to the same location
From: Ozkan Sezer <sezeroz AT gmail DOT com>
To: djgpp AT delorie DOT com
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

On Sat, Jun 4, 2011 at 4:02 PM, Mok-Kong Shen <mok-kong DOT shen AT t-online DOT de> 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 <stdio.h>
> #include <stdlib.h>
>
> 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.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019