Operating System - HP-UX
1833059 Members
2532 Online
110049 Solutions
New Discussion

Re: program dumping core file

 
Sup
Advisor

program dumping core file

Hi,

I have small program

******Working fine******************

main()
{
char *src="One";
char *dest="Two";

while ( src== NULL )
*dest++=*src++;
}

**********************************


******Dumping core******************

main()
{
char *src="One";
char *dest="Two";

while ( src )
*dest++=*src++;
}

**********************************

Anybody know diff.

Thanx
Raj
7 REPLIES 7
A. Clay Stephenson
Acclaimed Contributor

Re: program dumping core file

Hi:

Even your working versions is NOT working; it's simply doing nothing.

src is never NULL so the loop never executes.

main()
{
char *src="One";
char *dest="Two";

while ( src== NULL )
*dest++=*src++;
}

**********************************


******Dumping core******************

In this case, src is NEVER NULL, so it executes forever (or until a memory boundary is voilated and you dump core).

main()
{
char *src="One";
char *dest="Two";

while ( src )
*dest++=*src++;
}

You probably want something like this:
char *src="One",dest[80];
char *dptr = dest;

while (*src != '\0')
{
*dptr = *src;
++dptr;
++src;
}
*dptr = '\0';

This is not yet fully robust but it does indicate your problem.

If it ain't broke, I can fix that.
Sridhar Bhaskarla
Honored Contributor

Re: program dumping core file

Not a programmer but with a little knowledge of shell programming, it looks like the first case is returning a "false" in the while loop causing it to break. In the second case, it's true resulting a never ending loop causing a segment violation.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Ceesjan van Hattum
Esteemed Contributor

Re: program dumping core file

Clay Stephenson is correct. The inside of a while-loop has to be a boolean: TRUE or FALSE; i.e. equals the exit value of a test.
It is also better to use a ending space for these things, like an array of 20 characters. This way, you will never have these anoying memorydumps, because it cannot take all memory; only up until the end of the array.
Memory-errors like this, consuming all memory will not only corrupt your memoryspace, but can have some effects on other programs as well, using the same available space...
So take care about your memorymanagement. Read some manuals about 'malloc' as well.

Regards,
Ceesjan
Sup
Advisor

Re: program dumping core file

Whoops..
Thanx for the inputs.

My problems resolved after putting
while ( *src != NULL )

I wanted to know why this is not working

*src="One" /*Last character is NULL char*/

while ( src != NULL )
*dest++=*src++; /* Moves to last NULL char after third iteration So "src" should be == NULL */

The following cases works

1> while ( *src != NULL )
*dest++=*src++;

2> *src=NULL;
*dest="Two";
while ( src != NULL )
*dest++=*src++;

3> *src=NULL;
*dest="Two";
while ( *src != NULL )
*dest++=*src++;


4> *src='\0';
*dest="Two";
while ( src != NULL )
*dest++=*src++;


5> *src='\0';
*dest="Two";
while ( *src != NULL )
*dest++=*src++;


Just curious...

Thanx











Deepak Extross
Honored Contributor

Re: program dumping core file

Errrm, not exactly.
You have declared "src" to be a char pointer, and so it will hold a memory address. You can verify this by printing out src like so:
printf("src=%ld\n", src);

So (src== NULL) will never be true. Instead, you have to check the value of *src, (the contents of the memory address src points to) as you have discovered.

Hope this clarifies the matter.
H.Merijn Brand (procura
Honored Contributor

Re: program dumping core file

Please asign points to above messages. The answers are good, explainatory, and helpful

N/A for me please
Enjoy, Have FUN! H.Merijn
Sup
Advisor

Re: program dumping core file

Hi,

This is what happening...

1> *src=NULL -> No memory is allocated/reserved by compiler for NULL character so address of
src is always 0.

So ( src== NULL ) becames true, It is
equivalent to
( 0 == 0 )

2> *src="One" -> Here 4th character is NULL and it is inserted by compiler. It has physical address.

content A B C \0
Address 100 101 102 103

So ( src== NULL ) Never becomes true, It is
equivalent to
( 103 == 0 )

Thanx