[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
 

malloc ter sig underligt



Hej på listen.

Jeg er i gang med et program der kan forkorte en brøk.
Jeg har skrevet en funktion der finder alle tal som går op i 
[input tal]. Hvis jeg gør sådan her:

int *pointer,tal,hent;

pointer = (int *) malloc(sizeof(int));

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

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

virker det helt fint. (find_numbers retunerer størrelsen af array
"pointer", som indeholder alle tal som går op i "tal").

Hvis jeg så gør sådan her:


int *pointer,tal,hent;
int *pointerto,talto,hentto;
pointer = (int *) malloc(sizeof(int));
pointerto = (int *) malloc(sizeof(int));
...
hent = find_numbers(pointer,tal); 
hentto = find_numbers(pointerto,talto); 

while (hent>0) {
printf("%i\n",pointer[hent-1]); /* BOGUS! */
}
while (hentto>0) {
printf("%i\n",pointer[hentto-1]); /* BOGUS! */
}

altså så den finder to forskellige. Dér går det galt.
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. 

Jeg aner ikke hvad der sker, eller hvordan man retter
op på det.
Er der nogle der har en løsning på mit problem? Jeg har arbejdet på
det en halv dag
nu men jeg kan simpelthen ikke finde ud af det. :-) 


Med venlig hilsen
Anders Søndergaard


 
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] *