Nekorektni ukonceni programu (dynamicke nasobeni matic) – C / C++ – Fórum – Programujte.com
 x   TIP: Přetáhni ikonu na hlavní panel pro připnutí webu

Nekorektni ukonceni programu (dynamicke nasobeni matic) – C / C++ – Fórum – Programujte.comNekorektni ukonceni programu (dynamicke nasobeni matic) – C / C++ – Fórum – Programujte.com

 

petrla0
Duch
3. 12. 2010   #1
-
0
-

Po spuštení programu program normálně projede. Ale při opakování cyklu vyhodí windows okno že program přestal pracovat (to standartní windowsácké) .
Program




#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char **argv)
{
/*Deklarace promených a nastaveni zakladnich aspektu pro chod programu*/
int Opak,r,s, velA_r,velAB_sr,velB_s;
int i,j,k;
unsigned int *MatA,*MatB,*MatC ,*zacA,*zacB,*zacC;

char Program;

Program=Opak=k=0;

/*Hlavni program, a jeho opakovani dokud neni zadana nula*/
/*cyklus opakovani, tak aby uzivatel nemusel pri kazdem novem nasobeni zapinat program*/

while(1){


srand((unsigned int) time(NULL));
printf("<----------Nasobeni Matic---------->\n");
printf("Pro ukonceni programu zadejte 0 \n\n");
printf("Zadej pocet radku matice A: "); scanf("%d",&velA_r);
if(velA_r==0)return 0;
printf("Zadej pocet sloupcu matice A==pocet radku matice B: ");scanf("%d",&velAB_sr);
printf("Pocet radku matice B: %d \n ",velAB_sr);
printf("Zadej pocet sloupcu matice B: ");scanf("%d",&velB_s);

MatA = (int*) malloc(velA_r *velAB_sr*sizeof(int));
MatB = (int*) malloc(velB_s *velAB_sr*sizeof(int));
MatC = (int*) malloc(velB_s *velA_r *sizeof(int));
zacA = MatA;
zacB = MatB;
zacC = MatC;
if (MatA == NULL||MatB == NULL||MatC == NULL)
{
printf("Malo pameti mas hochu!\n");
return 0;
}


printf("<-------------------------------->\nMatice A:\n");
for(r=0;r<velA_r;r++){
for(s=0;s<velAB_sr;s++){
*MatA=rand()%10;
printf("%d ",*MatA);
MatA++;
}
printf("\n");
}
srand((unsigned int) time(NULL));
printf("<-------------------------------->\nMatice B:\n");
for(r=0;r<velAB_sr;r++){
for(s=0;s<velB_s;s++){
*MatB=rand()%10;
printf("%d ",*MatB);
MatB++;
}
printf("\n");
}
printf("<-------------------------------->\nPronasobena Matice A s Matici B:\n");
/* vynulování matice C*/

for(r=0;r<velA_r;r++){
for(s=0;s<velB_s;s++){
*MatC=0;
MatC++;
}
}
/*Násobení matic vysledna matice je MatC*/
MatA=zacA;
MatB=zacB;
MatC=zacC;
k=velA_r*velB_s;
for(r=0;r<=k;r++){

MatA+=(velAB_sr*(r/velB_s));
MatB+=(r%velB_s);

for(s=0;s<velAB_sr;s++){
*MatC+=(*MatA)*(*MatB);
MatA++;
MatB+=velB_s;

}
MatC++;
MatA=zacA;
MatB=zacB;
}


/*vytiskne pronasobenou matici C*/
MatC=zacC;
for(r=0;r<velA_r;r++){
for(s=0;s<velB_s;s++){
printf("%3d ",*MatC);
MatC++;
}
printf("\n");
}
_getch();
free(MatA);
free(MatB);
free(MatC);
free(zacA);
free(zacB);
free(zacC);
}
return 0;



}

Výpis z debugu programu:
Program Received signal SIGTRAP

