How do you repeat the pointer?

For example:

int *start; start = (int*)malloc(40); 

If I wanted to repeat all 40 bytes, how would I do it? I tried to do something like:

 while(start != NULL){ start++; } 

but this is repeated through a huge amount of values, which is much more than 40. Thus, how do you guarantee that you iterate over all 40 bytes.

Thanks for the help.

+7
c pointers
source share
4 answers

There are two problems here.

One ptr ++ passes as many bytes as an element type indicates .

The type is int , so it will skip 4 bytes each time (assuming a 32-bit machine, since the integer is 4 bytes (32 bits)).

If you want to iterate over all 40 bytes (one byte at a time), iterate using, say, the char data type (or type cast your int * in char * and then increment)

Another problem is ending the loop.

In the end, no one puts NULL at the end, so your loop will continue to work (and the pointer advances) until it starts, it can be empty or exit your allocated memory area and it will work. The behavior is undefined .

If you allocated 40 bytes, you must end 40 bytes yourself.

Update:

Based on the comments dropping the voice to the original question, it is worth mentioning that the type returning the result malloc is not a good idea in C. The main reason is that it can potentially affect the failed distribution. This is a requirement in C ++. Details can be found in the same question about SO. Search "casting return value of malloc"

+6
source share

First of all, you must correctly distribute int :

 int* start = malloc( sizeof( int )*40 ) ; 

Then you can use array subtyping:

 for( size_t i = 0 ; i < 40 ; i++ ) { start[i] = 0 ; } 

or a pointer to the end of the allocated memory:

 int* end = start+40 ; int* iter = start ; while( iter < end ) { *iter= 0 ; iter++ ; } 
+4
source share

Arrays are contiguous blocks of memory. Since the name of the array is basically a pointer to the first element, you can use array notation to access the rest of the block. Remember, however, that C does not have error checking at the boundaries of the array, so if you leave the end of the memory block, you can do all kinds of things that you did not intend, and most likely some memory error or segmentation error. Since your int can be of variable size, I would use this code instead:

 int *start; int i; start = malloc(40 * sizeof(int)); for (i = 0; i < 40; i++) { start[i] = 0; } 

Something like this will work well. The way you do this, at least from the code you sent, there is no way to stop the loop, because as soon as it exceeds the memory block, it will continue to work until it starts in NULL or not get a memory error. In other words, the loop will only be completed if it is running at null. This zero may be within the block of memory that you allocated, or it may be outside the block.

EDIT: One thing I noticed about my code. It will allocate space for 40 ints, which may be 4 bytes, 8 bytes or something else depending on the architecture of the machine you are working on. If you REALLY want only 40 bytes of integers, then do something like this:

 int *start; int i; int size; size = 40/sizeof(int); start = malloc(size); for (i = 0; i < size; i++) { start[i] = 0; } 

Or you can use char or unsigned char data type if you need to. Another thing I noticed. The malloc function returns a void pointer type that is compatible with all pointers, so there is no need to do typecast on malloc.

+1
source share

Well arrays of arrays in C are not limited, so there are several options:

 int *start; int cnt = 0; start = (int*)malloc(sizeof(int)*40);; while(cnt<40) { start++; cnt++; } 

Another option:

 int *start; int *ref; start = ref = (int*)malloc(sizeof(int)*40); while(start != ref+40) start++; 

And this last one is closest to what you seem to want to do:

 int *start; start = ref = (int*)malloc(sizeof(int)*41); start[40] = -1; while((*start) != -1) start++; 

I would suggest learning more about how pointers work in C. It looks like you don't understand this very well. Also, remember that C removes the training wheels. Arrays are not limited or end in the standard way, and the pointer (address in memory) will never be NULL after iterating through the array, and the contents that the pointer points to can be any.

0
source share

All Articles