This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

malloc and two dimensional array ( char * * )

I want to allocate memory which I can use as a two dimensional array. But the following code is not working

unsigned char xdata ** memo;
unsigned char i = 0;
// n is number of rows
// size is number of columns
memo = (unsigned char xdata **) malloc( n*2 );
for ( ; i < size; i++)
	memo[i] = (unsigned char xdata *)malloc( size );
this code was inspired from
C++ new operator
memo = new char * [n];
for (int i=0; i < n; i++)
	memo[i*2] = new char [size];
Can you help me resolving this problem? Is there any problem with the logic?

PS: Fairly large memory has been allocated using init_mempool().

Parents Reply Children
  • Technically, I need an array of pointers(char **) because I dont want to waste the memory by using static array.
    I want to be able to free the memory not needed and allocate only when it is required.

    ___
    |___|-> "Hello World"
    |___|-> "Welcome!"
    |___|-> "Greetings! Mortal"
    |___|-> NULL
    |___|-> NULL

  • "I dont want to waste the memory by using static array."

    So instead you will waste both code & memory with the overheads of dynamic allocation?!

    Did you follow that link:
    http://www.keil.com/forum/docs/thread2622.asp

    "I want to be able to free the memory not needed and allocate only when it is required."

    Keil's overlay analysis will do this for you - without the need for malloc!

  • What if I have to sort the char strings? In that case swapping the pointers etc. will be more easy than to physically move the characters. In fact sorting is a major concern in my application that should be fast.

  • "What if I have to sort the char strings? In that case swapping the pointers etc. will be more easy..."

    Yes, but that has nothing to do with dynamic allocation, does it?

    Whether the pointers point to statically- or dynamically-allocated storage makes no difference!

  • I think you people want to avoid malloc at all costs :) ...but the problem is that I have two applications, notepad & phonebook, that will share the memory. And if one application requires more memory than the other why should I restrict it? It is the user who is to decide in which application to save more entries. And the optimimum technique is to use memory dynamically. Moreover, both applications use different data structures.

    Perhaps you'll say that 8051 is not meant for such things but I must do it somehow because I don't want to lose the client :) ...

    And if malloc can help simplify the logic why not use it? And being a developer atleast one should know how to such things...malloc may pose some severe problems but difficult things are not to be put aside :) ...

    See you later...

    Regards,
    aFFAN

  • Technically, I need an array of pointers(char **)

    Do you need to allocate the array AND the stuff that the array objects point to? If so, then...

    Since you're using malloc to allocate the array of pointers, you need a pointer to the array:

    char xdata * xdata *PtrtoPtrs;

    You can allocate the table as follows:

    PtrtoPtrs = malloc (100 * sizeof (char xdata *));

    This allocates an array of 100 pointers.

    PtrToPtrs[0] is the first pointer in the table. PtrToPtrs[99] is the last (100th) pointer in the table.

    Once you allocate the table, you should probably clear the pointers to NULL.

    When you add items to the table, you'll need to malloc memory for those strings as well. For example:

    char buffer [50];  // contains string to add to table
    
    for (i=0; i<100; i++)
      {
      get_the_next_string(buffer);
      PtrToPtrs[i] = malloc(strlen(buffer)+1);
      if (PtrToPtrs[i] != NULL)
        strcpy (PtrToPtrs[i], Buffer);
      }
    

    Of course, you'll need to FREE the memory you stop using.

    Jon


    }

  • "I think you people want to avoid malloc at all costs"

    As already noted, that is generally a good approach for embedded systems suited to an 8051

    What you're doing is not a conventional embedded system for an 8051, and dynamic allocation may well be appropriate to your application - but you hadn't described the application before, so we couldn't tell, could we?

    Too many people just assume that anything a PC can do will be good on an 8051.

  • I have two applications, notepad & phonebook
    Both excellent applications for a microprocessor, how come you have chosen a microcontroller?

    Erik

  • "Both excellent applications for a microprocessor, how come you have chosen a microcontroller?"

    He mentioned the word 'client'.

    Enough said?

  • If you have to wrestle this into a '51, why not define the variables in the areas you want to malloc as C externs and use a small assembler module to define the locations.

    That way you can greb what you need to grab and leave the rest to the compiler.

    Erik

    Ps are you related to Abdul Rauf, whom we see now and then in the '51 fora?

  • Thanks a lot Jon, it really helped me...

  • RE: Erik

    why not define the variables in the areas you want to malloc as C externs ...

    that sounds like a good idea...but honestly I don't know how to do that...but I'll think over it...thanks

    are you related to Abdul Rauf...

    Well, I'm not related to the Abdul Rauf you are talking about...but my father's name is also Abdul Rauf who could not be here cuz he is a biologist :)