0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Offtopic comment:
Just to add one offtopic comment add the code block when having codes in the post
0
chopin17's Avatar, Join Date: Apr 2007
Go4Expert Member
Quote:
You're code is incomplete. It means nothing. There is no such thing as a 'dangling' pointer, despite all the protestations of various gurus that you might feel compelled to kneel before, and kiss the hems of their garments. There are valid pointers and invalid pointers. It baffles me that you cannot understand that valid information can be preserved, despite the death of its originator.
First thing first : If it baffles you to answer something then simply don't answer it !
And about my code, is not incomplete, is just a Code Snippet focused on what I'm interesting for....
Is just a simple question ::::::
Code:
#include <stdio.h>

char *createstring()
{
    char line[1000];
    gets (line);
    return line;
}

int main()
{
    char *buffer;
    buffer = createstring();
    return 0;
}
You like that better ???

line points to 0xSomeAddressHavingData
createstring() exits
line points to 0xNothing
buffer points to line points to nothing

how you call this ??
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
First of all, if you can't hack a little criticism, you're getting into the wrong business. If you refuse to think, you're definitely getting into the wrong business.

I call what you posted incorrect code. It also doesn't match your snippit, which makes your snippet no good for explaining the problem. The code that you show is full of woes. You cannot return the address of the array and do any good because THE ARRAY goes away. You have the address of where it was, which, because it is an autovariable, is useless information. When you use malloc, the array exists on the heap. The pointer to it is an autovariable, so you will lose THAT, but the array remains. If you return the value of the pointer to main, main now has the heap address stored in ITS autovariable. It doesn't matter that you lost the one in 'createstring'.

Further, do not use 'gets'. It puts no constraint on how many characters you are willing to accept. If the user enters 1001, which is merely half of a full screen, your program is toast. Use 'fgets'.

Your statement, 'line points to 0xNothing' is not correct. line points to precisely the memory that it pointed to before, but that memory NO LONGER CONTAINS LINE, or, at least, won't in the very near future. Get that data area from the heap, and it'll be there until you free it.

I recommended that you study autovariables and scope. Clearly, you still need to do that. You also need to read this.

There is a library function that performs your createstring function. It is called 'strdup'.
Code:
#include <stdio.h>

char *createstring()
{
    char *line = (char *) malloc (1000);
   if (line == NULL) return NULL;  // ERROR! YOUR REQUEST WAS DENIED.
   return fgets (line, 1000, stdin);
}

int main()
{
    char *buffer;
    buffer = createstring();
    if (buffer == NULL) return 1;// EITHER MALLOC OR FGETS FAILED
    // Use the buffer
    ...
    ...
    free (buffer);
    return 0;
}
0
chopin17's Avatar, Join Date: Apr 2007
Go4Expert Member
I don't know If i ask clever questions but sure this one ->http://cboard.cprogramming.com/showthread.php?t=88784
is called a clever answer..
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Yes, that's a good answer. You got the same answer here. It seems likely that you are going to pay no attention to either of them. You might want to get your hand fitted for a mop, because janitorial duties might well be your future.

If you ever dropped a competing forum's link on MY forum, I would warn you once, then ban you.
0
chopin17's Avatar, Join Date: Apr 2007
Go4Expert Member
Be sure that my future will be better than yours...
At least I am not so rude like you.
Oh... .and about that ban thing .............
I think I ban my self....

Bye !
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by DaWei
If you ever dropped a competing forum's link on MY forum, I would warn you once, then ban you.
We would prefer banning the forum also.
0
bughunter2's Avatar, Join Date: Feb 2007
Go4Expert Member
A dangling pointer does not exist, pointers are valid or not. If a pointer is invalid, it means it does not point to a valid block of memory. Which in turn means you forgot to allocate memory for it or did not point it to a particular block of memory.
0
bughunter2's Avatar, Join Date: Feb 2007
Go4Expert Member
Oops I see there was a second page which I didn't read. I tried to reply to the last post of the previous page.