473,320 Members | 1,802 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,320 software developers and data experts.

why this does n't works

Hi all,

why

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
char *t[5]; /*= {"BASIC"};*/

strcpy(t[0],"BASIC");
printf("....%s",t[0]);

return EXIT_SUCCESS;
}
/* i am getting segmentation violation in lcc compiler*/

is not working whereas

int main(void)
{
char *t[5] = {"BASIC"};

/*strcpy(t[0],"BASIC");*/
printf("....%s",t[0]);

return EXIT_SUCCESS;
}
is working ?

Does this mean that while using strcpy the char pointer should
always be initialized ...?????
Jun 27 '08 #1
8 1159
aa*****@gmail.com said:
Hi all,

why

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
char *t[5]; /*= {"BASIC"};*/
This defines, and reserves storage for, an array of five char *, but
doesn't point them anywhere and doesn't reserve any storage for them to
point at.
strcpy(t[0],"BASIC");
t[0] exists, but doesn't point anywhere special, so you're trashing memory
you don't own.
printf("....%s",t[0]);

return EXIT_SUCCESS;
}
/* i am getting segmentation violation in lcc compiler*/
Deep shock.
>
is not working whereas

int main(void)
{
char *t[5] = {"BASIC"};

/*strcpy(t[0],"BASIC");*/
printf("....%s",t[0]);

return EXIT_SUCCESS;
}
is working ?
No, it just isn't breaking in the way you were hoping it to break.
Undefined behaviour *doesn't* necessarily mean "it will crash".

