Thread: Can not get valid value from Char* buffer

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    3

    Can not get valid value from Char* buffer

    Hi,guys:

    I put two struct variables into a continuous memory at first by using memcpy(). And then I want to print out each variable's element. Sometimes it works,but sometimes it just prints out "0". Can someone tell me the reason of that?


    Thanks,
    Attached Files Attached Files
    • File Type: c test.c (711 Bytes, 146 views)

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The only thing I see wrong is an egregious lack of spacing. BTW, sizeof(char) is defined as 1.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    post your code.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Works all the time for me. What do you mean it works sometimes?

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    3
    The issue happens in the MPI code. The new code is attached.
    Code:
    #include <stdio.h>
    #include <mpi.h>
    #include <stdlib.h>
    #include <string.h>
    typedef struct message message;
    struct message
    {
      message*prev;
    	message*next;
    	int recv_pe;
    };
    
    //typedef struct message message;
    
    int main (int argc, char **argv)
    {
      int rank, size;
     
      int iter=1000;
      int number=15;
    	
    	int ierr,errclass;
      ierr=0; 
    	MPI_Request request;
    	MPI_Status status; 
    	int send_buffer[5],recv_buffer[5];
      MPI_Init (&argc, &argv);	/* starts MPI */
      MPI_Comm_rank (MPI_COMM_WORLD, &rank);	/* get current process id */
      MPI_Comm_size (MPI_COMM_WORLD, &size);	/* get number of processes */
     
    // MPI_Errhandler_set(MPI_COMM_WORLD,MPI_ERRORS_RETURN); /* return info about
    //	      errors */
    //	printf( "Hello world from process %d of %d\n", rank, size );
    	if(rank==0)
    	{  
    	    int kk;
    	    for(kk=0;kk<iter;kk++)
    				{
    	    int buffsize= number*sizeof(message);
    			char*buffer=(char*)malloc(buffsize*sizeof(char));
    	   // void*buffer=(char*)malloc((unsigned)buffsize);
    			int position=0;
    			message* sendevent[number];
    			int i;
    			for(i=0,position=0;i<number;i++,position+=sizeof(message))
    			{
    				sendevent[i]=malloc(sizeof(message));
    				sendevent[i]->recv_pe=i;
            memcpy(&buffer[position],sendevent[i],sizeof(message));
          }
    			ierr= MPI_Isend(buffer,buffsize, MPI_BYTE,1, 1,MPI_COMM_WORLD,&request);         
    			}
    		}
    	else
    		{
    		  int kk;
    			for(kk=0;kk<iter;kk++)
    			{
    	    int buffsize=number*sizeof(message);
    			char*buffer=(char*)malloc(sizeof(char)*buffsize);
    				int position=0;
    			  message *tmp=NULL;
    				int flag=0;
    				int i;
    				MPI_Request request;
    				while(flag==0)
    					{
    						MPI_Iprobe(MPI_ANY_SOURCE,
    						MPI_ANY_TAG,
    						 MPI_COMM_WORLD,
    						 &flag,
    						 &status);
              }
             ierr=MPI_Irecv(buffer,buffsize,MPI_BYTE,0,1,MPI_COMM_WORLD,&request);   		        
    				for(i=0,position=0;i<number;i++,position+=sizeof(message))
    				{
    				// MPI_Unpack(buffer,buffsize,&position,&revevent[i],sizeof(revevent[i]),MPI_BYTE,MPI_COMM_WORLD);
    				 tmp=(message*)&buffer[position];
    				 if(i==1&&tmp->recv_pe==0)
    				 printf("error :recv data invalid\n");
    				}
    			}
    		}
    				
      MPI_Finalize();
      return 0;
    }
    Attached Files Attached Files
    Last edited by Salem; 01-05-2012 at 02:54 PM. Reason: inlined code - indent it crap due to mixed spaces and tabs

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Post your code means cut and paste it into the edit window when you make a post. Don't attach it as a file, it's a PITA for us. Attachments are more suited for images and the like, non-text files. Be sure to use code tags. Read this link: Announcements - General Programming Boards.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking if char *path; is a valid directory.
    By b1nd3r in forum C Programming
    Replies: 2
    Last Post: 11-22-2010, 02:55 PM
  2. Socket file descriptor valid and then not valid
    By Florian in forum C Programming
    Replies: 3
    Last Post: 05-22-2010, 08:23 AM
  3. (char)09 not valid?
    By mikeyp in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2009, 09:09 PM
  4. how to know if there is some char in the buffer..
    By transgalactic2 in forum C Programming
    Replies: 30
    Last Post: 01-30-2009, 03:27 PM
  5. Using a *char Buffer
    By Hankyaku in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2003, 01:49 AM