質問編集履歴

1

コードを編集し、valgrindを使ってデバッグした結果を追加しました

2020/04/26 10:58

投稿

aardvark
aardvark

スコア17

test CHANGED
File without changes
test CHANGED
@@ -345,3 +345,453 @@
345
345
  }コード
346
346
 
347
347
  ```
348
+
349
+
350
+
351
+ 編集:皆様、ありがとうございます。
352
+
353
+ コードを以下のように編集し、valgrindをランしてみたところ、以下のようなメッセージが出ました。
354
+
355
+ こちらはどのように解釈されるのでしょうか?
356
+
357
+ ```C
358
+
359
+ #include <stdio.h>
360
+
361
+ #include <string.h>
362
+
363
+ #include <stdlib.h>
364
+
365
+
366
+
367
+ #define NEW(p, n){p = malloc((n)*sizeof(p[0]));}
368
+
369
+
370
+
371
+ typedef struct slobj_{
372
+
373
+ struct slobj_* next;
374
+
375
+ int j;
376
+
377
+ double v;
378
+
379
+ }* slobj;
380
+
381
+
382
+
383
+ typedef struct{
384
+
385
+ slobj head;
386
+
387
+ slobj tail;
388
+
389
+ }* slist;
390
+
391
+
392
+
393
+ typedef struct{
394
+
395
+ int n,m;
396
+
397
+ slist* A;
398
+
399
+ } smatrix;
400
+
401
+
402
+
403
+ slist slist_new(void){
404
+
405
+ slist L;
406
+
407
+ NEW(L, 1);
408
+
409
+ L -> head = NULL;
410
+
411
+ L -> tail = NULL;
412
+
413
+ return L;
414
+
415
+ }
416
+
417
+
418
+
419
+ slobj slobj_new(int x, double y){
420
+
421
+ slobj p;
422
+
423
+ NEW(p, 1);
424
+
425
+ p -> j = x;
426
+
427
+ p -> v = y;
428
+
429
+ p -> next = NULL;
430
+
431
+ return p;
432
+
433
+ }
434
+
435
+
436
+
437
+ void slist_insert_head(slist L, slobj p){
438
+
439
+ p -> next = L -> head;
440
+
441
+ L -> head = p;
442
+
443
+ }
444
+
445
+
446
+
447
+ void slist_insert_tail(slist L, slobj p){
448
+
449
+ if(L -> head == NULL){
450
+
451
+ L -> tail = p;
452
+
453
+ L -> head = p;
454
+
455
+ }
456
+
457
+ else if(L -> head == L -> tail){
458
+
459
+ L -> tail = p;
460
+
461
+ L -> head -> next = p;
462
+
463
+ }
464
+
465
+ else{
466
+
467
+ L -> tail -> next = p;
468
+
469
+ L -> tail = p;
470
+
471
+ }
472
+
473
+ }
474
+
475
+
476
+
477
+ void slist_print(slist L)
478
+
479
+ {
480
+
481
+ slobj p;
482
+
483
+ p = L->head;
484
+
485
+ while (p != NULL){
486
+
487
+ printf("%d ", p -> j);
488
+
489
+ printf("%lf ", p -> v);
490
+
491
+ p = p-> next;
492
+
493
+ }
494
+
495
+ }
496
+
497
+
498
+
499
+ smatrix smatrix_new(int n, int m){
500
+
501
+ smatrix S;
502
+
503
+ int i;
504
+
505
+ NEW(S.A, n);
506
+
507
+ S.n = n;
508
+
509
+ S.m = m;
510
+
511
+ for(i=0; i<= n-1; i++){
512
+
513
+ S.A[i]=slist_new();
514
+
515
+ }
516
+
517
+ return S;
518
+
519
+ }
520
+
521
+
522
+
523
+ smatrix smatrix_read(void){
524
+
525
+ smatrix S;
526
+
527
+ slobj p;
528
+
529
+ int i, n, m, j;
530
+
531
+ double v;
532
+
533
+ scanf("%d", &n);
534
+
535
+ scanf("%d", &m);
536
+
537
+ S = smatrix_new(n, m);
538
+
539
+ for(i = 0; i <= n-1; i++){
540
+
541
+ while(1){
542
+
543
+ scanf("%d", &j);
544
+
545
+ if (j < 0){
546
+
547
+ break;
548
+
549
+ }
550
+
551
+ scanf("%lf", &v);
552
+
553
+ slobj p = slobj_new(j, v);
554
+
555
+ slist_insert_tail(S.A[i], p);
556
+
557
+ }
558
+
559
+ }
560
+
561
+ return S;
562
+
563
+ }
564
+
565
+
566
+
567
+ void smatrix_print(smatrix S){
568
+
569
+ int i;
570
+
571
+ printf("%d ", S.n);
572
+
573
+ printf("%d\n", S.m);
574
+
575
+ for(i = 0; i <= S.n-1; i++){
576
+
577
+ slist_print(S.A[i]);
578
+
579
+ printf("%d\n", -1);
580
+
581
+ }
582
+
583
+ }
584
+
585
+
586
+
587
+ smatrix smatrix_transpose(smatrix S){
588
+
589
+ int i, k;
590
+
591
+ double x;
592
+
593
+ slobj p, q;
594
+
595
+ smatrix T;
596
+
597
+ for(i = 0; i <= S.n-1; i++){
598
+
599
+ p = S.A[i]->head;
600
+
601
+ while(1){
602
+
603
+ k = p->j;
604
+
605
+ x = p->v;
606
+
607
+ q = slobj_new(i+1, x);
608
+
609
+ slist_insert_tail(T.A[k-1], q);
610
+
611
+ p = p->next;
612
+
613
+ if(p=NULL){
614
+
615
+ break;
616
+
617
+ }
618
+
619
+ }
620
+
621
+ }
622
+
623
+ return T;
624
+
625
+ }
626
+
627
+
628
+
629
+ void slist_free(slist L)
630
+
631
+ {
632
+
633
+ slobj p, q, r;
634
+
635
+ p = L->head;
636
+
637
+ q = p->next;
638
+
639
+ while (q != NULL){
640
+
641
+ r = q;
642
+
643
+ q = q-> next;
644
+
645
+ free(p);
646
+
647
+ p = r;
648
+
649
+ }
650
+
651
+ free(L);
652
+
653
+ }
654
+
655
+
656
+
657
+ void smatrix_free(smatrix S){
658
+
659
+ int i;
660
+
661
+ for(i = 0; i <= S.n-1; i++){
662
+
663
+ if(S.A[i]->head != NULL){
664
+
665
+ slist_free(S.A[i]);
666
+
667
+ }
668
+
669
+ }
670
+
671
+ }
672
+
673
+
674
+
675
+ int main(){
676
+
677
+ smatrix A;
678
+
679
+ smatrix B;
680
+
681
+ A = smatrix_read();
682
+
683
+ smatrix_print(A);
684
+
685
+ B = smatrix_transpose(A);
686
+
687
+ smatrix_print(B);
688
+
689
+ smatrix_free(A);
690
+
691
+ smatrix_free(B);
692
+
693
+ return 0;
694
+
695
+ }
696
+
697
+ ```
698
+
699
+ ```valgrind
700
+
701
+ ==1188== Memcheck, a memory error detector
702
+
703
+ ==1188== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
704
+
705
+ ==1188== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
706
+
707
+ ==1188== Command: ./a.out
708
+
709
+ ==1188==
710
+
711
+ ==1188== error calling PR_SET_PTRACER, vgdb might block
712
+
713
+ 2 3
714
+
715
+ 1 1.0 -1
716
+
717
+ 2 3.4 -1
718
+
719
+ 2 3
720
+
721
+ 1 1.000000 -1
722
+
723
+ 2 3.400000 -1
724
+
725
+ ==1188== Use of uninitialised value of size 8
726
+
727
+ ==1188== at 0x108BD3: smatrix_transpose (in /home/keigoh/a.out)
728
+
729
+ ==1188== by 0x108D26: main (in /home/keigoh/a.out)
730
+
731
+ ==1188==
732
+
733
+ ==1188== Invalid read of size 8
734
+
735
+ ==1188== at 0x108BD3: smatrix_transpose (in /home/keigoh/a.out)
736
+
737
+ ==1188== by 0x108D26: main (in /home/keigoh/a.out)
738
+
739
+ ==1188== Address 0x2b911eb00 is not stack'd, malloc'd or (recently) free'd
740
+
741
+ ==1188==
742
+
743
+ ==1188==
744
+
745
+ ==1188== Process terminating with default action of signal 11 (SIGSEGV)
746
+
747
+ ==1188== Access not within mapped region at address 0x2B911EB00
748
+
749
+ ==1188== at 0x108BD3: smatrix_transpose (in /home/keigoh/a.out)
750
+
751
+ ==1188== by 0x108D26: main (in /home/keigoh/a.out)
752
+
753
+ ==1188== If you believe this happened as a result of a stack
754
+
755
+ ==1188== overflow in your program's main thread (unlikely but
756
+
757
+ ==1188== possible), you can try to increase the size of the
758
+
759
+ ==1188== main thread stack using the --main-stacksize= flag.
760
+
761
+ ==1188== The main thread stack size used in this run was 8388608.
762
+
763
+ ==1188==
764
+
765
+ ==1188== HEAP SUMMARY:
766
+
767
+ ==1188== in use at exit: 120 bytes in 6 blocks
768
+
769
+ ==1188== total heap usage: 8 allocs, 2 frees, 2,168 bytes allocated
770
+
771
+ ==1188==
772
+
773
+ ==1188== LEAK SUMMARY:
774
+
775
+ ==1188== definitely lost: 0 bytes in 0 blocks
776
+
777
+ ==1188== indirectly lost: 0 bytes in 0 blocks
778
+
779
+ ==1188== possibly lost: 0 bytes in 0 blocks
780
+
781
+ ==1188== still reachable: 120 bytes in 6 blocks
782
+
783
+ ==1188== suppressed: 0 bytes in 0 blocks
784
+
785
+ ==1188== Rerun with --leak-check=full to see details of leaked memory
786
+
787
+ ==1188==
788
+
789
+ ==1188== For counts of detected and suppressed errors, rerun with: -v
790
+
791
+ ==1188== Use --track-origins=yes to see where uninitialised values come from
792
+
793
+ ==1188== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
794
+
795
+ Segmentation fault (core dumped)
796
+
797
+ ```