Thread: segmentation fault using memcpy

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    15

    Question segmentation fault using memcpy

    hi

    im trying to browse a tree-structure within my prog. the function i implemented to do this gets a treepointer and a path and returns a node-pointer. with the first call everything is fine, but being a recursive function, the second call fails with a segmentation fault at the call of [memcpy].
    this is how my method looks like:

    pSceneGraph
    findSceneGraphNode(pSceneGraph root, char* objectname){
    pSceneGraph tempSceneGraph =malloc(sizeof(struct sceneGraph));

    char* sub_string;
    char* rest_string;

    memcpy ( sub_string, objectname, 50*sizeof(char) );
    rest_string=strchr(objektname,'/');
    strtok(sub_string,"/");

    if (!isEmpty(root) && (sub_string!=NULL)){
    if (strcmp(sub_string,root->object.name)==0){
    if (rest_string!=NULL){
    ++*rest_string;
    return findSceneGraphNode(root->child,rest_string);
    }
    return root;
    }
    return findSceneGraphNode(root->next,objectname);
    }
    return (pSceneGraph) 0;

    }


    any ideas?

    thanks for any suggestions
    martin

  2. #2

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Code:
    memcpy ( sub_string, objectname, 50*sizeof(char) );
    memcpy takes destination first, sub_string is an uninitialized pointer with likely no valid memory at its destination.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    15
    ok. and how do i initialize it correctly?

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Allocate some memory for it. I can see from the code that you know how to use malloc() so you shouldn't have any problem with that.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    Code:
    char* sub_string = malloc(50 * sizeof *sub_string);
    char* rest_string;
    
    if (sub_string == NULL)
      return NULL;
    
    /* Rest of code */

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    15
    thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault in memcpy
    By George2 in forum C Programming
    Replies: 14
    Last Post: 08-24-2007, 02:58 PM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM