LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-29-2008, 05:55 PM   #1
mkrems
Member
 
Registered: Jan 2008
Distribution: Ubuntu
Posts: 31

Rep: Reputation: 15
string operations in c


In the following code, the minimum size for cOut is 25 as seen. However, the length of cOut by the time this code is run is 17, which would make me think that the minimum size for cOut should be 18. Anything lower than 25 causes a segmentation fault. Does the function strcat need some extra elements in the string for intermediate operations or something?

char *cTemp, cOut[25] = "rho_of_t_", cRight[5];

iRight = "1512"

// convert integer to string
sprintf(cRight, "%d", iRight);
// concatenate strings
strcat(cOut, cRight);
strcat(cOut, ".dat");



Thanks!
 
Old 04-29-2008, 06:27 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
char * cOut[25] is an array-of-ptrs-to char (25 to be exact)
char cOut[25] is an array-of-char (25 'slots' long) aka a c 'string' if you terminate correctly (ie '\0')
iirc, if you initialise a ptr-to-char during a declaration, that makes it a constant ?
It's been a while and I don't have my books handy.
 
Old 04-29-2008, 07:04 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
In your description I think you confuse minimum with maximum.
I don't understand the line
Code:
iRight = "1512"
did you mean
Code:
int iRight = 1512;
 
Old 04-29-2008, 07:17 PM   #4
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
sprintf(cRight, "%d", iRight);

Whats that line for? iRight has been given a string value immediately before, yet your trying to convert it to a string? (sprintf can not know that iRight is not a number, since you told it to expect a number, it will interpret the pointer as a number). There is also no garentee that the output will be 4 chars (for a total of 5 in the string). This leads to memory corruption in the surrounding data, which can easily lead to messed-up data that will cause a segfault.

Your using unsafe functions, in a unsafe mannor, and the requirement of no-less then 25 for the size is probably due to the program corrupting itself, and with a value of 25 it simply has more room to corrupt itself before it starts overriding memory that will cause a segfault.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash operations with string alenD Linux - Software 6 10-17-2008 02:43 PM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-20-2007 11:14 PM
The method contains(String) is undefined for the type String sampada Programming 1 06-30-2007 10:02 AM
Rewrite rule with query string in the pattern string basahkuyup Linux - Newbie 2 10-17-2006 02:06 AM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration