how is this working ? 
Author Message
 how is this working ?

Quote:

> Hi
>  I could not understand how the following code works fine

> int func1(char *str) {

>  char *newString;
>  int a ;
>  int b;
>  ....
>  ....
>  ....

>  strcpy(newString, str);// It seems to work , but how ? //

> }
>   there is no memory allocated for newString. But the string is being
> copied.
>  How is this happening ? Is some other memory being overwritten or am I
> overlooking something ?

> thanks
>  Mahesh

We should not allways pull back behind terms like "not defined". What happens

in practise is, that newString is a pointer of type char. It only keeps
an address. As it has a random value since you forgot to initialize it
it points anywhere. Unfortunatelly it points into your programs own
address space. Thus you randomly overwrite something within your own
program. If you are short-term-luckey you overwrite some instructions
which already have been executed and never will be executed again. In
fact this is the worst that can happen. You may happily live a while
with this bug and it randomly strikes later.

To avoid a bug like this you allways should initialize your variables.
In this case to values which force the error condition:

int
func1(char *str)
{
     char *newString = NULL;
     int a = 0,
         b = 0;

     ...

     strcpy(newString, str); /* now will cause a segmentation fault */

     return (0);

Quote:
}

/* Ciao, Toni */


Thu, 03 Feb 2005 01:36:04 GMT  
 how is this working ?

Quote:

> To avoid a bug like this you allways should initialize your
> variables. In this case to values which force the error condition:

> int
> func1(char *str)
> {
>      char *newString = NULL;
>      int a = 0,
>          b = 0;

>      ...

>      strcpy(newString, str); /* now will cause a segmentation fault */

It will cause a segmentation fault if you're lucky.  If you're
unlucky, you won't get anything nearly so clear.  In a
large-model MS-DOS program, for instance, you might overwrite the
interrupt 8 interrupt vector, which will cause a crash in 1/18th
of a second or less, when the next timer tick comes along.  In a
small-model MS-DOS program, you might just overwrite the first
few bytes of the data segment, and then who know what will
happen.
--
"In My Egotistical Opinion, most people's C programs should be indented six
 feet downward and covered with dirt." -- Blair P. Houghton


Thu, 03 Feb 2005 01:43:08 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Am I working on the right lines... (long)

2. ifstream won't work or am i dumb

3. I am new to programming and am lost

4. how good am I? Am I Good Enough????

5. Hmm, Am I the only guy working with HttpModules?

6. Help, I am new to cprograming and can't figure out why this does not work right. - mail.cpp (0/1)

7. Help. I am unable to have WebClient working in c# .

8. this code works on SGI does not work on linux

9. reinstall IIS - how to get ASPX files to work again - ASP does work

10. Help, I am new to cprograming and can't figure out why this does not work right. - mail.cpp (0/1)

11. Help, I am new to cprograming and can't figure out why this does not work right. - mail.cpp (1/1)

12. C++/ATL/ADO - Intellisense not working (statement completion options) not working

 

 
Powered by phpBB® Forum Software