0

Good day. I have this: MAP_ITEM **map what I think is pointer to array of pointers (correct me if I am wrong please) and I have to allocate space for it. I can allocate space using malloc for 1 pointer, but have no idea how to do this. help would be really appreciated.

7
  • Learn the basics... answering this very basic question would be proving you a disservice. If you don't yet know the difference between a pointer to a pointer, an array or an array of pointers, then you shouldn't be trying to allocate heap memory just yet IMO Commented Apr 21, 2014 at 16:10
  • yeah. that helped me so much. in documentation to malloc isn't ** even mentioned. really thank you . . . Commented Apr 21, 2014 at 16:18
  • That's why I said you should learn the basics: * is a pointer, ** is a pointer to a pointer. If you don't know about that, and how to work with double indirection, then you ought to stay clear of this stuff. First come to terms with what a pointer actually is, how you use them and what they are actually for. Commented Apr 21, 2014 at 16:27
  • good. pointer to a pointer. now, problem is I have into map insert two dimensional array. this is where I am stucked. I can allocate pointer to basic array and work with it: int *pointer = (int *)malloc(size of(int)*numofelements); but how do I allocate two dimensional array, create pointer and then access its elements? Commented Apr 21, 2014 at 16:37
  • (int**) malloc(sizeof(int*) * numofpointers) Commented Apr 21, 2014 at 16:41

2 Answers 2

1

Here is an example, written for use with char **, but you can modify for your purposes:

char ** allocMemory(char ** a, int numStrings, int maxStrLen)
{
    int i;
    a = calloc(sizeof(char*)*(numStrings+1), sizeof(char*));
    for(i=0;i<numStrings; i++)
    {
      a[i] = calloc(sizeof(char)*maxStrLen + 1, sizeof(char));
    }
    return a;
}    

call it like this: (for array of 10 strings, each having maximum of 79 characters (leave one for NULL term)

char **arrayOfString;

arrayOfString = allocMemory(arrayOfString, 10, 80);

//

You also need to free memory created with allocMemory

void freeMemory(char ** a, int numStrings)
{
    int i;
    for(i=0;i<numStrings; i++)
        if(a[i]) free(a[i]);
    free(a);
}  

Call it like this:

freeMemory(arrayOfStrings, 10);
Sign up to request clarification or add additional context in comments.

1 Comment

You are welcome. char ** are not trivial to manage, but with care, can be.
0

Import the stdlib.h Then use the malloc function. Where is an example for a one dimentional array:

int* my_in_array = (int*) malloc(sizeof(int) * size_of_my_array);

Note that malloc receives the number of bytes you want to allocate, so sizeof will tell you how many bytes a datatype will need (in this case an int, but it can be used for chars, structures, ...) and then I multiplicate it by size_of_my_array, wich is the number of elements of my array.

Now, just try to se this for you case.

2 Comments

Note: you will need more malloc calls than just one
Also, where is another function that you should use: free(<pointer>) wich must be called to free the memory block that you allocated, although the operative systems does this in the end of the program, is good practice to release memory when you don't need it anymore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.