Memory dump

Discussion in 'C Programming' started by erfan, Jan 18, 2008.

  1. erfan

    erfan Guest

    Hi,comp.c:
    I try to learn malloc,facing a problem like this:
    --------

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

    int main(void)
    {
    char *some_memory;
    int megabyte=1; /*Please take care here!!!
    Here is 1 byte*/
    int exit_code=1;
    some_memory=(char *)malloc(megabyte);
    if (some_memory!=NULL)
    {
    sprintf(some_memory,"123456789abcdef"); /* 1
    byte=8 bits */
    printf("%s",some_memory);
    exit_code=0;
    }
    else printf("not enough memory");
    free(some_memory);
    exit(exit_code);
    }
    -------------------------
    two questions:
    1. I want to test how many bits should the memory dump. i malloc only
    1 byte,and when i fill the memory with 16 characters,that will dump?
    why not 8 ? i think 1 byte equals 8 bits
    2.Does the memory dump really do harm to the memory? why gcc doesn`t
    gave a warn before i run it?
    if i change megabyte into 1024*1024*1024? but the memory`s real
    capality is 512M, what willhappen?
     
    erfan, Jan 18, 2008
    #1
    1. Advertising

  2. erfan said:

    > Hi,comp.c:
    > I try to learn malloc,facing a problem like this:
    > --------
    >
    > #include<unistd.h>


    You don't need this header (which isn't part of standard C anyway).

    > #include<stdlib.h>
    > #include<stdio.h>
    >
    > int main(void)
    > {
    > char *some_memory;
    > int megabyte=1; /*Please take care here!!!
    > Here is 1 byte*/
    > int exit_code=1;
    > some_memory=(char *)malloc(megabyte);


    You don't need the cast. The canonical form is:

    some_memory = malloc(megabyte * sizeof *some_memory);

    > if (some_memory!=NULL)
    > {
    > sprintf(some_memory,"123456789abcdef"); /* 1
    > byte=8 bits */


    1 byte = CHAR_BIT bits. CHAR_BIT is defined in <limits.h>, and it is fairly
    likely that, on *your* system, it's set to 8. (It must be at least 8, but
    can be higher, and is higher on some systems.)

    Your sprintf is unwise, because it attempts to store sixteen bytes of
    information in a region of memory that is only guaranteed to have one byte
    available to it. C doesn't define what will happen in this situation. On
    some implementations, you might get away with it. On others, the program
    might produce unexpected output, or no output at all. Or the program might
    crash or lock up. It might even hang or crash the whole computer. Or it
    might corrupt the disk, or it might jump to an unexpected part of memory
    and start executing whatever instructions it finds there.

    > printf("%s",some_memory);
    > exit_code=0;
    > }
    > else printf("not enough memory");
    > free(some_memory);
    > exit(exit_code);
    > }
    > -------------------------
    > two questions:
    > 1. I want to test how many bits should the memory dump. i malloc only
    > 1 byte,and when i fill the memory with 16 characters,that will dump?


    See above. I'm not sure what you mean by "memory dump" in this context. If
    you are asking about core dumps, probably the best place to ask is
    comp.unix.programmer, or perhaps a newsgroup dealing specifically with
    your compiler.

    > why not 8 ?


    Why 8?

    > i think 1 byte equals 8 bits


    See above.

    > 2.Does the memory dump really do harm to the memory?


    See above.

    > why gcc doesn`t
    > gave a warn before i run it?


    Because gcc isn't a mindreader. It is required to diagnose syntax errors
    and constraint violations, and it is allowed to warn about anything else
    it likes, but it can't diagnose any arbitrary programming mistake.


    > if i change megabyte into 1024*1024*1024? but the memory`s real
    > capality is 512M, what willhappen?


    If malloc can meet the request, it will return a pointer to the first byte
    in the allocated region. If not, it will return NULL.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Jan 18, 2008
    #2
    1. Advertising

  3. erfan

    Army1987 Guest

    erfan wrote:

    > Hi,comp.c:
    > I try to learn malloc,facing a problem like this:

    [snip code deliberately attempting to dump core]
    > two questions:
    > 1. I want to test how many bits should the memory dump. i malloc only
    > 1 byte,and when i fill the memory with 16 characters,that will dump?
    > why not 8 ? i think 1 byte equals 8 bits

    So what? Look up 'bit'. A character occupies as many bits a byte does.
    Btw, don't do that. On some implementations, abort() causes a core dump,
    too, and it's a less obscure way to do that. (Not to mention that the
    consequences of overrunning an array can be different than that, e.g. you
    could overwrite other data.)

    --
    Army1987 (Replace "NOSPAM" with "email")
     
    Army1987, Jan 18, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Microsoft News Server

    Getting proper Stack information from memory dump

    Microsoft News Server, Nov 9, 2004, in forum: ASP .Net
    Replies:
    9
    Views:
    1,299
    Jim Cheshire [MSFT]
    Nov 11, 2004
  2. halfdog
    Replies:
    12
    Views:
    12,594
  3. Otto Barnes

    Hashtable Memory Dump?

    Otto Barnes, Aug 4, 2003, in forum: C++
    Replies:
    1
    Views:
    417
    Mike Wahler
    Aug 4, 2003
  4. Chris Cranford

    Dump Memory Pointer?

    Chris Cranford, Apr 14, 2004, in forum: C++
    Replies:
    5
    Views:
    3,831
    Christopher Benson-Manica
    Apr 15, 2004
  5. Todd
    Replies:
    4
    Views:
    567
    Jeff Higgins
    Sep 5, 2007
Loading...

Share This Page