Thread: strcpy() with an array of strings

  1. #1
    J-Dogg
    Guest

    strcpy() with an array of strings

    Hi. I have a multidimensional arrays of char's that I'm using to hold strings. The multidimensional array is shared with a forked process. I'm getting a segmentation fault when I try to use strcpy to place a string into one of the rows of the mulitidimensional array. Here's my code.


    Code:
    #include <string.h>
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/shm.h>
    #include <stdlib.h>
    
    int shmid;
    int main()
    {
    	int id, index;
                    char **shmaddr = (char **)malloc(7 * sizeof(char *));
                    shmaddr[0] = (char *)malloc(7 *  50 * sizeof(char));
                    for (index = 0; index < 7; index++)
                    {
    	                shmaddr[index] = shmaddr[0] + index * 50;
                    }
    	shmid = shmget(IPC_PRIVATE, 4096, IPC_CREAT|0644);
    	shmaddr = shmat(shmid, (void *) 0, 0);
     	fork();
                    strcpy(shmaddr[0],"stuff");
                    printf("First position of shared memory has contents %s\n", shmaddr[0]);
                    if (id != 0){
    	shmctl(shmid, IPC_RMID, (struct shmid_ds *) 0);}
    
    	return 0;
    }


    code tags fixed by Hammer.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>shmaddr[0] = (char *)malloc(7 * 50 * sizeof(char));
    >>shmaddr[index] = shmaddr[0] + index * 50;
    shmaddr[0] holds a pointer to a malloc() section of data. You overwrite that pointer just 2 lines later. shmaddr[0] no longer points to your malloc()'d memory, in fact it points to memory you probably don't own, hence strcpy() can't do its job.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    J-Dogg
    Guest
    Oh... that's the code somebody gave me when I told them I wanted a multidimensional char array that was shared between all processes... why then, does replacing the call to strcpy() with

    <code>
    shmaddr[0] = "stuff";
    </code>

    work?

  4. #4
    J-Dogg
    Guest
    Sorry about starting a new thread for the same thing. I won't do that again, but I still could use a hand with this problem. I was allocating the memory wrong before, as you guys pionted out. I fixed that, and now use the following code:

    Code:
    char ** buf;
    int shmid;
    
    shmid = shmget(IPC_PRIVATE, 350 * sizeof(char), IPC_CREAT|0644);
    buf = shmat(shmid, (void *) 0, 0);
    for (index = 0; index < 7; index++)
    {
         buf[index] = buf[0] + index * sizeof(char);
    }
    But I still can't figure out how to copy a char * array into this space.
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. intializing an array of strings
    By doubty in forum C Programming
    Replies: 4
    Last Post: 06-19-2009, 12:59 PM
  2. Replies: 1
    Last Post: 03-19-2009, 10:56 AM
  3. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  4. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  5. remove strings from array
    By ipe in forum C Programming
    Replies: 2
    Last Post: 01-12-2003, 04:53 AM