I'm trying this in Visual Studio 2005 (VC++)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <bitset>
#include <sstream>
#include <conio.h>
#include <iostream>
using namespace std;
#include <time.h>
string tot_msg, message, to_be_sent_msg, sent_msg, msg_to_be_sent;
char * transport_layer_data_header;
char * transport_layer_data_msg;
bool whole_msg_sent=false;
int actual_len, i, seq_num=0;
char * mynode = "2";
char * dest_node = "8";
int main()
{
cout << "Enter the whole msg : ";
cin >> message;
tot_msg = message;
char lesser_msg[5];
actual_len = strlen(message.c_str());
for (i=0; i<actual_len; i++) {
// Pad data
lesser_msg[i]=message[i];
}
for (i=actual_len; i<=4; i++) {
// Pad NULLs
lesser_msg[i]=NULL;
}
lesser_msg[5]='\0';
sent_msg = lesser_msg;
char *transport_layer_data_header = (char *)malloc(6 * sizeof(char));
sprintf(transport_layer_data_header,"d%d%d%d%d",atoi(mynode),atoi(dest_node),0,seq_num);
transport_layer_data_msg = strcat(transport_layer_data_header,lesser_msg);
cout << transport_layer_data_msg << endl;
whole_msg_sent=true; // Setting the appropriate flag.
//free(transport_layer_data_header);
//free(transport_layer_data_header2);
std::cin.get();
return 0;
}
After the eventual output, this error is thrown.
Run-time error : The stack around the variable 'lesser_msg' was corrupted.
Curiously, I ran the same program in a UNIX machine under g++ and there was no error. Why would VC++ crib?