473,385 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

strcat implementation?

Never mind my last post about pointer subtraction, I traced the code and
found the offender to be strcat.

char call[] = "del ";
system(strcat(strcat(call, del->table_name), ".tab"));

After this line, trying to dereference del results in page faults or
garbage. This makes me wonder how strcat is implemented, in terms of what it
actually does with the arguments. Seems like it actually moves the pointer.
Anyone know?
Nov 14 '05 #1
5 5533
Kevin C. wrote:
Never mind my last post about pointer subtraction, I traced the code and
found the offender to be strcat.

char call[] = "del ";
system(strcat(strcat(call, del->table_name), ".tab"));

You have declared call[] as an array of 5 chars; your strcat call has
written beyond the array so anything is possible.

Nov 14 '05 #2
On Fri, 07 May 2004 06:56:56 GMT, "Kevin C." <no****@fake.com> wrote:
Never mind my last post about pointer subtraction, I traced the code and
found the offender to be strcat.

char call[] = "del ";
system(strcat(strcat(call, del->table_name), ".tab"));

After this line, trying to dereference del results in page faults or
garbage. This makes me wonder how strcat is implemented, in terms of what it
actually does with the arguments. Seems like it actually moves the pointer.
Anyone know?


In addition to the other comments, if you don't know much about
pointers and arrays (and it seems you don't), then you might find it
easier to use the string class from the <string> header. e.g.

std::string call = "del ";
call += del->table_name;
call += ".tab";

There are no memory management issues to be concerned with with
std::string - it handles its own memory allocation, freeing the memory
when the string goes out of scope. That will be slower than the array
approach, but this is only a concern in a tight inner loop generally.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Nov 14 '05 #3
"Kevin C." <no****@fake.com> wrote in message news:<Yq****************@newssvr27.news.prodigy.co m>...
Never mind my last post about pointer subtraction, I traced the code and
found the offender to be strcat.

char call[] = "del ";
system(strcat(strcat(call, del->table_name), ".tab"));

After this line, trying to dereference del results in page faults or
garbage. This makes me wonder how strcat is implemented, in terms of what it
actually does with the arguments. Seems like it actually moves the pointer.
Anyone know?


The array call is allocated 5 bytes above ('d' 'e' 'l' ' ' '\0'). Any
attempt
to call strcat with it as the target will overwrite its array
boundries, unless you are attempting to append the empty string.

You could try to fix this as a hack by having
char call[100] = "del ";
Assuming that 100 will always hold what you need. Sadly, such
assumptions turn out to be right only until they are not. Maybe the
assumption was right when the code was written, etc. The correct way
to fix it is to figure out how much room you need for the 3 strings
you are putting together (not forgetting to add 1 for the terminating
NUL), dynamically allocate it, put it all together, and
issue your system command.

const char *fmt = "del %s.tab";
/* allocates 1 byte more than needed. whatever */
char *syscmd = malloc(strlen(fmt) + strlen(del->table_name) + 1);

if (syscmd == NULL) {
exit(EXIT_FAILURE);
}
else {
sprintf(syscmd, fmt, del->table_name);
system(syscmd);
free(syscmd);
}
Nov 14 '05 #4
On Fri, 7 May 2004, Kevin C. wrote:
Never mind my last post about pointer subtraction, I traced the code and
found the offender to be strcat.

char call[] = "del ";
system(strcat(strcat(call, del->table_name), ".tab"));

After this line, trying to dereference del results in page faults or
garbage. This makes me wonder how strcat is implemented, in terms of what it
actually does with the arguments. Seems like it actually moves the pointer.
Anyone know?


You are over-thinking this. Just read the definition of strcat. You have
declared the array call to be 5 characters long. The function strcat will
store everything in the call array. If the resulting string is greater
than 5 characters (and it is) then you are going to have undefined
behaviour. This is true for 100% of the implementations of strcat. You
don't need to know how your compiler has implemented strcat to know this.

Try:

char *call;
char tmp1[] = "del ";
char tmp2[] = ".tab";
int len = sizeof tmp1 + sizeof tmp2 + strlen(del->table_name);

if((call = malloc(len)) == NULL) exit(EXIT_FAILURE);
sprintf(call, "%s%s%s", tmp1, del->table_name, tmp2);
system(call);

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@whitehouse.gov
Nov 14 '05 #5
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

tom_usenet wrote:
| On Fri, 07 May 2004 06:56:56 GMT, "Kevin C." <no****@fake.com> wrote:
|
|
|>Never mind my last post about pointer subtraction, I traced the code and
|>found the offender to be strcat.
|>
|>char call[] = "del ";
|>system(strcat(strcat(call, del->table_name), ".tab"));
|>
|>After this line, trying to dereference del results in page faults or
|>garbage. This makes me wonder how strcat is implemented, in terms of
what it
|>actually does with the arguments. Seems like it actually moves the
pointer.
|>Anyone know?
|
|
| In addition to the other comments, if you don't know much about
| pointers and arrays (and it seems you don't), then you might find it
| easier to use the string class from the <string> header. e.g.
|
| std::string call = "del ";
| call += del->table_name;
| call += ".tab";
|
| There are no memory management issues to be concerned with with
| std::string - it handles its own memory allocation, freeing the memory
| when the string goes out of scope. That will be slower than the array
| approach, but this is only a concern in a tight inner loop generally.
|
| Tom

Ummmm, doesn't work in C.

Ross
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFAm6q99bR4xmappRARAgI9AKCW55sbN5ZHcETo4DE7tO N7mp9uwACfQJ2/
KhsDGc+zvbyMtNboClOiRf8=
=bdRS
-----END PGP SIGNATURE-----
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jon | last post by:
using Borland Compiler. x = "this is a string" strcat(x,x) strcat(x,x) will produce "this is a stringthis is a stringthis is a stringthis is a stringt"
18
by: Ian Stanley | last post by:
Hi, Continuing my strcat segmentation fault posting- I have a problem which occurs when appending two sting literals using strcat. I have tried to fix it by writing my own function that does the...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
6
by: santosh | last post by:
The following code is giving run-time error..... char buf = "Hello" ; strcat(buf,buf) ; Second line is giving the run-time error. Any comments please............
87
by: Robert Seacord | last post by:
The SEI has published CMU/SEI-2006-TR-006 "Specifications for Managed Strings" and released a "proof-of-concept" implementation of the managed string library. The specification, source code for...
28
by: Mahesh | last post by:
Hi, I am looking for efficient string cancatination c code. I did it using ptr but my code i think is not efficient. Please help. Thanks a lot
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.