MapReduce partitioning issues in C

dspaces1

I'm currently trying to use structs to hold variables of keys and values from a file. My string variable is a buffer that contains random words from a .sample file and filesize is the total number of bytes in that same file.

 struct keyValue{
    char* keystring;
    int value;
 };


int main(...){
...
/***** Split the input ****/
      struct keyValue split[numOfWords];
      int x;
      for(x=0; x<numOfWords;x++)
        {

          //split[x].keystring = wordRead(string,filesize);
          split[x].value = 1;
          //printf("this is your output %s\n",split[x].keystring);
        }
....
return 0; 
}

Here is my splitting function:

/**** Read word by word *****/

char * wordRead ( char* strings, unsigned long size){

  static int wordpointer=0;
  char* WordString;
  int i;
  for(i=0; wordpointer < size; wordpointer++,i++) {
    if ((strings[wordpointer] == ' ') )
      {
    //WordString[i+1]='/0';
    wordpointer++;
    break;
      }
    WordString[i]=strings[wordpointer];

  }

  printf("here is your word %s:\n",WordString);
  return WordString;

}

I keep getting Segmentation fault ( core dumped). I tried calling the functions a few time before the for loop and it sort of worked but the words overlap because char WordString is a pointer and previous letters over-lapped ex.

here is your word hello:
here is your word whylo:

How should dump out the pointer? Also any way I can get split[x].keystring to hold the strings from my wordRead function?

dspaces1

The code works a lot better with BLUEPIXY comment suggestion by allocating space for WordString with malloc()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related