Thread: [Dev-C++] strcpy x pointer array
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <and...@su...> - 2003-03-06 23:28:07
|
1. Please consider the following piece of code
#include <stdio.h>
#include <string.h>
...
int main()
{
char *vect[100];
...
/* line x */ vect[3]="text";
...
/* line y */ strcpy(vect[3], "text");
...
}
...
2. Both commands in lines x and y don´t gererate any compilation errors;
3. The command in line x works fine;
4. Why the command in line y generates an execution error interrupting the
execution of
the program?
Regards
Andre
|
|
From: Per W. <pw...@ia...> - 2003-03-06 23:44:34
|
char *vect[100] means that you have an array of 100 character pointers.
the first assignment assigns the address of a string, i.e. a character
pointer to the 4th element of your array. That's ok.
Your line 'y' on the other hand tries to copy data to the pointer already
stored in vect[3].
If you run without line x, vect[3] will point to random address and the
copy will most probably fail misserably.
If you run with line x, vect[3] will point to a constant, i.e.
write-protected (for most environments at least), text string that your
compiler has created. The strcpy() call will then fail misserably when it
tries to overwrite the write-protected memory where the first "text"
string is stored.
You might do:
vect[3] =3D strdup("text");
...
free(vect[3]);
strdup() will first allocate a block of memory large enough for the
string. Then it will copy "text" to the new memory block. Then it will set
the character pointer vect[3] to point at the newly allocated memory.
However, to avoid memory leaks, you must then release the memory that
strdup() allocated, i..e calling free() on it.
/Per W
On Thu, 6 Mar 2003, Andr=E9 Mac=E1rio Barros wrote:
> 1. Please consider the following piece of code
>
> #include <stdio.h>
> #include <string.h>
> ...
> int main()
> {
> char *vect[100];
>
> ...
> /* line x */ vect[3]=3D"text";
> ...
> /* line y */ strcpy(vect[3], "text");
> ...
> }
> ...
>
> 2. Both commands in lines x and y don=B4t gererate any compilation errors=
;
>
> 3. The command in line x works fine;
>
> 4. Why the command in line y generates an execution error interrupting th=
e
> execution of
> the program?
>
> Regards
> Andre
>
>
>
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: Etnus, makers of TotalView, The debugg=
er
> for complex code. Debugging C/C++ programs can leave you feeling lost and
> disoriented. TotalView can help you find your way. Available on major UNI=
X
> and Linux platforms. Try it free. www.etnus.com
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Zodiaq <zo...@ac...> - 2003-03-06 23:54:13
|
Hello André and all Dev-C++ users,
Friday, March 7, 2003, 12:27:42 AM, André wrote as follows:
AMB> 1. Please consider the following piece of code
AMB> #include <stdio.h>
AMB> #include <string.h>
AMB> ...
AMB> int main()
AMB> {
AMB> char *vect[100];
AMB> ...
AMB> /* line x */ vect[3]="text";
AMB> ...
AMB> /* line y */ strcpy(vect[3], "text");
AMB> ...
AMB> }
AMB> ...
AMB> 2. Both commands in lines x and y don´t gererate any compilation errors;
AMB> 3. The command in line x works fine;
You are asigning a new value for pointer at vect[3] which is ok...
AMB> 4. Why the command in line y generates an execution error interrupting the
AMB> execution of
AMB> the program?
You are copying string "text" to const value "text" - pointer from
line x points at this...
Before line y, first get some space to hold new data:
vect[3]= new char[ strlen( "text" )];
now strcpy should work fine
--
regards, Zodiaq
|
|
From: Per W. <pw...@ia...> - 2003-03-07 00:46:53
|
How about vect[3] =3D new char[strlen("text")+1];
/Per W
On Fri, 7 Mar 2003, Zodiaq wrote:
> Hello Andr=E9 and all Dev-C++ users,
>
> Friday, March 7, 2003, 12:27:42 AM, Andr=E9 wrote as follows:
>
> AMB> 1. Please consider the following piece of code
>
> AMB> #include <stdio.h>
> AMB> #include <string.h>
> AMB> ...
> AMB> int main()
> AMB> {
> AMB> char *vect[100];
>
> AMB> ...
> AMB> /* line x */ vect[3]=3D"text";
> AMB> ...
> AMB> /* line y */ strcpy(vect[3], "text");
> AMB> ...
> AMB> }
> AMB> ...
>
> AMB> 2. Both commands in lines x and y don=B4t gererate any compilation e=
rrors;
>
> AMB> 3. The command in line x works fine;
>
> You are asigning a new value for pointer at vect[3] which is ok...
>
> AMB> 4. Why the command in line y generates an execution error interrupti=
ng the
> AMB> execution of
> AMB> the program?
>
> You are copying string "text" to const value "text" - pointer from
> line x points at this...
>
> Before line y, first get some space to hold new data:
> vect[3]=3D new char[ strlen( "text" )];
> now strcpy should work fine
>
>
> --
> regards, Zodiaq
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: Etnus, makers of TotalView, The debugg=
er
> for complex code. Debugging C/C++ programs can leave you feeling lost and
> disoriented. TotalView can help you find your way. Available on major UNI=
X
> and Linux platforms. Try it free. www.etnus.com
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Ioannis V. <no...@ho...> - 2003-03-07 02:36:25
|
-------Original Message-------=0D
=0D
From: Andr=E9 Mac=E1rio Barros=0D
Date: =D0=E1=F1=E1=F3=EA=E5=F5=DE, 7 =CC=DC=F1=F4=E9=EF=F2 2003 1:30:46 =F0=
=EC=0D
To: Dev-Cpp-Users=0D
Subject: [Dev-C++] strcpy x pointer array=0D
=0D
1. Please consider the following piece of code=0D
=0D
#include <stdio.h>=0D
#include <string.h>=0D
=2E..=0D
int main()=0D
{=0D
char *vect[100];=0D
=0D
=2E..=0D
/* line x */ vect[3]=3D"text";=0D
=2E..=0D
/* line y */ strcpy(vect[3], "text");=0D
=2E..=0D
}=0D
=2E..=0D
=0D
2. Both commands in lines x and y don=B4t gererate any compilation errors=
;=0D
=0D
3. The command in line x works fine;=0D
=0D
4. Why the command in line y generates an execution error interrupting th=
e=0D
execution of=0D
the program?=0D
=0D
=0D
=3D=3D> What you did is similar to:=0D
=0D
const char *p=3D"text";=0D
=0D
strcpy(p, "text");=0D
=0D
=0D
=3D=3D> After some thinking you will understand why & what it means.=0D
=0D
=0D
=0D
Ioannis Vranos=0D
=0D
* Programming pages: http://www.noicys.freeurl.com=0D
* Alternative URL: http://run.to/noicys=20 |