Thread: Malloc() quesiton

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    96

    Malloc() quesiton

    If i have a pointer with some information already in it and then make a malloc() call, does this assign additional memory space to the pointer variable or does it overwrite the memory space which I already have assigned?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It overwrites the pointer.
    The memory it used to point to still exists, but you might not be able to get to it any more (this is a memory leak).

    Use realloc if you want to expand what you've already got.
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    How can it increase the space? Is it psychic somehow?
    I think you fail to understand how basic C works.
    malloc is not psychic; it does not know whether you've allocated anything before the current call or not and just returns a memory block of the specified size. It returns an address and addresses are stored in pointers, so you're just overwriting the previous address stored in the pointer you assign it to.
    Last edited by Elysia; 02-12-2009 at 09:09 AM.
    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
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    malloc is not physic;
    I think Elysia means malloc is not psychic.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by matsp View Post
    I think Elysia means malloc is not psychic.

    --
    Mats
    Well it's not a physic either.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    96
    I'm attempting to use the realloc() function but I keep getting:

    *** glibc detected *** ./simple: realloc(): invalid next size: 0x0962c740 ***

    and then a backtrace and I'm not too sure why. Here is the code for realloc() which I have:

    Code:
    nt myHTTPclientResponse(int socket, char *code, char *message, char *server, char *lastModified, char *date, char *contentType, int contentlength, char *body) {
          char mess_in[BUFSIZ];
    
          int nr;   
          int ret;         
    
          if((nr=read(socket, mess_in, BUFSIZ)) < 0) {
                fprintf(stderr, "Trouble Reading From Server.\n");
                fprintf(stderr, "Errno Value:  %s\n", strerror(errno));
          }      
    
          strncat(message, mess_in, BUFSIZ);
         	while (nr != 0) {
                if(ret = (realloc(message, nr *sizeof(char))) == NULL) {
                      fprintf(stderr, "Trouble with relloc function trying to assign more memory to *message\n");
                      fprintf(stderr, "Errno Value:  %s\n", strerror(errno));
                }
    
                strncat(message, mess_in, strlen(mess_in));
    
                memset(mess_in, '\0', BUFSIZ);
    
                if((nr=read(socket, mess_in, BUFSIZ)) < 0) {
                      fprintf(stderr, "Trouble Reading From Server.\n");
                      fprintf(stderr, "Errno Value:  %s\n", strerror(errno));
                }    
          }
    //      fprintf(stdout, "%s\n\n", message);
    
          return nr;
    }
    I know that it is something in my syntax of the second argument of the realloc() because if I just substitute BUFSIZ there it runs fine. I was just trying to allocate enough memory for the amount of data that is read in.
    Last edited by NuNn; 02-12-2009 at 12:24 PM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    How about actually initializing nr?
    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.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Elysia View Post
    How about actually initializing nr?
    That's what has unfortunately been done inside the if-statement. Yes it's bad style tho.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh yes, I didn't see it there.
    So then the culprit is one of the read statements, because clearly it is trying to allocate too much.
    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.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    96
    I will initialize it do 0 just to be safe anyway to make it more better practice

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. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM
  5. malloc always setting length of 8?
    By Zarkhalar in forum C++ Programming
    Replies: 7
    Last Post: 08-01-2004, 11:36 PM