Guys, I have got a new problem with it. I am not able to delete the allocated memory, here lies the code
Code:
#include <iostream>
#include <pthread.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h> //memset
#include <stdlib.h> //exit()
#include <net/ethernet.h>
#include <netinet/ip.h> //Provides declarations for ip header
#include <netinet/tcp.h> //Provides declarations for tcp header
#include <arpa/inet.h>
using namespace std;
struct message{
int a;
int b;
char c[50];
};
pthread_mutex_t thread_lock;
void* function(void*);
int main(){
int i=0;pthread_t thread;message *message1;
while(1){
while(i<50000){
message1=new message;
message1->a = i*1;
message1->b = i*2;
pthread_create(&thread, NULL, function, message1);
i++;
}
i=0;
sleep(5);
}
return 0;
}
void* function(void* data){
sleep(3);
message *message1 = (message*)data;
pthread_mutex_lock(&thread_lock);
cout<<"Result of Addition: "<<message1->a+message1->b<<endl;
pthread_mutex_unlock(&thread_lock);
//delete (message*)data
//delete (message*)*&data;
//delete message1;
pthread_exit(NULL);
}
The problem is that I am not able to free the memory that was allocated in main with these three delete statements. consequently, the program fails, I think memory out when the memory usage reaches around 10.4%. Please tell how can I free the memory