473,492 Members | 4,279 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

setting pointers in array to NULL

2 New Member
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 13670
mac11
256 Contributor
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,065 Recognized Expert Moderator Expert
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 New Member
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

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

Similar topics

8
3661
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
20
2900
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes)...
19
14486
by: gaga | last post by:
I can't seem to get this to work: #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *names; char **np;
3
3428
by: ozbear | last post by:
This is probably an obvious question. I know that pointer comparisons are only defined if the two pointers point somewhere "into" the storage allocated to the same object, or if they are NULL,...
8
2559
by: Steve Lambert | last post by:
Hi, I'd be grateful if someone could clarify this for me. In the linked list structure my intention is to declare an array of length 3 containing pointers to node eg. Node *Iterators The...
7
2460
by: Frank M. | last post by:
I'm trying to declare an array of pointers to structures so that I can make the last element a NULL pointer. I figure that it would more easily allow my library routines to know when to stop...
5
3137
by: Amogh | last post by:
Hi, My question is related to setting freed pointers to NULL. After freeing a pointer: 1) Should the freeing routine also be responsible for setting the pointer to null? 2) Or, should the...
23
7363
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
25
12982
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
0
7118
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6980
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7364
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3087
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1397
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
282
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.