Thread: seg fault in pointer struct

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    27

    seg fault in pointer struct

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct {
      int value;
      char* value2;
    } demo;
    
    void test(int* v) {
      *v=1;
    }
    
    void test2(char* v) {
      strcpy(v, "ape");
    }
    
    int main() {
      demo* d;
      d=(demo*)malloc(sizeof(demo));
    
      test(&(d->value));
      printf("%d", d->value);
    
      // Initialize value2 first
      d->value2=(char*)(10*sizeof(char));
      test2(d->value2);
      puts(d->value2);
    
    }
    This gives seg fault. Why? Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Perhaps because you forgot to call malloc ?
    Code:
    $ gcc -W -Wall -Wextra -g bar.c
    $ gdb ./a.out 
    GNU gdb (GDB) 7.1-ubuntu
    Copyright (C) 2010 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "x86_64-linux-gnu".
    For bug reporting instructions, please see:
    <http://www.gnu.org/software/gdb/bugs/>...
    Reading symbols from /home/sc/Documents/coding/a.out...done.
    (gdb) run
    Starting program: /home/sc/Documents/coding/a.out 
    
    Program received signal SIGSEGV, Segmentation fault.
    memcpy () at ../sysdeps/x86_64/memcpy.S:91
    91	../sysdeps/x86_64/memcpy.S: No such file or directory.
    	in ../sysdeps/x86_64/memcpy.S
    (gdb) where
    #0  memcpy () at ../sysdeps/x86_64/memcpy.S:91
    #1  0x000000000040063d in test2 (v=0xa <Address 0xa out of bounds>) at bar.c:15
    #2  0x0000000000400697 in main () at bar.c:27
    Your address is 10, not a pointer to 10 bytes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    27
    oh yeah i forgot to put malloc in line 26

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Well it's another fantastic example of why you shouldn't cast malloc in a C program!
    FAQ > Casting malloc - Cprogramming.com

    Had you not beaten the compiler into submission with the cast, you would have gotten a warning from the compiler.

    Instead, the OS just killed your program with no information.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-29-2011, 01:21 AM
  2. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  3. Segmentation Fault With pointer to pointer strcpy
    By touchy in forum C++ Programming
    Replies: 3
    Last Post: 03-09-2011, 12:35 AM
  4. seg fault with nested struct from pointer
    By seaking1 in forum C Programming
    Replies: 4
    Last Post: 05-01-2009, 03:09 PM
  5. Replies: 1
    Last Post: 05-05-2004, 06:58 AM