Single stepping until exit from function ntdll!TpWaitForAlpcCompletion, \nwhich has no line number information.
Continuing...
Single stepping until exit from function ntdll!RtlGetDaclSecurityDescriptor, \nwhich has no line number information.
Continuing...
Single stepping until exit from function ntdll!RtlCreateUserStack, \nwhich has no line number information.
Continuing...
Single stepping until exit from function ntdll!AlpcMaxAllowedMessageLength, \nwhich has no line number information.
Continuing...
Single stepping until exit from function ntdll!LdrWx86FormatVirtualImage, \nwhich has no line number information.
Continuing...
Single stepping until exit from function ntdll!AlpcMaxAllowedMessageLength, \nwhich has no line number information.
Continuing...
Single stepping until exit from function ntdll!LdrQueryProcessModuleInformation, \nwhich has no line number information.
Continuing...
Single stepping until exit from function ntdll!AlpcMaxAllowedMessageLength, \nwhich has no line number information.
Continuing...
Single stepping until exit from function ntdll!RtlGetLongestNtPathLength, \nwhich has no line number information.
Continuing...
Single stepping until exit from function wcstoul, \nwhich has no line number information.
Continuing...
Single stepping until exit from function ntdll!RtlInitBarrier, \nwhich has no line number information.
Continuing...
Single stepping until exit from function wcstoul, \nwhich has no line number information.
Continuing...
Single stepping until exit from function ntdll!EtwUnregisterTraceGuids, \nwhich has no line number information.
Continuing...
Single stepping until exit from function wcstoul, \nwhich has no line number information.
Continuing...
Single stepping until exit from function ntdll!RtlRemovePrivileges, \nwhich has no line number information.
Continuing...
Single stepping until exit from function wcstoul, \nwhich has no line number information.
Continuing...
Single stepping until exit from function ntdll!RtlInitBarrier, \nwhich has no line number information.
Continuing...
Single stepping until exit from function ntdll!AlpcMaxAllowedMessageLength, \nwhich has no line number information.
Continuing...
Single stepping until exit from function ntdll!RtlRemovePrivileges, \nwhich has no line number information.
Continuing...
Single stepping until exit from function ntdll!RtlInitBarrier, \nwhich has no line number information.
Continuing...
Single stepping until exit from function ntdll!EtwUnregisterTraceGuids, \nwhich has no line number information.
Continuing...
Single stepping until exit from function ntdll!RtlInitBarrier, \nwhich has no line number information.
Continuing...
Single stepping until exit from function ntdll!RtlRemovePrivileges, \nwhich has no line number information.
Continuing...
Single stepping until exit from function ntdll!RtlInitBarrier, \nwhich has no line number information.
Continuing...

Jeslti s tím někdo emá zkušenost čím by to mohlo být ?, bych bych velmi rád. Nenní důležité program rozchodit, spíše se poučit z chyby.
program v příloze je špatný !!!!

Nahlásit jako SPAM
IP: 213.192.4.–
KIIV
~ Moderátor
+43
God of flame
3. 12. 2010   #2
-
0
-

EDIT: tak tady pro zmenu uvolnujes stejnou pamet aspon 2x


pravidlo: jeden malloc => jeden free

Nahlásit jako SPAM
IP: 94.142.234.–
Program vždy dělá to co naprogramujete, ne to co chcete...
KIIV
~ Moderátor
+43
God of flame
3. 12. 2010   #3
-
0
-



==18154== Memcheck, a memory error detector
==18154== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==18154== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
==18154== Command: ./a.out
==18154==
<----------Nasobeni Matic---------->
Pro ukonceni programu zadejte 0

