Memory error when copying char pointer

This is a discussion on Memory error when copying char pointer within the C Programming forums, part of the General Programming Boards category; Dear All, am trying to copy on char ** object to another but getting this memeory error: VM: Pagefault SIGSEGV ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    8

    Memory error when copying char pointer

    Dear All,
    am trying to copy on char ** object to another but getting this memeory error:
    VM: Pagefault SIGSEGV bad addr no page read .. Memory fault (core dumped).

    The original array is being printed normally and then the above error message appears.
    Here is my code and appreciate the help:
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <errno.h>
    #include <string.h> 
    #include <fcntl.h>
    #include <stdlib.h>
    #include <sys/stat.h>
    
    extern char **getline();
    
    
    int main() {
      int i,j;
      char **args, ** args_original;
      int * pipe_fd;
      while(1) {
          printf("$ ");
          args = NULL;
          args_original = getline();
       
        /* print first array */ 
        for(j = 0; args_original[j] != NULL; j++)
    	printf(" Original before copying: %d: %s\n", j, args_original [j]);
       
       /* copy args_original to args*/
          for(i = 0; args_original[i] != NULL; i++){
    	 args[i]= strcpy(args[i], args_original[i]);
    	  
          }
          args[i] = NULL;
          
    
    for(j = 0; args[j] != NULL; j++)
    	printf(" copy: %d: %s\n", j, args[j]);
    
       }/* end while */
    }
    Regards,
    sismail

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
       /* copy args_original to args*/
          for(i = 0; args_original[i] != NULL; i++){
    	 args[i]= strcpy(args[i], args_original[i]);
    You're trying to copy a string into nowhere land. You need to malloc() some memory for args first.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    Thanks for your answer, I actually tried the following:

    Code:
          
          s = (getSize(args_original) * sizeof(char));
          args = malloc(s);
    ...
    Code:
    int getSize(char ** args){
    
    int s = 0;
    int i;
    for (i =0; args[i] != NULL; i++)
    	s = s+ strlen(args[i]);
    return s;
    }/* end: getSize */
    But still getting the same error. It could be that the size am allocating is not enough. But am not sure how to get exactly the size of a char ** if not in the way I already did.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,002
    You are treating args as if it were a 1D array. It doesn't work because you don't understand what a pointer to pointer is and how you use dynamic 2D arrays.
    Take a look: http://cboard.cprogramming.com/c-pro...tml#post925712
    Perhaps it will show you why it's wrong.
    Last edited by Elysia; 03-08-2010 at 09:12 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    thanks for the info. but hte link is broken

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,002
    Since when is copy n' paste considered such a bad thing? All you had to do was to copy and paste the URL instead of waiting for someone to hopefully fix it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    you need to take it easy on your self ;-)
    But thanks anyway.

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    Thanks to all , here is the right code I used to fix this issue:
    Code:
     args_original = getline();
     
          args = malloc(20 * 2 * sizeof(char*)); 
    
        /* print first array */ 
    	printf("Size:%d\n", s);
        for(j = 0; args_original[j] != NULL; j++)
    	printf(" Original before copying: %d: %s\n", j, args_original [j]);
       
      /*    copy args_original to args*/
          for(i = 0; args_original[i] != NULL; i++){
    		args[i] = malloc(strlen(args_original[i]) * sizeof(char));
    		args[i]= strcpy(args[i], args_original[i]);
            }
    Last edited by sismail; 03-08-2010 at 09:43 AM.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    > args = malloc(20 * 2 * sizeof(char*));
    Rather than guessing there are only 40 max pointers, how about moving it after the loop which in effect COUNTS them for you.

    > args[i] = malloc(strlen(args_original[i]) * sizeof(char));
    You're one char short.
    strlen() only counts the number of chars, but strcpy() ALSO copies a \0 as well.
    You didn't allocate space for that.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  2. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 10:22 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21