Click here to Skip to main content
14,244,275 members

C / C++ / MFC

 
GeneralRe: Has C++ ever considered XML style namespaces? Pin
Stefan_Lang6-May-19 4:41
memberStefan_Lang6-May-19 4:41 
QuestionMFC Top Level menu position. Pin
Maximilien29-Apr-19 7:50
memberMaximilien29-Apr-19 7:50 
AnswerRe: MFC Top Level menu position. Pin
leon de boer1-May-19 0:13
memberleon de boer1-May-19 0:13 
QuestionImage display problem in listcontrol Pin
tianzhili439925-Apr-19 16:50
membertianzhili439925-Apr-19 16:50 
AnswerRe: Image display problem in listcontrol Pin
Victor Nijegorodov25-Apr-19 21:07
memberVictor Nijegorodov25-Apr-19 21:07 
GeneralRe: Image display problem in listcontrol Pin
tianzhili439925-Apr-19 22:29
membertianzhili439925-Apr-19 22:29 
GeneralRe: Image display problem in listcontrol Pin
Victor Nijegorodov26-Apr-19 2:03
memberVictor Nijegorodov26-Apr-19 2:03 
QuestionConversion from- C++ to C-language < free(): invalid size > Pin
zak10025-Apr-19 9:14
memberzak10025-Apr-19 9:14 
Hi,
I am converting C++ mpi program into C:

I am free() error because I am using malloc at several places. I have data stored in a file. The first line of the file tells about number of lines of data in the file. I am getting that error correct i.e. 3200.

I have also corrected several errors with the cooperation of code project's developers. I am testing my code with:
print statements followed by:
finalize();
return;


My code upto the point where I am getting invalid free() error is:

#include <stdio.h>
#include <math.h>
#include <mpi.h>
#include <time.h>
#include <stdlib.h>
//#include <vector.h>


//using namespace std;

// Sorts the input row into chunks to be scattered two all the processors.
void sortByProcess(/*vector<double>*/double* list1, double* list2, int count);

// Swaps two rows.
void swap(double** list, int count, int row1, int row2);


int rank, size;
int main(int argc, char * argv[])
{
  double sTime, eTime, rTime;
  /*ifstream*/ FILE* inFile;
  int num_rows = 3200;
  int num_cols = 3200;
  int cur_control = 0;
  double * send_buffer = NULL;
  double * recv_buffer = NULL;
  double ** data = NULL;
  double determinant;
  char strNum_rows[20]; 
  /*vector<double>*/double* file_buffer;

  // Just get the initialization of the program going.
  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Comm_size(MPI_COMM_WORLD, &size);

  // If the input file is not given, print message and exit.
  if(argc < 2)
  {
    /*cout <<*/ printf("No input file given.\n");// << endl;
    MPI_Finalize();
    return 0;
  }
  // If the root node (0), then open the input file and read in the
  // number of rows.
  if(!rank)
  {
    printf("After rank inside  if @@@@@");
    inFile = fopen(argv[1], "r");
    fgets(strNum_rows, 20, inFile); 
    num_rows = atoi(strNum_rows);
    printf("num_rows???? =%d",num_rows);
    file_buffer = malloc(num_rows * sizeof(int));

    /*???? inFile.open(argv[1]);
    inFile >> num_rows;
    file_buffer.resize(num_rows);*/
  }
  
  //printf("After rank outside @@@@@@@@#");
  
   send_buffer = (double *)malloc(num_rows * sizeof(double));
  
/*?????send_buffer = new double[num_rows];*/
  //printf("After send_buffer #####@");
  // Broadcasts the number of rows to each processor.
  MPI_Bcast (&num_rows, 1, MPI_INT, 0, MPI_COMM_WORLD);
  num_cols = num_rows / size;
  // Allocate the memory on each processor.
  //printf("After Bcast #####@");

  data = (double **) malloc(num_cols * sizeof(double *));
  /*???? data #####@");*/
  


  for(int i = 0; i < num_cols; i++)
  data[i] = (double *) malloc(num_rows * sizeof(double));

  /* ???  data[i] = new double[num_rows]; */

  for(int i = 0; i < num_cols; i++)
  {
    for(int j = 0; j < num_rows; j++)
      data[i][j] = 0;
  }

   //printf("Before recv_buffer $$$$$$$@");
   recv_buffer = (double *) malloc(num_cols * sizeof(double));
  /*???? recv_buffer = new double[num_cols];*/
   
  

  // Scatter the data.
  for(int i = 0; i < num_rows; i++)
  {
    if(!rank)
    {
      for(int j = 0; j < num_rows; j++){
         fgets(strNum_rows, 20, inFile); 
         file_buffer[j] = atof(strNum_rows);
      }
        /*????? inFile >> file_buffer[j];*/
      sortByProcess(file_buffer, send_buffer, num_rows);
    }
     
    //printf("After sortByProcess ^^^^^^@");

    
    // Scatters the data so that each process gets the next value for their columns.
    MPI_Scatter(send_buffer, num_cols, MPI_DOUBLE, recv_buffer, num_cols, MPI_DOUBLE, 0, MPI_COMM_WORLD   );
    for(int j = 0; j < num_cols; j++)
    {
      data[j][i] = recv_buffer[j];
    }
  }

    //printf("After Scatter  ^^^^^^@");
   
  /*delete []*/ 

free(recv_buffer);
  /*delete []*/ 

free(send_buffer);
  // Begin timing.
  MPI_Barrier(MPI_COMM_WORLD);
  sTime = MPI_Wtime();

   printf("After Barrier  ^^^^^^@");
   MPI_Finalize();
    return 0;



The error which I am getting is:

$ mpicc gaussian.c
$ mpirun -np 4 ./a.out matrix.3200.txt
free(): invalid size
[lc2530hz:08268] *** Process received signal ***
[lc2530hz:08268] Signal: Aborted (6)
[lc2530hz:08268] Signal code:  (-6)
[lc2530hz:08268] [ 0] /lib/x86_64-linux-gnu/libc.so.6(+0x3ef20)[0x7f107b56af20]
[lc2530hz:08268] [ 1] /lib/x86_64-linux-gnu/libc.so.6(gsignal+0xc7)[0x7f107b56ae97]
[lc2530hz:08268] [ 2] /lib/x86_64-linux-gnu/libc.so.6(abort+0x141)[0x7f107b56c801]
[lc2530hz:08268] [ 3] /lib/x86_64-linux-gnu/libc.so.6(+0x89897)[0x7f107b5b5897]
[lc2530hz:08268] [ 4] /lib/x86_64-linux-gnu/libc.so.6(+0x9090a)[0x7f107b5bc90a]
[lc2530hz:08268] [ 5] /lib/x86_64-linux-gnu/libc.so.6(cfree+0x4dc)[0x7f107b5c3e2c]
[lc2530hz:08268] [ 6] ./a.out(+0x1056)[0x564381392056]
[lc2530hz:08268] [ 7] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x7f107b54db97]
[lc2530hz:08268] [ 8] ./a.out(+0xbda)[0x564381391bda]
[lc2530hz:08268] *** End of error message ***
--------------------------------------------------------------------------
mpirun noticed that process rank 0 with PID 0 on node lc2530hz exited on signal 6 (Aborted).
--------------------------------------------------------------------------
$


