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?

char lesser_msg[5];
	actual_len = strlen(message.c_str());
 
	for (i=0; i<actual_len; i++) {
		// Pad data
		lesser_msg[i]=message[i];
	}

What happens if the actual_len is greater than 5?

Oh, well - This is just a part of the bigger code.
This actually is the part where the msg is less than 5 chars.
I should have mentioned this, but the input I gave was less than 5 chars and it worked as expected on the UNIX machine (under g++).

Surprisingly, its the first time I'm noticing something that doesn't work in Windows, but does under UNIX (Very rare!)..

Oh, well - This is just a part of the bigger code.
This actually is the part where the msg is less than 5 chars.
I should have mentioned this, but the input I gave was less than 5 chars and it worked as expected on the UNIX machine (under g++).

Surprisingly, its the first time I'm noticing something that doesn't work in Windows, but does under UNIX (Very rare!)..

The input should be 4 character or less. And just use string!

The input should be 4 character or less. And just use string!

Care to explain how?
And by the way, how'd it work under g++?

The NULL padding was a requirement for my project. How else should I go about it ?

Edited 7 Years Ago by halluc1nati0n: n/a

Care to explain how?

Since your array is Array[5] , then only words of 4 chars or less will
work because it needs to store the null character. If not then its a bug.

The NULL padding was a requirement for my project. How else should I go about it ?

Use string. And you won't have to worry about any of this.

This article has been dead for over six months. Start a new discussion instead.