Thread: strcpy and 2d malloc

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    54

    strcpy and 2d malloc

    Hello everyone..
    I have the following code:

    Code:
    for (k = 1; k < argc; k++) {
            cop = strcmp(argv[k], "-n");
            if (!cop) {
                size = atoi(argv[k+1]);
            }
        }
    
    
        if ((player1 = malloc(size * sizeof(int *))) == NULL) {
            printf("\n\nCould not allocate memory!");
            return -1;
        }
        for (k = 0; k < size; k++) {
            if ((*(player1+k) = malloc(size * sizeof(int))) == NULL) {
                printf("\n\nCould not allocate memory!");
                return -1;
            }
        }
    and

    Code:
    for (k = 0; k < size; k++) {
            for (j = 0; j < size; j++) {
                strcpy(&player1[k][j], " ");
            }
         }
    when I run the code, instead of " " it shows NULL and segmentation fault.. Why?
    Last edited by Sotiris Kaniras; 01-25-2013 at 09:28 AM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Soritis, please wrap your code with code tags

    Like this : [code]/*your program*/[/code]

    And also tell us why you are not happy with your code
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You access argv[k+1] before checking that k+1 < argc, so it is possible that you are calling atoi with a null pointer if k == argc - 1. Then, you fail to check that size is positive (or zero, but that would not be useful) before calling malloc using it.

    Next, what is player1? A mental disconnect between what it is (its type) and how you are using it could be the mistake that is causing the segmentation fault here.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    This is the desired result:

    Code:
    			   W H I T E
    		 A   B   C   D   E   F   G   H  
    
    
      		 -   -   -   -   -   -   -   -  
    		/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \
                 1 |   |   |   |   |   |   |   |   | 1
    		\_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \    
                   2 |   |   |   |   |   |   |   |   | 2
    		  \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \      
                     3 |   |   |   |   |   |   |   |   | 3
    		    \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \        
                       4 |   |   |   |   |   |   |   |   | 4
    		      \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \          
                         5 |   |   |   |   |   |   |   |   | 5
    		        \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \            
                           6 |   |   |   |   |   |   |   |   | 6
    		          \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \              
                             7 |   |   |   |   |   |   |   |   | 7
    		            \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \                
                               8 |   |   |   |   |   |   |   |   | 8
    		              \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ 
    			       A   B   C   D   E   F   G   H 


    And I get this:

    Code:
    
    
    Code:
             -   -   -   -   -   -   -   -  
       1 | (null)  | (null)  | (null)  | (null)  | (null)  | (null)  | (null)  | (null)  | 1
              \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \  
         2 | (null)  | (null)  | (null)  | (null)  | (null)  | (null)  | (null)  | (null)  | 2
              \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \    
           3 | (null)  | (null)  | (null)  | (null)  | (null)  | (null)  | (null)  | (null)  | 3
                \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \      
             4 | (null)  | (null)  | (null)  | (null)  | (null)  | (null)  | (null)  | (null)  | 4
                  \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \        
               5 | (null)  | (null)  | (null)  | (null)  | (null)  | (null)  | (null)  | (null)  | 5
                    \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \          
                 6 | (null)  | (null)  | (null)  | (null)  | (null)  | (null)  | (null)  | (null)  | 6
                      \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \            
                   7 | (null)  | (null)  | (null)  | (null)  | (null)  | (null)  | (null)  | (null)  | 7
                        \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \              
                     8 | (null)  | (null)  | (null)  | (null)  | (null)  | (null)  | (null)  | (null)  | 8
                          \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \
                             A   B   C   D   E   F   G   H


    My program is divided to one file.h and 2 file.c..

    Header file:

    int design(char, char **player1, char **player2);

    designf.c

    Code:
    #include <stdio.h>
    #include <string.h>
    #include "hfiles.h"
    
    
    int design(char size, char **player1, char **player2)
    {
         int k, j, l, s, p, c, f, t, h;
         char *white[] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    
    
         h = 0;
         f = 0;
         c = 1;
         p = 0;
         t = 1;
         s = 0;
    
    
         for (k = 0; k < size; k++) {
            for (j = 0; j < size; j++) {
                strcpy(&player1[k][j], " ");
            }
         }
    
    
         for (k = 0; k < size; k++) {
             if (k == 0 && j == 0) {
                 if (size % 2 == 0) {
                     while (c < (size/2)) {
                           printf("    ");
                           c++;
                     }
                     printf("       W H I T E\n");
                 }
                 else {
                     while (c <= (size/2)) {
                          printf("    ");
                          c++;
                     }
                     printf("     W H I T E\n");
                 }
                 for (l = 0; l < size; l++) {
                     if (l == 0) {
                         printf("          ");
                     }
                     printf("%2s  ", white[l]);
                 }
                 printf("\n\n");
            }
    
    
            s = k;
            while (s > 0) {
                 printf("  ");
                   s--;
              }
              for (j = 0; j < size; j++) {
                  if (j <= size && k == 0) {
                      if (j == 0) {
                          printf("        ");
                      }
                      printf(" -  ");
                  }
                  if (j == size && k == 0) {
                      printf("\n");
                      for (l = 0; l < size; l++) {
                          if (l == 0) {
                              printf("          ");
                          }
                          if (l < size-1)
                              printf("/ \\_");
                          else
                              printf("/ \\");
                      }
                  }
              }
              printf("\n");
              while (f < k) {
                  printf("  ");
                  f++;
              }
              f = 0;
              if (k < 10) {
                  printf("   %d", k+1);
              }
              else
                 printf("  %d", k+1);
              while (p <= size) {
                   if (p < size) {
                       printf(" | %s ", player1[k][j]);
                   }
                   else
                      printf(" | %d", k+1);
                   p++;
              }
              p = 0;
              printf("\n");
              for (l = 1; l <= size; l++) {
                  if (l == 1) {
                      printf("          ");
                  }
                  if (k == 0) {
                      printf("\\_/ ");
                  }
                  else {
                      if (l == 1) {
                          while (t < k) {
                              printf("  ");
                              t++;
                          }
                          t = 1;
                      }
                      printf("\\_/ ");
                  }
              }
              if (k < size) {
                  printf("\\");
              }
          }
    
    
         printf("\n");
         if (size % 2 == 0) {
             while (h <= size/2) {
                 printf("    ");
                 h++;
             }
         }
         else {
             while (h <= size/2) {
                 printf("    ");
                 h++;
             }
             printf("   ");
         }
         printf("    ");
         for (k = 0; k < size; k++) {
             printf("%2s  ", white[k]);
         }
    
    
         return 0;
    }


    main.c

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include "hfiles.h"
    
    
    int main(int argc, char *argv[])
    {
        int k, j;
        char size, cop, **player1, **player2;
    
    
        cop = 1;
        size = 11;
        for (k = 1; k < argc; k++) {
            cop = strcmp(argv[k], "-n");
            if (!cop) {
                size = atoi(argv[k+1]);
            }
        }
    
    
        if ((player1 = malloc(size * sizeof(int *))) == NULL) {
            printf("\n\nCould not allocate memory!");
            return -1;
        }
        for (k = 0; k < size; k++) {
            if ((*(player1+k) = malloc(size * sizeof(int))) == NULL) {
                printf("\n\nCould not allocate memory!");
                return -1;
            }
        }
    
    
        if ((player2 = malloc(size * sizeof(int *))) == NULL) {
            printf("\n\nCould not allocate memory!");
            return -1;
        }
        for (k = 0; k < size; k++) {
             if ((*(player2+k) = malloc(size * sizeof(int))) == NULL) {
                 printf("\n\nCould not allocate memory!");
                 return -1;
             }
        }
    
    
        design(size, player1, player2);
        printf("\n\n");
    
    
        for (k = 0; k < size; k++) {
            free(*(player1+k));
            free(*(player2+k));
        }
        free(player1);
        free(player2);
    
    
        return 0;
    }
    Sorry for the size of the post!

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Right, since player1 is a char**, why do you use sizeof(int*)? Rather, you should use: player1 = malloc(size * sizeof(*player1))
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    Quote Originally Posted by laserlight View Post
    Right, since player1 is a char**, why do you use sizeof(int*)? Rather, you should use: player1 = malloc(size * sizeof(*player1))
    I did it but nothing.. Still the same..

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Notice that there are other places that need a similiar fix.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    You could try the following as a debugging aid:
    1. Use calloc instead of malloc
    2. Use assert in appropriate places, for example

    Code:
    assert(&player1[k][j] != NULL);
    strcpy(&player1[k][j], " ")

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    int design(char size, char **player1, char **player2)
    ...
    strcpy(&player1[k][j], " ");
    ...
    printf(" | %s ", player1[k][j]);
    I recommend brushing up your knowledge about string arrays in C (actually I recommend the whole tutorial).
    player1[k][j] is a character, not a string.

    Bye, Andreas

  10. #10
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    Quote Originally Posted by AndiPersti View Post
    Code:
    int design(char size, char **player1, char **player2)
    ...
    strcpy(&player1[k][j], " ");
    ...
    printf(" | %s ", player1[k][j]);
    I recommend brushing up your knowledge about string arrays in C (actually I recommend the whole tutorial).
    player1[k][j] is a character, not a string.

    Bye, Andreas
    I corrected it to "%c" but it doesn't write out anything.. why?

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    What Andreas said is correct. Probably you never reach the printf (this has to do with the counters) or you don't have something in the array to output.

    I agree on Andreas for the tutorial. Take a look at it, have a break and then continue
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  12. #12
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    Quote Originally Posted by std10093 View Post
    What Andreas said is correct. Probably you never reach the printf (this has to do with the counters) or you don't have something in the array to output.

    I agree on Andreas for the tutorial. Take a look at it, have a break and then continue
    No, it reaches the printf, I've tested it

  13. #13
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    print it like this
    Code:
    printf("|%c|\n", ...);
    to see exactly what the data is. It may be a whitespace.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  14. #14
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    Code:
                         w h i t e
               a   b   c   d   e   f   g   h  
    
    
               -   -   -   -   -   -   -   -  
              / \_/ \_/ \_/ \_/ \_/ \_/ \_/ \
           1 |  
     |  
     |  
     |  
     |  
     |  
     |  
     |  
    
    
     | 1
              \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \  
             2 |  
     |  
     |  
     |  
     |  
     |  
     |  
     |  
    
    
     | 2
                \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \    
               3 |  
     |  
     |  
     |  
     |  
     |  
     |  
     |  
    
    
     | 3
                  \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \      
                 4 |  
     |  
     |  
     |  
     |  
     |  
     |  
     |  
    
    
     | 4
                    \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \        
                   5 |  
     |  
     |  
     |  
     |  
     |  
     |  
     |  
    
    
     | 5
                      \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \          
                     6 |  
     |  
     |  
     |  
     |  
     |  
     |  
     |  
    
    
     | 6
                        \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \            
                       7 |  
     |  
     |  
     |  
     |  
     |  
     |  
     |  
    
    
     | 7
                          \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \              
                         8 |  
     |  
     |  
     |  
     |  
     |  
     |  
     |  
    
    
     | 8
                            \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ 
                             a   b   c   d   e   f   g   h
    So, it works!

  15. #15
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    newline character I guess is that you have in there.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-19-2010, 07:42 AM
  2. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  3. strcpy
    By Abda92 in forum C Programming
    Replies: 7
    Last Post: 09-13-2006, 10:05 AM
  4. strcpy
    By Luigi in forum C++ Programming
    Replies: 17
    Last Post: 02-16-2003, 04:11 PM
  5. HELP!! strcpy
    By abbynormal87 in forum C++ Programming
    Replies: 3
    Last Post: 05-02-2002, 07:34 AM