Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Help needed

spurs01spurs01 Member Posts: 44
#include
#include
#include

int main(void)
{
char *p2;

/*S1*/

p2 = malloc(strlen(pl) + 1);
if(p2 == NULL)
{
printf("malloc failed
");
exit(1);
}

strcpy (p2,pl); /*S2*/
free(p2); /*S3*/
return 0;
}

Can anyone explain to me what is the contents of memory at point S1,S2&S3?

i could not understand wat is it??

Comments

  • whoiewhoie Member Posts: 672
    : #include
    : #include
    : #include
    :
    : int main(void)
    : {
    : char *p2;
    :
    : /*S1*/

    The value of the pointer 'p2' is indeterminate.


    : p2 = malloc(strlen(pl) + 1);
    : if(p2 == NULL)
    : {
    : printf("malloc failed
    ");
    : exit(1);
    : }
    :
    : strcpy (p2,pl); /*S2*/

    The value of 'p2' is a pointer pointing to enough storage to contain strlen(pl) + 1 characters. The value(s) of that storage is indeterminate.

    After the call, 'p2' still points to the same storage, but the values of that memory are whatever 'pl' was.


    : free(p2); /*S3*/

    After the call to free, the value of the pointer 'p2' is indeterminate.

    indeterminate -

    3.17.2
    1 indeterminate value
    either an unspecified value or a trap representation

    unspecified value -

    3.17.3
    1 unspecified value
    valid value of the relevant type where this International Standard imposes no requirements on which value is chosen in any instance

    2 NOTE An unspecified value cannot be a trap representation.


    trap representation -

    6.2.6.1 General

    5 Certain object representations need not represent a value of the object type. If the stored value of an object has such a representation and is read by an lvalue expression that does not have character type, the behavior is undefined. If such a representation is produced by a side effect that modifies all or any part of the object by an lvalue expression that does not have character type, the behavior is undefined.41) Such a representation is called a trap representation.

    footnote 41 -

    41) Thus, an automatic variable can be initialized to a trap representation without causing undefined behavior, but the value of the variable cannot be used until a proper value is stored in it.


    HTH,
    Will
    --
    http://www.tuxedo.org/~esr/faqs/smart-questions.html
    http://www.eskimo.com/~scs/C-faq/top.html
    http://www.parashift.com/c++-faq-lite/
    http://www.accu.org/


  • Justin BibJustin Bib USAMember Posts: 0

    _____ \ http://forcoder.org \ free video tutorials and ebooks about // Objective-C C Swift PHP C# C++ PL/SQL Ruby Assembly Visual Basic JavaScript Delphi R Go Scratch Python Java Perl Visual Basic .NET MATLAB VBScript Kotlin Alice Julia Bash Lua FoxPro D F# Fortran Dart Apex Prolog Awk Crystal Lisp Hack COBOL ABAP Erlang Clojure Scala Ada Logo Rust Scheme Transact-SQL LabVIEW SAS ML // ____________

Sign In or Register to comment.