could you help me about this problem?

Discussion in 'C Programming' started by softwindow, Feb 27, 2007.

  1. softwindow

    softwindow Guest

    #include "stdio.h"
    #include "malloc.h"
    struct student{
    int age;
    char *nms;
    struct student *next;
    };
    struct student *create(){
    int ags=0,size=sizeof(struct student);
    char *nms=" ";
    struct student *head=NULL,*tail=NULL,*p=NULL;
    scanf("%d%s",&ags,nms); //-------------------here!!!!!!
    while(ags!=0){
    p=(struct student * )malloc(size);
    nms=(char *)malloc(20);
    p->age=ags;
    p->nms=nms;
    p->next=NULL;
    if(head==NULL){
    head=p;
    }else{
    tail->next=p;
    }
    tail=p;
    scanf("%d%s",&ags,nms);
    }
    return head;
    }
    int main(void) {
    struct student *ptr=create();
    while(ptr){
    printf("%d====%s\n",ptr->age,ptr->nms);
    free(ptr->nms);
    free(ptr);
    ptr=ptr->next;
    }
    }
    ************************************
    i think that is no problem with it.but it doesn't work!
    why?
     
    softwindow, Feb 27, 2007
    #1
    1. Advertisements

  2. softwindow

    Dave Hansen Guest

    You didn't give us much of a hint as to what you think "doesn't work"
    means. But one obvious problem is the code above. Once you pass a
    pointer to free, you can't then try to dereference it. Try something
    like

    tptr = ptr->next;
    free(ptr);
    ptr = tptr;

    (with an appropriate declaration of tptr, of course.)

    Regards,
    -=Dave
     
    Dave Hansen, Feb 27, 2007
    #2
    1. Advertisements

  3. softwindow

    Manish Guest

    If there is no problem, then why do you say it doesn't work.

    Sorry, I am not a mind reader, you will have to spell out what exactly
    your issue is.
     
    Manish, Feb 27, 2007
    #3
  4. softwindow

    Manish Guest

    What is your input, do you have enough memory allocated for 'nms'?
     
    Manish, Feb 27, 2007
    #4
  5. softwindow

    softwindow Guest

    ******************************************
    i have change code as your code,it is my fault;
    now i find it throw error at the first " scanf("%d
    %s",&ags,nms); //-------------------here
    "
    when i input "10 finy", it throw a error.
     
    softwindow, Feb 27, 2007
    #5
  6. softwindow

    Manish Guest

    okay now let's see the '10' gets stored in ags and 'finy' which needs
    5 (4 bytes for "finy" and 1 extra byte for '\0') bytes of storage
    space, you try to store it in 'nms' which has how much space ... ?
    (Ans. 1 byte)
     
    Manish, Feb 27, 2007
    #6
  7. softwindow

    softwindow Guest

    oh!my god! yes,you are right!
    thanks!
     
    softwindow, Feb 27, 2007
    #7
  8. softwindow

    John Turner Guest

    nms doesn't have any storage that he's allowed to write to. It points
    to a string literal.

    See: http://c-faq.com/decl/strlitinit.html

    John
     
    John Turner, Feb 27, 2007
    #8
  9. The declaration of nms was

    char *nms=" ";

    so nms points to *two* bytes (' ' and '\0'). But, as someone else
    already pointed out, you're not allowed to write to those bytes,
    because they're part of a string literal. (You *might* get away with
    it, but it's undefined behavior.)

    Another comment: white space can make your code easier to read.
    Rather than

    char *nms=" ";

    try:

    char *nms = " ";

    And so on.
     
    Keith Thompson, Feb 27, 2007
    #9
  10. softwindow

    pete Guest

    /* BEGIN new.c */

    #include <stdio.h>
    #include <stdlib.h>

    #define LENGTH 19

    struct student {
    int age;
    char *nms;
    struct student *next;
    };

    struct student *create(void)
    {
    struct student *head = NULL;
    struct student *tail = NULL;
    struct student *p = NULL;
    int ags;
    char *nms;

    do {
    p = malloc(sizeof *p);
    if (p == NULL) {
    puts("p == NULL");
    exit(EXIT_FAILURE);
    }
    p -> next = NULL;
    nms = malloc(LENGTH + 1);
    if (nms == NULL) {
    puts("nms == NULL");
    exit(EXIT_FAILURE);
    }
    if (scanf("%d%s", &ags, nms) != 2) {
    puts("scanf(\"%d%s\", &ags, nms) != 2");
    exit(EXIT_FAILURE);
    }
    p -> age = ags;
    p -> nms = nms;
    if (head == NULL) {
    head = p;
    } else {
    tail -> next = p;
    }
    tail = p;
    } while (ags != 0);
    return head;
    }

    int main(void)
    {
    struct student *next;
    struct student *ptr = create();

    while (ptr != NULL) {
    next = ptr -> next;
    printf("%d====%s\n", ptr -> age, ptr -> nms);
    free(ptr -> nms);
    free(ptr);
    ptr = next;
    }
    return 0;
    }

    /* END new.c */
     
    pete, Feb 27, 2007
    #10
  11. softwindow

    softwindow Guest

    **************************************
    you say "you're not allowed to write to those bytes, because they're
    part of a string literal"

    but i try it,i can write bytes like follow:

    char *nms=" ";
    scanf("%s",nms);

    you can try it.
     
    softwindow, Feb 28, 2007
    #11
  12. softwindow

    Nelu Guest

    The standard says that the behavior is undefined if you attempt
    to modify such a string. It is possible that *you* can do that
    but that's not something you should rely on.
     
    Nelu, Feb 28, 2007
    #12
  13. [...]

    Please trim quoted material. In particular, don't quote signatures
    unless you're actually commenting on them.
    As I write above:

    You *might* get away with it, but it's undefined behavior.

    The most likely results of attempting to modify a string literal are
    (a) your program immediately crashes, or (b) it "works", and the
    string literal is modified.

    Undefined behavior doesn't mean that the system is going to stop you
    from doing it. It means the behavior is undefined; anything can
    happen, so it's entirely up to you to avoid doing it.

    See question 1.32 in the comp.lang.c FAQ, <http://www.c-faq.com/>.
     
    Keith Thompson, Feb 28, 2007
    #13
  14. Let me try it... This is what happens when I run the program and type
    'b':

    $ foo
    b
    Segmentation fault

    So it's not exactly working for me. :)

    In addition to what others are saying about the behavior in this
    instance being undefined, the standard says that string literals may be
    in read-only memory, and can actually be shared in memory, for instance:

    #include <stdio.h>

    int main(void)
    {
    char *a = "foobar";
    char *b = "foobar";
    char *c = "frobozz";

    printf("a = %p\n", a);
    printf("b = %p\n", b);
    printf("c = %p\n", c);

    return 0;
    }

    prints the following on my system:

    a = 0x8048524
    b = 0x8048524
    c = 0x804852b

    (Note a and b point to the same place.)

    So, on my system, if it were allowed, modifying a's "foobar" would
    effectively also modify b's; it's probably not what was desired.

    -Beej
     
    Beej Jorgensen, Feb 28, 2007
    #14
  15. Groovy hepcat softwindow was jivin' on 27 Feb 2007 07:25:48 -0800 in
    comp.lang.c.
    could you help me about this problem?'s a cool scene! Dig it!
    That should be:

    No such header. It should be:

    Here you create a string literal of one character length, resulting
    in two bytes being allocated (one for your single character, and one
    for the terminating '\0'). This may be placed in memory marked as
    being readable but not writable. String literals are not modifiable.
    And here you attempt to read an arbitrarily long string (likely more
    than one character) into the non-modifiable, one character long string
    literal, thus attempting to not only modify a non-modifiable object,
    but actually overflow that, writing to memory you don't own; possibly
    memory that doesn't even exist! BANG! And your program crashes. Or it
    goes on "working", but ends up doing weird things to your hard drive.
    Or it makes demons fly out of your nose.
    Had you read the FAQ list of this newsgroup or simply lurked here
    for a while you would know that scanf() is not the best thing to use
    for interactive input. It is customary, upon first entering a
    newsgroup, to read a month or two of articles (lurk) before posting
    and to seek out and read the newsgroup's FAQ, if it has one. Failing
    to do so before posting is very rude! Please read the FAQ
    (http://www.eskimo.com/~scs/C-faq/top.html) before posting any more.
    What is the user supposed to be enterring anyhow? A prompt would be
    helpful.
    Don't cast the return from malloc(). Do check the return from
    malloc().
    Don't cast the return from malloc(). Do check the return from
    malloc().
    Here you copy the address of your string literal to your struct
    member. Thus, your struct member will point to your string literal. No
    other storage has been set aside, and no data copied.
    A bit dodgy! I think it will probably work, since the else clause is
    not executed first time around (because head == NULL first time
    through the loop). But it doesn't look good to dereference tail above
    the point where you actually set it to a useful value..., assuming p
    actually does contain a useful value.
    And again you attempt to write X characters (where X is some unknown
    number) to a one character long string literal.
    Check the return value. It's always a good idea.
    And here you're trying to free memory that was not allocated by
    malloc(), calloc() or realloc(). BANG! More undefined behaviour! More
    nasal demons! Remember, the nms member of your structure points at a
    string literal.
    And once again, BANG! You're dereferencing ptr after freeing the
    memory it points at.
    return 0;
    Think again! There are many problems here. There may be some I
    haven't spotted; after all, I only gave your code a cursory look. Even
    so, I still found many serous errors.
    Because it's so severely broken.

    --

    Dig the even newer still, yet more improved, sig!

    http://alphalink.com.au/~phaywood/
    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
    I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
     
    Peter Shaggy Haywood, Feb 28, 2007
    #15
  16. softwindow

    softwindow Guest

    thank a lot!

    thanks for your excellent explaining!
     
    softwindow, Feb 28, 2007
    #16
  17. There was no need to repeat the whole thing.
     
    Keith Thompson, Feb 28, 2007
    #17
  18. softwindow

    pete Guest

    Sometimes I do that as a cheap and easy way of saving
    the explanation in my email "Sent box".
     
    pete, Feb 28, 2007
    #18
  19. softwindow

    Richard Bos Guest

    That's all very well for you, but it is anti-social to then continue to
    post it to a newsgroup. All good mail- and news-readers have features to
    save a post without sending it. The saving is not the problem; the
    unsnipped sending is.

    Richard
     
    Richard Bos, Feb 28, 2007
    #19
  20. softwindow

    CBFalconer Guest

    Hey, I have a patent on that. Cease and desist immediately, or you
    will hear from my lawyers. (They also work for Microsoft. :)
     
    CBFalconer, Feb 28, 2007
    #20
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.