Does this mean that while using strcpy the char pointer should
always be initialized ...?????
Gee, ya think?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #2
i guess the problem arises because you have not allocated any memory
for the string. Probably you can try this because it seems to work for
me:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(void)
{
char *t[5]; /*= {"BASIC"};*/
int i;
i = strlen("BASIC");
t[0] = malloc(i * sizeof(char));
strcpy(t[0],"BASIC");
printf("....%s",t[0]);

return EXIT_SUCCESS;
}
Jun 27 '08 #3
pereges said:
i guess the problem arises because you have not allocated any memory
for the string. Probably you can try this because it seems to work for
me:
Doesn't make it right.
>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
Is your space bar broken?
int main(void)
{
char *t[5]; /*= {"BASIC"};*/
int i;
i = strlen("BASIC");
i is 5
t[0] = malloc(i * sizeof(char));
5 * sizeof(char) is 5 (by definition)
strcpy(t[0],"BASIC");
this call attempts to copy SIX bytes, but you only reserved five (assuming
the malloc worked, which you didn't check) - so you've written into memory
you don't own, and the behaviour of your program is now undefined.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #4
On Apr 16, 11:16 pm, Richard Heathfield <r...@see.sig.invalidwrote:
pereges said:
i guess the problem arises because you have not allocated any memory
for the string. Probably you can try this because it seems to work for
me:

Doesn't make it right.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

Is your space bar broken?
int main(void)
{
char *t[5]; /*= {"BASIC"};*/
int i;
i = strlen("BASIC");

i is 5
t[0] = malloc(i * sizeof(char));

5 * sizeof(char) is 5 (by definition)
strcpy(t[0],"BASIC");

this call attempts to copy SIX bytes, but you only reserved five (assuming
the malloc worked, which you didn't check) - so you've written into memory
you don't own, and the behaviour of your program is now undefined.
Yes, I think I forgot about the null character.
Jun 27 '08 #5

pereges <Br*****@gmail.comwrites:
i guess the problem arises because you have not allocated any memory
for the string. Probably you can try this because it seems to work for
me:
Seems to. You are lucky. I have listed some issues below. Hope it helps.
>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(void)
{
char *t[5]; /*= {"BASIC"};*/
int i;
i = strlen("BASIC");
t[0] = malloc(i * sizeof(char));
strcpy(t[0],"BASIC");
printf("....%s",t[0]);

return EXIT_SUCCESS;
}
Your code has errors. I thought Id try the output of "splint" to make
the diagnosis a little more consistent.

Since I didn't see the previous post I wonder why you are creating an
array of 5 character pointers? or did you think the 5 was the length of
BASIC? It's not clear in the absence of any comment what you really
intend.

From splint(*) :

test2.c:9:3: Assignment of size_t to int: i = strlen("BASIC")

In other words strlen returns a size_t and not an int. Personally when I
write C I tend to still use "int" where I KNOW the string is short
enough. But its not "correct". Use a size_t.

Also you only malloc enough for the "BASIC" part and not for the
trailing zero to indicate "end of string". Look up "strings" in C. Read
the manual for strlen and strcpy. This is one of the most basic but
important things to know in C.

More from splint:

test2.c:10:34: Parameter to sizeof is type char: sizeof(char)
Operand of sizeof operator is a type. (Safer to use expression, int *x =
sizeof (*x); instead of sizeof (int).) (Use -sizeoftype to inhibit warning)

Personally, for me, this line would just be

t[0] = malloc(strlen("BASIC")+1);

But even then we have a problem that something like splint can tell us
no matter how much we think we know best:

strcpy (t[0], ...)
A possibly null pointer is passed as a parameter corresponding to a formal
parameter with no /*@null@*/ annotation. If NULL may be used for this
parameter, add a /*@null@*/ annotation to the function parameter declaration.

Why is this? It's because you did not check for malloc returning NULL,
but that is another story and down to good programming practices.

So following that hint we can do:

char *p = malloc(strlen("BASIC")+1);
if(p==NULL)
return EXIT_FAILURE;
t[0]=p;
strcpy(t[0],"BASIC");
(*)(Splint is a tool for statically checking C programs for
security vulnerabilities and coding mistakes. With minimal effort,
Splint can be used as a better lint. If additional effort is invested
adding annotations to programs, Splint can perform stronger checking
than can be done by any standard lint. See http://www.splint.org/ ).

Jun 27 '08 #6
Richard Heathfield wrote:
>
aa*****@gmail.com said:
[... snip first version which SEGV's ...]

int main(void)
{
char *t[5] = {"BASIC"};

/*strcpy(t[0],"BASIC");*/
printf("....%s",t[0]);

return EXIT_SUCCESS;
}
is working ?

No, it just isn't breaking in the way you were hoping it to break.
Undefined behaviour *doesn't* necessarily mean "it will crash".
Isn't this second version perfectly well-defined? Doesn't the line:

char *t[5] = {"BASIC"};

mean:

t is an array of 5 character pointers, the first one points
to the literal "BASIC", and the rest are NULL

and so the reference to t[0] means the string "BASIC"?
Does this mean that while using strcpy the char pointer should
always be initialized ...?????

Gee, ya think?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Jun 27 '08 #7
On Wed, 16 Apr 2008 16:44:54 -0400, Kenneth Brody
<ke******@spamcop.netwrote:
>Richard Heathfield wrote:
>>
aa*****@gmail.com said:
[... snip first version which SEGV's ...]
>
int main(void)
{
char *t[5] = {"BASIC"};

/*strcpy(t[0],"BASIC");*/
printf("....%s",t[0]);

return EXIT_SUCCESS;
}
is working ?

No, it just isn't breaking in the way you were hoping it to break.
Undefined behaviour *doesn't* necessarily mean "it will crash".

Isn't this second version perfectly well-defined? Doesn't the line:

char *t[5] = {"BASIC"};

mean:

t is an array of 5 character pointers, the first one points
to the literal "BASIC", and the rest are NULL
Yes
>
and so the reference to t[0] means the string "BASIC"?
Yes but it might be better phrased as'"the string literal "BASIC"'.

It is only well defined as long as there is no attempt to update the
characters pointed to by t[0]. Any attempt to do so (as implied by
the commented call to strcpy) invokes undefined behavior.
Remove del for email
Jun 27 '08 #8
Kenneth Brody said:
Richard Heathfield wrote:
>>
<snip>
>Undefined behaviour *doesn't* necessarily mean "it will crash".

Isn't this second version perfectly well-defined?
Yes, it is, and I owe the OP an apology. I misread the code.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #9

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

Similar topics

162
by: Isaac Grover | last post by:
Hi everyone, Just out of curiosity I recently pointed one of my hand-typed pages at the W3 Validator, and my hand-typed code was just ripped to shreds. Then I pointed some major sites...
7
by: Todd Cary | last post by:
I inherited an application where the Flyout works in Firefox but not in IE. My JavaScript background is sparse, so I am not sure how to approach the problem. Any suggestions are welcomed! The...
1
by: Georg Scholz | last post by:
Hello, The class "Control" contains a documented Property "ControlType". So for example, in a form, you can write code like this: Dim c as control set c = me.Controls("textbox1") if...
5
by: rogsonl | last post by:
My computer was moved last week, and the company changed the network groups we work on. As a result, one of the main benefits from Whidbey (database connectivity) no longer works. Situation: 1....
9
by: David Thielen | last post by:
Hi; I am sure I am missing something here but I cannot figure it out. Below I have a program and I cannot figure out why the xpath selects that throw an exception fail. From what I know they...
11
by: minnesotti | last post by:
Hi there, I subscribed to a photographic pictures-hosting website which is heavy on JavaScript. My preferred latest browser Mozilla Firefox does not work with it -- no pictures are displayed and...
14
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it...
3
by: wwwmike | last post by:
For some reasons my HTTP Redirect for 404 errors does not work for certain files like .gif .jpg on IIS Version: 5.1, but it works perfectly with the VisualStudio2005 internal webserver ...
5
by: antonyliu2002 | last post by:
Hi, It looks like so many people are having problems with the javascript submit in firefox. I searched around, but haven't found a solution yet. Mostly, people were saying, try this or try...
1
by: rockdale | last post by:
Hi: I have a web application which runs fine on our production server. But now when I install abother production server, it gets "System.NullReferenceException: Object reference not set to an...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.