Thread: Pointers + Malloc = Painloc

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    1) Mem should be inside main.
    2) You should initialize them to NULL unless you allocate some more memory.
    3) The ending } should most likely be on its own line to help readability.
    4) You must free what you malloc. You currently are not.

    And just for the sake of it...
    T* vs T *: http://www.research.att.com/~bs/bs_faq2.html#whitespace
    If you're interested.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by Elysia View Post
    1) Mem should be inside main.
    2) You should initialize them to NULL unless you allocate some more memory.
    3) The ending } should most likely be on its own line to help readability.
    4) You must free what you malloc. You currently are not.

    And just for the sake of it...
    T* vs T *: http://www.research.att.com/~bs/bs_faq2.html#whitespace
    If you're interested.
    There's nothing about the subject at the link you posted.

    What *is* the difference?
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by IceDane View Post
    There's nothing about the subject at the link you posted.
    What *is* the difference?
    There is a difference, in the way it's written and interpreted.
    To the compiler, it's the same.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    ok, I've gotten mem properly malloced, but I don't really understand what this is pointing to. *mem is pointing to a block of memory 256 bytes long, right? How do I put something in that memory? I.E., if I want the 245th byte to hold the hex "4a", how do I do that?

    I've tried:
    Code:
      void *mem = malloc(256); // I think this is the right initialization
      mem[245] = 71;
      *mem[245] = 71;
      (&mem)[245] = (void *)71;  // gives warning without cast
      mem += 245;  // increments by 245*4 bytes.  I think?
      *mem = 71;
    none of them work, and in fact all lines (except for those with comments) above give a warning and an error:
    warning: derefrencing 'void *' pointer
    error: invalid use of void expression


    What is a valid use of a void expression?


    Edit: Elysia, I am freeing 'mem', I just didn't put it in the above code. I was typing it in a hurry.
    EDIT 2: Maybe I found my answer? Does this make sense to anyone:
    Code:
      void *mem = malloc(256);
      int i = 0;
      unsigned char *test;
      for(i=0; i<256; i++) {
        test = mem;
        if(i==0) *test = 71;
        printf("&#37;p(%p), ", (mem)++, *test);
    Last edited by Chalks; 10-19-2008 at 12:31 PM.

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. Structures, arrays, pointers, malloc.
    By omnificient in forum C Programming
    Replies: 9
    Last Post: 02-29-2008, 12:05 PM
  3. malloc pointers within a struct
    By tikelele in forum C Programming
    Replies: 3
    Last Post: 11-20-2007, 11:02 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. pointers
    By fanaonc in forum C Programming
    Replies: 3
    Last Post: 11-17-2001, 02:18 AM