Thread: Problems with malloc

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    3

    Question Problems with malloc

    I want to allocate room in a buffer so I can send data through a socket (I've got to write a simple FTP program for class); however, every time I call malloc, it only allocates 4 bytes. I wrote some sample code to test it:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main() {
    	char* buffer;
    	buffer = (char*) malloc((size_t)sizeof("Sample string."));
    	printf("%d\n",sizeof(buffer));
    	printf("%d\n",sizeof("Sample string."));
    	free(buffer);
    	return 0;
    }
    The output is:

    4
    15
    Shouldn't they both say "15"? What am I doing wrong with malloc?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    sizeof(buffer) returns the size of the pointer named buffer, not the size of the buffer. So you are allocating 15 bytes, but sizeof(buffer) does not show that. You do not need to cast the result of sizeof to size_t.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    sizeof(buffer) gives you the size of a char *, not the size of the memory attached to the variable called buffer that is of type char * - the key here is that sizeof() is a compile-time resolved resource - it doesn't know anything about what happens in malloc, or if you do something like this:
    Code:
    char array[100];
    char *buffer = array;
    Quite clearly, buffer points to an array of 100 chars, but sizeof(buffer) will be 4 on most systems (2 or 8 are other likely numbers in 16 and 64-bit systems respectively, 100 is NEVER going to be the answer).

    --
    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.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    3
    How can I determine the size of the buffer?

    (I apologize, I haven't written C for the longest.)

    Edited to include: In the client/server I have written, it will only copy the first four characters from the source file into the destination file, even though I am allocating the same size in both buffers. I will post code.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In this case you already know the size of the buffer since you are using it. You just need to store it somewhere to keep track of it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I agree with laserlight - _YOU_ need to track the size of memory allocations in some way. The very easy way is to make it a constant, but if the allocations vary greatly in size, you may want to optimize it. You could use a struct, e.g.
    Code:
    struct memallocation
    {
       size_t size;
       void *ptr;
    };
    Or just have two parameters that you use each time you allocate memory.

    Of course, another option is to just make sure you allocate the right amount in the first place, e.g. read a string into a fixed storage [make sure it's large enough], then use strlen() to figure out it's length and use malloc(len+1) [+1 to add the zero-termination].

    --
    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.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    3
    Here is the majority of the reading and sending from the client side. Again, the server only gets four letters of the destination file.

    Code:
    //CLIENT
    // Get the file size
    fseek (fpRead , 0 , SEEK_END);
    fileSize = ftell(fpRead);
    rewind (fpRead);
    
    // Allocate memory to contain the whole file.
    buffer = (char*)malloc(fileSize);
    
    // Read the file into the buffer.
    fread(buffer,1,fileSize,fpRead);
    
    //Write to socket.
    n = write(sockfd,buffer,strlen(buffer));
    if (n < 0) 
    	error("ERROR writing to socket");
    Server code:

    Code:
    //SERVER
    //Parse fileSize received from socket
    fileSize = atoi(command);
    
    //Allocate space for incoming file
    buffer = (char*) malloc (fileSize);
    
    //Read in file from socket
    n = read(newsockfd,buffer,sizeof(buffer));
    if (n < 0)
    	error("ERROR reading from socket"
    	
    //Write buffer to file
    fpWrite=fopen("recieved.txt", "ab+");
    fwrite(buffer,1,fileSize,fpWrite);
    fflush(fpWrite);
    My file is always corrupt or truncated. Any ideas?

    EDITED: Oh, I forgot to take into account '\0'. I added 1 to my mallocs, and it worked flawlessly. Thanks everyone!
    Last edited by Zachary Lewis; 02-18-2008 at 06:27 AM.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you want to do this with some flexibility, e.g. allowing LARGE files to be transferred, you should consider transmitting the file in pieces - consider that it's quite possible to create files much larger than physical memory, which even if you CAN allocate a large enough buffer with malloc, will definitely make for a much slower solution than copying the file "piece-by-piece". Sure, if the file is small, you can load up the entire file in memory and then transmit it via a socket, but if the file is large there are two problems:
    1. IP packets are limited in size, so you would be sending multiple packets.
    2. If you don't have at least as much physical RAM as the file is large, swapping will happen.

    Copying something like 1.5 to 4KB at a time solves two problems:
    1. You don't need to allocate a variable size memory.
    2. You can copy ANY size file, no matter what it's size is.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using malloc
    By ulillillia in forum C Programming
    Replies: 34
    Last Post: 02-20-2008, 06:41 PM
  2. Problems with pointers and malloc()
    By Deirdre in forum C Programming
    Replies: 3
    Last Post: 10-28-2007, 04:20 PM
  3. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  4. malloc, realloc
    By figo2476 in forum C Programming
    Replies: 3
    Last Post: 04-28-2006, 10:11 PM
  5. 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