Zadej pocet radku matice A: 10
Zadej pocet sloupcu matice A==pocet radku matice B: 10
Pocet radku matice B: 10
Zadej pocet sloupcu matice B: 10
<-------------------------------->
Matice A:
6 5 5 1 6 8 9 7 9 7
3 8 2 9 7 4 5 4 4 5
3 5 7 8 8 0 5 8 7 9
5 6 4 0 9 1 1 9 0 0
8 3 0 2 4 9 7 9 6 1
6 9 8 6 9 8 8 5 9 8
6 4 4 1 6 5 4 7 6 4
7 5 0 9 9 6 9 6 6 7
9 4 6 0 2 8 8 1 5 7
9 1 3 5 4 0 0 8 7 7
<-------------------------------->
Matice B:
2 4 3 7 1 8 4 8 1 0
1 6 8 1 6 9 3 1 4 5
6 9 6 6 0 7 5 9 4 6
0 8 1 5 8 2 3 4 2 4
5 4 0 5 7 8 4 1 0 9
8 8 0 6 5 2 5 2 4 1
8 4 0 1 0 8 5 3 2 0
0 9 6 0 5 3 1 9 6 1
0 4 9 1 3 6 5 8 8 9
0 9 6 2 0 6 2 8 1 6
<-------------------------------->
Pronasobena Matice A s Matici B:
==18154== Invalid read of size 4
==18154== at 0x400AC7: main (test.c:144)
==18154== Address 0x518a570 is 0 bytes after a block of size 400 alloc'd
==18154== at 0x4C244E8: malloc (vg_replace_malloc.c:236)
==18154== by 0x4008A0: main (test.c:44)
==18154==
==18154== Invalid read of size 4
==18154== at 0x400ACD: main (test.c:144)
==18154== Address 0x518a1d0 is 0 bytes after a block of size 400 alloc'd
==18154== at 0x4C244E8: malloc (vg_replace_malloc.c:236)
==18154== by 0x40086A: main (test.c:40)
==18154==
==18154== Invalid write of size 4
==18154== at 0x400ADE: main (test.c:144)
==18154== Address 0x518a570 is 0 bytes after a block of size 400 alloc'd
==18154== at 0x4C244E8: malloc (vg_replace_malloc.c:236)
==18154== by 0x4008A0: main (test.c:44)
==18154==
213 393 254 192 188 383 242 342 219 251
133 327 184 164 224 298 180 224 148 225
133 402 264 178 214 358 204 347 190 296
101 221 141 117 154 231 113 177 105 145
167 296 164 158 178 270 184 255 177 137
242 483 303 255 263 466 291 383 244 340
142 295 195 153 158 285 173 263 162 192
184 391 202 209 250 371 231 303 178 250
196 310 188 188 107 321 206 277 150 170
57 288 211 148 144 252 142 308 146 192
==18154== Invalid free() / delete / delete[]
==18154== at 0x4C240FD: free (vg_replace_malloc.c:366)
==18154== by 0x400BA1: main (test.c:189)
==18154== Address 0x518a570 is 0 bytes after a block of size 400 alloc'd
==18154== at 0x4C244E8: malloc (vg_replace_malloc.c:236)
==18154== by 0x4008A0: main (test.c:44)
==18154==
==18154== Invalid free() / delete / delete[]
==18154== at 0x4C240FD: free (vg_replace_malloc.c:366)
==18154== by 0x400BAD: main (test.c:191)
==18154== Address 0x518a040 is 0 bytes inside a block of size 400 free'd
==18154== at 0x4C240FD: free (vg_replace_malloc.c:366)
==18154== by 0x400B89: main (test.c:185)
==18154==
==18154== Invalid free() / delete / delete[]
==18154== at 0x4C240FD: free (vg_replace_malloc.c:366)
==18154== by 0x400BB9: main (test.c:193)
==18154== Address 0x518a210 is 0 bytes inside a block of size 400 free'd
==18154== at 0x4C240FD: free (vg_replace_malloc.c:366)
==18154== by 0x400B95: main (test.c:187)
==18154==
<----------Nasobeni Matic---------->
Pro ukonceni programu zadejte 0

Zadej pocet radku matice A: 0
==18154==
==18154== HEAP SUMMARY:
==18154== in use at exit: 0 bytes in 0 blocks
==18154== total heap usage: 3 allocs, 6 frees, 1,200 bytes allocated
==18154==
==18154== All heap blocks were freed -- no leaks are possible
==18154==
==18154== For counts of detected and suppressed errors, rerun with: -v
==18154== ERROR SUMMARY: 33 errors from 6 contexts (suppressed: 4 from 4)

Nahlásit jako SPAM
IP: 94.142.234.–
Program vždy dělá to co naprogramujete, ne to co chcete...
KIIV
~ Moderátor
+43
God of flame
3. 12. 2010   #4
-
0
-

radek 144 je toto: *MatC+=(*MatA)*(*MatB);

edit: ale chyba je toto: for(r=0;r<=k;r++){

Nahlásit jako SPAM
IP: 94.142.234.–
Program vždy dělá to co naprogramujete, ne to co chcete...
Zjistit počet nových příspěvků

Přidej příspěvek

Toto téma je starší jak čtvrt roku – přidej svůj příspěvek jen tehdy, máš-li k tématu opravdu co říct!

Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku

×Vložení zdrojáku

×Vložení obrázku

Vložit URL obrázku Vybrat obrázek na disku
Vlož URL adresu obrázku:
Klikni a vyber obrázek z počítače:

×Vložení videa

Aktuálně jsou podporována videa ze serverů YouTube, Vimeo a Dailymotion.
×
 
Podporujeme Gravatara.
Zadej URL adresu Avatara (40 x 40 px) nebo emailovou adresu pro použití Gravatara.
Email nikam neukládáme, po získání Gravatara je zahozen.
-
Pravidla pro psaní příspěvků, používej diakritiku. ENTER pro nový odstavec, SHIFT + ENTER pro nový řádek.
Sledovat nové příspěvky (pouze pro přihlášené)
Sleduj vlákno a v případě přidání nového příspěvku o tom budeš vědět mezi prvními.
Reaguješ na příspěvek:

Uživatelé prohlížející si toto vlákno

Uživatelé on-line: 0 registrovaných, 14 hostů

Podobná vlákna

Dynamicke nasobeni matic — založil cecilconrad

Nasobeni matic — založil dreIx

Násobení matic — založil Redwizara

Násobení dvou matic — založil Zke

Moderátoři diskuze

 

Hostujeme u Českého hostingu       ISSN 1801-1586       ⇡ Nahoru Webtea.cz logo © 20032024 Programujte.com
Zasadilo a pěstuje Webtea.cz, šéfredaktor Lukáš Churý