Thread: fault with code but unsure where!

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    85

    fault with code but unsure where!

    hi everyone
    ive just made a little program that uses structures, and memory allocation
    the program comes up with a error message saying 'segmentation fault'
    i think this is because there is a bug in the memory allocation part of the program but im not sure

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include "simpleio.h"
    
    struct book
    {
        char *author;
        char *name;
    }copy1;
    struct book copy2;
    
    int main()
    {
        copy1.author=malloc (*copy1.author * sizeof (char));
        copy1.name=malloc (*copy1.name * sizeof (char));
        printf("please enter the authors name and the name of the title\n");
        gets(copy1.author);
        gets(copy1.name);
        
        copy2.author = copy1.author;
        copy2.name = copy1.name;
        free(copy1.author);
        free(copy1.name);
        
        printf("your book %s by %s is on its way in the post\n", copy2.name, copy2.author);
        return 0;
    }
    thanks in advance

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    You need to figure out how much memory you'll need to allocate at runtime using malloc(), at runtime.

    This bit is nonsense.
    Code:
      copy1.author=malloc (*copy1.author * sizeof (char));
      copy1.name=malloc (*copy1.name * sizeof (char));
    Also, don't use gets(). Google why it's the Devil and what you can do about it.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    sizeof (char)

    that's one byte, you may want more...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    ive used fgets but you need three input variables to make it work so that was out the question for me and i know about its 'crash' issues and its vulnerabilities
    thanks for the replies but i already know there is a problem im just not sure how to fix it...

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by aadil7 View Post
    ive used fgets but you need three input variables to make it work
    I'm not sure what you think this means. You need to give fgets three parameters, but you don't need three input variables. You call fgets with (1) where you want the input to go (2) how much input you want (3) where you want it from.

  6. #6
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    1st of all could you let us know where and when you get the error, secondly......

    Quote Originally Posted by msh View Post
    You need to figure out how much memory you'll need to allocate at runtime using malloc(), at runtime.

    This bit is nonsense.
    Code:
      copy1.author=malloc (*copy1.author * sizeof (char));
      copy1.name=malloc (*copy1.name * sizeof (char));
    is right. Re-check your memory allocation.
    malloc
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  7. #7
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    Closed by request.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic array
    By mouse666666 in forum C Programming
    Replies: 36
    Last Post: 04-11-2010, 02:27 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Help with code segmentation fault
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 04-10-2003, 03:00 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM