Thread: Memory Allocation

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    22

    Memory Allocation

    I'm reasonably new to c and dynamic memory allocation really confuses me.

    Take this code snippet:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
            char** test = malloc(sizeof(char)*100);
            char* temp1 = "hi";
            char* temp2 = temp1;
            test[0]=temp1;
    
            printf("%s\n", test[0]);
            printf("%s\n", temp2);
    }
    Why is it that if test doesn't have a call to malloc, the program terminates with an error; whereas, temp1 and temp2 don't need calls to malloc?

    I'm also confused overall when calls to malloc are necessary

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Pointers are just like every other variable in that they hold a value. The value they hold is the address of a variable, instead of a non-address variable. So...
    Code:
    char *test = "point at (make my vlaue hold the address of) a string literal";
    A string literal is basically a string that's set aside in memory some place separate from you actually allocating it by hand using something like malloc, and separate from you making an array.

    Just like you can assign one value to another variable of the same type, you can make pointers pass their value (the address they hold) to another pointer:
    Code:
    char *texs2 = test;
    Now both of them hold the address of that string literal as their value. So you have basically three ways to make a (character pointer) pointer point to something:

    1. Assign it the address of a block of memory directly using something like malloc.
    2. Assign it the address some other pointer is already holding.
    3. Assign it the address of a string literal (works really only for char *), or other array.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    22
    A string literal is basically a string that's set aside in memory some place separate from you actually allocating it by hand using something like malloc, and separate from you making an array.
    So I guess my question is: why are you allowed to assign a pointer to a string literal, and not a pointer of a double pointer to a string literal? (aka. char* strlit = "string" but not char** doubp[0] = "string")

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You haven't allocated a 'doubp[0]'. That's why. There is no such thing as 'doubp[0]', because you haven't allocated it. It's the same reason you can't just do this:
    Code:
    char *p; 
    p[0] = 'a';
    p doesn't point at anything, so you can't make what it points at hold a value. (Because it isn't pointing anywhere you've allocated.)


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Prodiga1 View Post
    Code:
            char** test = malloc(sizeof(char)*100);
    Your test variable isn't allocating correctly. You are allocating space for 100 characters and storing it in a char **. You would store a block of characters in a char * (single pointer). You need to allocate 100*sizeof(char *). The easy way to ensure you're always using the right type is to do:
    Code:
    char **test = malloc(100 * sizeof(*test));
    That way, whatever type test is, you always allocate the right size.

    Quote Originally Posted by Prodiga1 View Post
    So I guess my question is: why are you allowed to assign a pointer to a string literal, and not a pointer of a double pointer to a string literal? (aka. char* strlit = "string" but not char** doubp[0] = "string")
    The simple answer is because when you use a string literal like that, you are basically giving the address of the start of that string. That is always of type char *, so you have to assign it to a compatible type (like another char *). The doubp declaration doesn't do what you think it does. You are declaring doubp as a zero-element array of pointers to pointers to chars and trying to initialize that to "string".

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    22
    That makes sense.

    On the same token, why is:
    Code:
    1) char** stringArray = {"string1", "string2"};  //not allowed
    2) char* stringArray[] = {"string1", "string2"}; //allowed
    but

    Code:
    1) char* string = "text"; //allowed
    2) char  string[] = {'t','e','x','t','\0'}; //allowed
    In both cases, the power of (1) is equal on both sides of assignment. Why is the program able to automatically allocate memory for char* string = "text" but not char** stringArray?

    or even char** stringArray = {string}; (as defined in (1))

    Is the long and short of this that, whenever a char** is initialized, you must dynamically allocate memory for it?
    Last edited by Prodiga1; 03-01-2011 at 10:11 PM.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Because string literals are a special case. You can do this:
    Code:
    char *s = "yay";
    But you can't do this:
    Code:
    int *i = 1233445523423423423;
    You can't create arrays on the fly like that for anything other than character arrays.
    Code:
    foo = { "this", "that" }
    ...wouldn't be a character array.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    22
    Got it. Thanks for the help, it's immensely appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory allocation problem
    By ariko in forum C Programming
    Replies: 10
    Last Post: 07-24-2010, 09:09 PM
  2. Problem with custom dynamic memory allocation routines
    By BLauritson in forum C++ Programming
    Replies: 12
    Last Post: 03-11-2010, 07:26 AM
  3. Bug in Best-Fit Memory Allocation program (Simulation)
    By RommelTJ in forum C Programming
    Replies: 6
    Last Post: 12-13-2009, 04:43 PM
  4. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  5. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 08:36 AM