Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

C++ String Problem


  • Please log in to reply

#1
comanighttrain

comanighttrain

    Member

  • Member
  • PipPipPip
  • 553 posts
Hey guys,

Iv been programming in C++ for about two years. I was taught using borland 5.5, so maybe iv been spared some of the perils of C programming.

Recently iv been trying to write opensource programs (eventually for linux), i was writing a test harness when i hit upon this problem.

I try to concatenate two strings and the program crash's.

heres the code :
----------------------------------------------------------------------------------------------
{

char disk, partition[4], fileSys[10];
char *command = "mount ";

cout << "\nPlease enter the location of the disk on the IDE cable( 0 - 3 ) >>";
cin >> disk;
cout << "\nEnter partition number (1 - 100) >>";
cin >> partition;
cout << "\nEnter the file System >>";
cin >> fileSys;

switch (int (disk))
{
case '0' : disk = 'a';
break;
case '1' : disk = 'b';
break;
case '2' : disk = 'c';
break;
case '3' : disk = 'd';
break;
default : cout << "Invalid position";
break;
}
strcat(command, fileSys);
cout << "Final Command : " << command;

}
-----------------------------------------------------------------------------------------------

I have tried with both the old string.h header and the newer cstring and it still crashes w/o explaination.

Im using the dev-C++ IDE and i think it uses the mingW compiler, iv tried both the stable and new beta and still it crashes out. I can use the command line borland compiler, but i hate using it due to it being slow and trouble some when you have long path names.

Anyone got any ideas?
Dave
  • 0

Advertisements


#2
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
try this:

//char *command = "mount ";
char command[] = "mount ";

it's still not right - there's not enough memory allocated for command.

try this #2:
char disk, partition[4], fileSys[10], command[16];
//char *command = "mount ";
//char mount[] = "mount ";
strcpy(command, "mount ");

Edited by bdlt, 18 August 2005 - 07:08 PM.

  • 0

#3
comanighttrain

comanighttrain

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 553 posts
thanks man, the first solution worked and is more suitable (command could theoretically be over 100 characters long, but usually is less than 20).

I tried the original solution in both Borland turbo 5.5 and GCC(Linux) and it worked worked any, apart from a segmentation fault in linux but thats another story all together i think.

If you arnt too busy helping other people, could you give me an explaination or recommend some reading?

Thanks alot!
  • 0

#4
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
I have time. let me search for a good reference.

here's what happened:

command was only allocated 5 bytes of memory
when strcat() is executed more than 5 bytes are written into the location of command, overwriting other memory. if you display 'fileSys' after strcat() you will see that it is being overwritten. the 1st try appears to work, but still has a problem.

make sense?
  • 0

#5
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
here are a couple of memory allocation links:

http://www.codersour...allocation.aspx

http://www.cplusplus...ial/tut3-4.html

you might want to run try #2 on the linux system and allow for 100 or more characters for command.
  • 0

#6
comanighttrain

comanighttrain

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 553 posts
thats great,
i was really stuck:)
thanks alot!
  • 0

#7
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
great! does it work on linux too?
  • 0

#8
comanighttrain

comanighttrain

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 553 posts
i havnt had a chance to work out the bugs, but it gave me a segmentation fault when i run it
-----------------------------------------------------------------------------------------------
void mountManControl()
{
ofstream output("C:/linuxproj/lens/testharness/log", ios::app);

char disk[1], partition[4], fileSys[10];
char command[] = "mount ";

cout << "\nPlease enter the location of the disk on the IDE cable( 0 - 3 ) >>";
cin >> disk;
cout << "\nEnter partition number (1 - 100) >>";
cin >> partition;
cout << "\nEnter the file System >>";
cin >> fileSys;

switch (int (disk[0]))
{
case '0' : disk[0] = 'a';
break;
case '1' : disk[0] = 'b';
break;
case '2' : disk[0] = 'c';
break;
case '3' : disk[0] = 'd';
break;
default : cout << "Invalid position";
break;
}
strcat(command, fileSys);
strcat(command, " /dev/hd");
strcat(command, disk);
strcat(command, partition);
strcat(command, " /root/home/mounted");

cout << "Final Command : " << command;
output << "\n" << command;
system("Pause");
-----------------------------------------------------------------------------------------------



im not sure where in the code the fault is coming from, but at a guess i think its the file stream (i had to change the stream to "/mnt/hda1/linuxproj/lens/testharness/log" so it goes to the right place, but its still wrong.

I have just realised the system call might not be portable either:S.
  • 0

#9
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
char command[] = "mount ";

there is not enough memory allocated for command in the line above. please try the following before making any other changes to the code.


//char disk[1], partition[4], fileSys[10];
//char command[] = "mount ";

char disk, partition[4], fileSys[10], command[256];
strcpy(command, "mount ");


if the segmentation fault still occurs, then add debug code before and after the ofstream output() line:
cout.flush() << "\n ok before ofstream output() ";
ofstream output("C:/linuxproj/lens/testharness/log", ios::app);
cout.flush() << "\n ok after ofstream output() ";

  • 0

#10
comanighttrain

comanighttrain

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 553 posts
Ok, iv got it working in Linux, but, oh yes, theres an anomoly. This is what iv got :

------------------------------------------------Code-----------------------------------------------------------------------
{
ofstream output("/mnt/sda1/Linuxproj/Lens/testharness/log", ios::app);

char disk[4], partition[4], fileSys[10], command[256];
strcpy(command, "mount ");

cout << "\nPlease enter the location of the disk on the IDE cable( 0 - 3 ) :";
cin.getline(disk,4); //this is ignored
cin.getline(disk,2); //this isnt...? What the...?

cout << "\nEnter partition number (1 - 100) :";
cin.getline(partition,4);

cout << "\nEnter the file System :";
cin.getline(fileSys,10);

switch (int (disk[0]))
{
case '0' : disk[0] = 'a';
break;
case '1' : disk[0] = 'b';
break;
case '2' : disk[0] = 'c';
break;
case '3' : disk[0] = 'd';
break;
default : cout << "Invalid position";
break;
}
strcat(command, fileSys);
strcat(command, " /dev/hd");
strcat(command, disk);
strcat(command, partition);
strcat(command, " /root/home/mounted");

cout << "Final Command : " << command;
output << "\n" << command;
}
------------------------------------------------Code--------------------------------------------------------------------

Cin >> has a problem with memory allocation involving strings, so we use cin.getline(string,max number of entries).

but the first cin.getline is ignored for some reason, but apart from that nuisance, the program works ok.

Any ideas about why the first cin.getline is ignored?
  • 0

#11
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
you might look at cin.ignore().

http://www.augustcou...ial/iotips.html
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP