[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> wrote:

> Jeg er i gang med et program der kan forkorte en brøk.

Så kunne du nok have fordel af algoritmen gcd().


[...]

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

Her reserverer du plads til nøjagtig 1 heltal (int).

> hent = find_numbers(pointer,tal); 

Jeg formoder, at find_numbers() skriver "tal"s primfaktorer ind i det
array, som "pointer" peger på. Det array er som overfor nævnt af
størrelse 1, så med mindre "tal" er meget lille (og derved har få
faktorer), skriver du ned i hukommelse, du ikke ejer. Og så kan alt
ske.


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

Sandsynligvis skriver de to kald af find_numbers() ind i overlappende
hukommelsesområder fordi de to malloc() reserverer for lidt hukommelse.
Prøv med:

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

(det virker så sikkert mens der er under 100 faktorer)


Venlig hilsen,
Anders


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