Somebody please guide me how to remove this error.

Zulfi.
AnswerRe: Conversion from- C++ to C-language < free(): invalid size > Pin
k505425-Apr-19 10:05
memberk505425-Apr-19 10:05 
AnswerRe: Conversion from- C++ to C-language < free(): invalid size > Pin
John R. Shaw26-Apr-19 9:19
memberJohn R. Shaw26-Apr-19 9:19 
GeneralRe: Conversion from- C++ to C-language < free(): invalid size > Pin
zak10026-Apr-19 10:22
memberzak10026-Apr-19 10:22 
GeneralRe: Conversion from- C++ to C-language < free(): invalid size > Pin
John R. Shaw26-Apr-19 12:31
memberJohn R. Shaw26-Apr-19 12:31 
GeneralRe: Conversion from- C++ to C-language < free(): invalid size > Pin
zak10026-Apr-19 14:57
memberzak10026-Apr-19 14:57 
GeneralRe: Conversion from- C++ to C-language < free(): invalid size > Pin
zak10026-Apr-19 17:53
memberzak10026-Apr-19 17:53 
GeneralRe: Conversion from- C++ to C-language < free(): invalid size > Pin
John R. Shaw30-Apr-19 4:56
memberJohn R. Shaw30-Apr-19 4:56 
GeneralRe: Conversion from- C++ to C-language < free(): invalid size > Pin
zak10030-Apr-19 5:48
memberzak10030-Apr-19 5:48 
GeneralMessage Closed Pin
30-Apr-19 0:01
grouppestcontrol130-Apr-19 0:01 
QuestionPointer to Pointer: Conversion from- C++ to C-language Pin
zak10024-Apr-19 18:47
memberzak10024-Apr-19 18:47 
AnswerRe: Pointer to Pointer: Conversion from- C++ to C-language Pin
Victor Nijegorodov24-Apr-19 20:41
memberVictor Nijegorodov24-Apr-19 20:41 
GeneralRe: Pointer to Pointer: Conversion from- C++ to C-language Pin
leon de boer25-Apr-19 2:09
memberleon de boer25-Apr-19 2:09 
GeneralRe: Pointer to Pointer: Conversion from- C++ to C-language Pin
k505425-Apr-19 7:16
memberk505425-Apr-19 7:16 
GeneralRe: Pointer to Pointer: Conversion from- C++ to C-language Pin
Richard MacCutchan24-Apr-19 21:36
protectorRichard MacCutchan24-Apr-19 21:36 
GeneralRe: Pointer to Pointer: Conversion from- C++ to C-language Pin
leon de boer25-Apr-19 2:04
memberleon de boer25-Apr-19 2:04 
GeneralRe: Pointer to Pointer: Conversion from- C++ to C-language Pin
Richard MacCutchan25-Apr-19 2:46
protectorRichard MacCutchan25-Apr-19 2:46 
GeneralRe: Pointer to Pointer: Conversion from- C++ to C-language Pin
zak10025-Apr-19 6:52
memberzak10025-Apr-19 6:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.