Thread: Quick question with pointers, arrays, and malloc. malloc space for array of pointers

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    35

    Quick question with pointers, arrays, and malloc. malloc space for array of pointers

    Yo, quick question.

    I'm tooling around with pointers, arrays, and malloc.

    My ultimate goal is to manage an array of pointers that is flexible. But had some questions regarding memory behavior.

    Code:
    void * pointers[0];
    
    int main(int argc, char * argv[]) {
    	int a = 5;
    	int b = 6;
    	
            //#1
    	//-shouldn't this cause a memory error? I haven't allocated enough space,
    	// and the declaration of the variable specifies "0" items.
    	//-does it not cause a memory error because "pointers" is on the heap?
    	pointers[4] = &a;
    	
            //#2
    	//allocate space for pointers
            //-is this the right way to allocate space for an array variable?
            //-does allocating space for a file-scoped variable leak memory? Taking into account that I didn't allocate the memory that was there to begin with? Or should I use realloc?
    	*pointers = malloc( sizeof(void *) );
    	pointers[0] = &a;
    	
            //#3
    	//-shouldn't these create memory errors? I only allocated space for 1 pointer.
    	pointers[1] = &b;
    	pointers[2] = &a;
    	
    	int * c = pointers[0];
    	int * d = pointers[1];
    	int e = *((int *)pointers[0]);
    	printf("%i %i %i\n",*c,*d,e);
    	return 0;
    }
    My questions are inline in the code.

    Thanks for any help!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by beheadedmyway View Post
    Yo, quick question.

    I'm tooling around with pointers, arrays, and malloc.

    My ultimate goal is to manage an array of pointers that is flexible. But had some questions regarding memory behavior.

    My questions are inline in the code.

    Thanks for any help!
    It's an error to try to create an array (of any data type), with zero elements in it.

    When you use malloc(), calloc(), or realloc(), you may get memory from the heap. You are not using malloc() in #1.

    #2. You don't have any code that will compile, so there can't be a memory leak, yet. Yes, if you do malloc() memory in a function, and you never free() it, it will be a memory leak.

    #3. Again, "pointers[] is nothing, so you haven't made a memory error yet - because pointers[], simply doesn't exist.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    It's all wrong because you can't have a zero-sized array. But let's pretend it's an array of one.

    1. It's undefined behavior to access beyond the end of an array. You might get a segfault, you might not. That's the nature of undefined behavior.

    2. Not sure what you want. Currently it's a memory leak. You can just do pointers[0] = &a and now the first (and only) element of your array points to the local variable “a”.

    3. See 1.

    When you say that you want to “manage an array of pointers that is flexible”, perhaps you mean something like this:
    Code:
    void **pointers;
    
    pointers = malloc(5 * sizeof *pointers);
    pointers[0] = some_memory_location;
    pointers[1] = some_memory_location;
    ...
    pointers[4] = some_memory_location;
    Like any “dynamic array”, you create a pointer to the type you want to store. Since you want to store void*, you create a pointer to a pointer to void. Then allocate space.

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    35
    Thanks!

    One other question if you wouldn't mind indulging me.

    Code:
    Code:
    int main(int argc, char * argv[]) {
    	int a = 5;
    	int b = 6;
    	
    	void ** pointers = malloc(2 * sizeof(void *));
    	pointers[0] = &a;
    	pointers[1] = &b;
    	
    	int test[2];
    	test[0] = 4;
    	test[1] = 8;
    	
    	printf("%lu\n", sizeof(test) / sizeof(int) );
    	printf("%lu\n", sizeof(pointers));
    	printf("%lu\n", sizeof(pointers) / sizeof(void *));
    	printf("%lu\n", sizeof(*pointers) / sizeof(void *));
            
    	return 0;
    }
    Is it possible to get the number of items for dynamic arrays like this? I can do it with the var "test" above, but am not sure if you can even do it with the "pointers" var.

    Thanks again

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you allocate some stuff:
    Code:
    type *foo = malloc( X * sizeof *foo );
    You cannot find keep track of the actual size by doing:
    Code:
    size = sizeof foo;
    If that's what you're asking.

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

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    35
    I'm not sure if that's what I was asking.

    I was curious if you can find the number of items a dynamic array can contain after it's been allocated.

    Code:
    int a = 5;
    int b = 6;
    void ** pointers = malloc(2 * sizeof(void *));
    pointers[0] = &a;
    pointers[1] = &b;
    unsigned long elements = sizeof(pointers) / sizeof(void *); //looking to get "2"
    printf("the number of elements pointers can contain: %lu",elements);
    I can't figure out what to pass to the sizeof operator to get the total bytes that "pointers" points to.

    sizeof(pointers) returns 8 which is the byte size of a pointer.
    sizeof(*pointers) returns 8 which is the byte size of a pointer (the type pointer points to).

    I'm looking to get the total bytes; so 16. which would be (16 / sizeof(void *)) = 2.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by beheadedmyway View Post
    I was curious if you can find the number of items a dynamic array can contain after it's been allocated.
    No.


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

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    35
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers to arrays
    By rakeshkool27 in forum C Programming
    Replies: 1
    Last Post: 01-24-2010, 07:28 AM
  2. Quick question on malloc and strings
    By officedog in forum C Programming
    Replies: 20
    Last Post: 11-06-2008, 05:14 PM
  3. Structures, arrays, pointers, malloc.
    By omnificient in forum C Programming
    Replies: 9
    Last Post: 02-29-2008, 12:05 PM
  4. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM