malloc() vs buffer[]?

This is a discussion on malloc() vs buffer[]? within the C Programming forums, part of the General Programming Boards category; I wanted to know the difference between using malloc() to allocate data to a pointer and creating an array[]. I'm ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    13

    malloc() vs buffer[]?

    I wanted to know the difference between using malloc() to allocate data to a pointer and creating an array[].
    I'm writing a program that gets data from the internet and stores a part of it. It reads a packet with the length of the data that is coming next, then it malloc()'s space for it and gets it. It happens that, somewhere I may need to do something like storageArea = &packet[x]. This means I can't free(packet) anymore. Would it be better if instead of using malloc() and not freeing the pointer, I created a buffer and maybe afford losing some memory?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    it really depends on what you're doing with the data afterwards. If you'll need the information later, you'll want to malloc it so that it will stick around after local variables go out of scope. Also, malloc is useful when you don't know how much memory you'll need at compile-time.


    It happens that, somewhere I may need to do something like storageArea = &packet[x]. This means I can't free(packet) anymore.
    why not? I think you can. Am I missing something here? You'll have to free it eventually.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Local function variables are destroyed some time around function the time that the function ends. There is no need for you to free() them. When you malloc() something, that block of memory stays beyond the function's life. You can do more with such a dynamically allocated buffer imo.

    Also of note, memory is allocated from different places if it's a local buffer vs a dynamic one.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    13
    Packet is the pointer that was set using malloc(), and storageArea is another pointer, where the data will be stored. If I do "storageArea = &packet[x]", "free(packet)" and then do something with storageArea the program will segfault, because I freed the whole packet memory, and storageArea now points the area freed.
    the packet pointer is passed as argument to the function that will handle it.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Don't free() it if you don't want to lose it.

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    13
    Yep, but would it be more efficient than using a buffer? Estimate a length and memcpy the contents of the packet to the storage area and free(packet)?

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Perhaps a stupid question, but why not just copy it to the storage area to begin with?

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    13
    Because it needs to be processed first... A part of it will go to one storage area, then another part will go to another and so on. Then I may need to use storageArea = &packet[x] storageArea2 = &packet[y] storageArea3 = packet[z] and so on. The example I gave above was just an example of what I need to do with the data... By now the program works without having to free the pointer since I'm going to use it later on, but I was wondering if it wasn't better to copy stuff and freeing it, instead of pointing to it, in order to avoid memory leaks.

  9. #9
    Registered User
    Join Date
    Jun 2007
    Posts
    13
    This:
    Code:
    struct example
    {
    unsigned char *storageArea;
    unsigned char *storageArea2;
    unsigned char storageArea3;
    };
    
    unsigned char *packet = malloc(length);
    getpacketfromnet(packet);
    storageArea = &packet[3];
    storageArea2 = &packet[7];
    storageArea3 = packet[22];
    or this:

    Code:
    struct example
    {
    unsigned char storageArea[300]; //300 is an expected length
    unsigned char storageArea2[200]; //200 is an expected length
    unsigned char storageArea3;
    };
    
    unsigned char *packet = malloc(length);
    getpacketfromnet(packet);
    strcpy(storageArea, &packet[3]);
    strcpy(storageArea2, &packet[7]);
    storageArea3 = packet[22];
    free(packet);
    is safer / best?

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    the result is logically different - so firstly - decide what is CORRECT version.

    You can use strcpy only on NULL-terminated strings
    You loose the pointer you need to free later in the first version

    So both versions contain errors
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Registered User
    Join Date
    Jun 2007
    Posts
    13
    Assuming they are NULL-terminated strings. If you didn't get what I meant, use memcpy with a a random size then, I didn't want to come up with another variable. This is an _example_ code, it doesn't have to be perfect, it's just to show you the problem. I would understand if you said that if I was asking wether to use memcpy or strcpy.
    I didn't get what you said about the first version

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    This is an _example_ code
    Ok - here is the _example_ answer - it depends.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Registered User
    Join Date
    Jun 2007
    Posts
    13
    I'm just asking if it's better to copy data and free the pointer or if it's ok to point to it without freeing the pointer. Feel free to ignore the code if you think it contain errors, as long as you get the general idea.

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Ok - ignoring the code - It depends. Sometimes copying is better, sometimes - just storing the pointer to the extern buffer. Satisfied?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #15
    Registered User
    Join Date
    Jun 2007
    Posts
    13
    When is it better doing each thing?
    Last edited by Krones; 06-16-2007 at 11:53 PM.

Page 1 of 2 12 LastLast
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, 04:46 PM
  2. the basics of malloc
    By nakedBallerina in forum C Programming
    Replies: 21
    Last Post: 05-20-2008, 03:32 AM
  3. 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
  4. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 06:37 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM

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