Question about malloc()

This is a discussion on Question about malloc() within the C Programming forums, part of the General Programming Boards category; I have a question regarding the following code Code: /*I omitted checking malloc() for NULL and didnt use free(); #include ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195

    Question about malloc()

    I have a question regarding the following code

    Code:
    /*I omitted checking malloc() for NULL and didnt use free();
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void) {
        char *p = malloc(sizeof *p);
        strcpy(p, "Hello, world!");
    
        printf("%s\n", p);
    
        return 0;
    }
    When I run it, I get:

    $gcc -Wall str.c -o str
    $./str
    Hello, world!
    $


    The question is if
    Code:
    char *p = malloc(sizeof *p);
    only allocates on byte, then how come the entire string can be copied to p since the string is like 15 bytes vs one.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Luck. C doesn't do boundry checking. If you want to trash your memory, it lets you.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    5

    the address of the first character

    the string does not get copied to the pointer. the address of the first character of the string gets copied.

    thus printf goes to the address of the beginning of the string and spits out everything until it sees a '\0'

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > gcc -Wall str.c -o str
    What does this do when you run it?

    gcc -Wall str.c -o str -lefence

    > the string does not get copied to the pointer
    Yes it does.


    > then how come the entire string can be copied to p since the string is like 15 bytes vs one.
    Maybe so, but the next call to malloc or free is likely to have a lot more "fun"
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Running the code with full warnings produced nothing.

    $gcc -Wall str.c -o str
    $./str
    Hello, world!

    Running efence(3) with the default arguments produced the following:

    $gcc -Wall str.c -o str -lefence
    $./str

    Electric Fence 2.2.0 Copyright (C) 1987-1999 Bruce Perens <bruce@perens.com>
    Segmentation fault
    $gcc -Wall -g str.c -o str -lefence
    $gdb str
    GNU gdb 6.1
    Copyright 2004 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB. Type "show warranty" for details.
    This GDB was configured as "i586-suse-linux"...Using host libthread_db library " /lib/tls/libthread_db.so.1".

    (gdb) run
    Starting program: /home/cdalten/str

    Electric Fence 2.2.0 Copyright (C) 1987-1999 Bruce Perens <bruce@perens.com>

    Program received signal SIGSEGV, Segmentation fault.
    0x40094ee6 in strcpy () from /lib/tls/libc.so.6
    (gdb) bt
    #0 0x40094ee6 in strcpy () from /lib/tls/libc.so.6
    #1 0x0804853c in main () at str.c:8

    Ahahahaha. The silent heap abuse! Okay, point proven. You win.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    5
    Quote Originally Posted by jmholber
    the string does not get copied to the pointer. the address of the first character of the string gets copied.
    uh... WTF were you thinking?

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Schitzo?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. Peculiar malloc / command argument question
    By Beowolf in forum C Programming
    Replies: 4
    Last Post: 09-10-2007, 11:54 PM
  4. Alternative to malloc
    By stellastarr in forum C Programming
    Replies: 13
    Last Post: 04-30-2007, 04:10 PM
  5. malloc, calloc question
    By chen1279 in forum C Programming
    Replies: 12
    Last Post: 09-07-2006, 05:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21