[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] malloc ter sig underligt



Anders Søndergaard <sslug@sslug> writes:

> [input tal]. Hvis jeg gør sådan her:

Er der væsentlige dele af koden du har udeladt?

> int *pointer,tal,hent;
>
> pointer = (int *) malloc(sizeof(int));

Her allokerer du plads til ét heltal.

> ...
> hent = find_numbers(pointer,tal); 

Jeg går ikke ud fra at find_numbers allokerer mere plads?

> while (hent>0) {
> printf("%i\n",pointer[hent-1]); /* tallene kommer fint ud */
> }

Derfor bruger du her noget plads du rent faktisk ikke har allokeret
for du har kun allokeret pointer[0].

> Jeg har prøvet mig frem, og fundet ud af at fejlen sker når de to mallocs:
> pointer = (int *) malloc(sizeof(int));
> pointerto = (int *) malloc(sizeof(int));
> står under hinanden. 

Sikkert fordi at pointer og pointerto bliver allokeret lige efter hinanden.

-- 
 Peter Makholm     |    Yes, you can fight it, but in the end the ultimate
 sslug@sslug |                           goal of life is to have fun
 http://hacking.dk |                                     -- Linus Torvalds


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 22:44 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *