setting pointers in array to NULL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JoxC
    New Member
    • Jul 2008
    • 2

    #1

    setting pointers in array to NULL

    Hey guys,

    I was wondering if there is an *efficient* way in setting every pointer in an array of pointers to NULL?
    memset cannot work for compatibility reasons, but I was playing a bit with memcpy: I built an array of NULL-pointers and tried to memcopy it to the array I wish to clear, but I couldn't find a working solution.
    e.g.:
    int** NULL_ARRAY;
    NULL_ARRAY = (int **) malloc (x * sizeof(int*));
    int** array;
    array = (int **) malloc (x * sizeof(int*));
    // fill array with sth.
    memcpy(array, NULL_ARRAY, x);
    doesn't work either with e.g.: memcpy(&(array[0]), &(NULL_ARRAY[0]), x);
    or: memcpy(array[0], NULL_ARRAY[0], x);

    Does anybody know a method which avoids a loop? Would be much appreciated. thx in advance!

    And a bit related to that: is there also a one-instruction routine for setting every member of an array of ints to a special value not equal to 0?

    best, Joerg
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    Originally posted by JoxC
    Does anybody know a method which avoids a loop? Would be much appreciated. thx in advance!
    I don't think any implementation will avoid a loop. Even if you had memset() proper to use it still has a loop inside, there is no other way to touch ever element of the array. Writing your own memset won't be very difficult though, just get a pointer to the array and step through the array by incrementing the pointer and copy the value you want into each position.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Originally posted by JoxC
      memset cannot work for compatibility reasons, but I was playing a bit with memcpy: I built an array of NULL-pointers and tried to memcopy it to the array
      If memset will not work for you then neither will memcpy because any method you use to build any array of NULL pointers to memcpy from is bound to be directly usable on the array you want to fill with NULL pointers.

      The only way to properly initialise a NULL pointer is to assign it the value 0 within a pointer context, that means an assignment using the = operator you will need to use a loop to do that to an array of pointers.

      As to a single instruction to fill an array with a non-zero value, there is not a single instruction to fill an array with 0 in C (or C++). If you are thinking of memset then that is a function not an instruction.

      The existence of such an instruction is platform dependent. I know that one of the platforms I currently work on has one, bc (block copy) but not all platforms do.


      And finally to anyone reading this and getting confused by the idea that memset to 0 does not create NULL pointers this is because the value of the NULL pointer is not set by the C/C++ standard. What is set is that when 0 is used in a pointer context the compiler shall set the value of the pointer to the NULL pointer value for the platform. Many platforms do use 0 has the NULL pointer value (for simplicity I image) but not all. Giving an example

      [code=c]
      int *p1;
      int *p2;

      p1 = 0;
      memset(&p2, 0, sizeof p2);

      /* p1 is definately set to be a NULL pointer, p2 is not it has just
      had all its memory locations set to 0. On many platforms this
      may be a NULL pointer but it is not guaranteed. p1 is portable
      p2 is not.
      */
      [/code]

      Comment

      • JoxC
        New Member
        • Jul 2008
        • 2

        #4
        Okay I feared this.. thanks a lot though you two!
        Thought that maybe the compiler can do fancy things like fast bit-operations to optimize such an array assignment, but ok then I go with the loops.. ;)

        Comment

        Working...