469,336 Members | 5,950 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

setting pointers in array to NULL

2
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
Jul 1 '08 #1
3 12231
mac11
256 100+
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.
Jul 1 '08 #2
Banfa
9,064 Expert Mod 8TB
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

Expand|Select|Wrap|Line Numbers
  1. int *p1;
  2. int *p2;
  3.  
  4. p1 = 0;
  5. memset(&p2, 0, sizeof p2);
  6.  
  7. /* p1 is definately set to be a NULL pointer, p2 is not it has just 
  8.    had all its memory locations set to 0.  On many platforms this
  9.    may be a NULL pointer but it is not guaranteed.  p1 is portable
  10.    p2 is not.
  11. */
  12.  
Jul 2 '08 #3
JoxC
2
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.. ;)
Jul 2 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by Peter B. Steiger | last post: by
20 posts views Thread by fix | last post: by
19 posts views Thread by gaga | last post: by
3 posts views Thread by ozbear | last post: by
8 posts views Thread by Steve Lambert | last post: by
7 posts views Thread by Frank M. | last post: by
5 posts views Thread by Amogh | last post: by
25 posts views Thread by J Caesar | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.