你如何迭代指针?

时间:2023-01-28 07:29:30

For example:

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

If I wanted to iterate through all 40 bytes, how would I do so? I tried doing something like:

如果我想迭代所有40个字节,我该怎么做?我尝试过这样的事情:

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

but that iterates through a massive number of values, which is much greater than 40. Thus, how do you ensure that you iterate through all 40 bytes.

但是,它会遍历大量的值,这远远大于40.因此,如何确保迭代所有40个字节。

Thanks for all the help.

谢谢你的帮助。

4 个解决方案

#1


7  

There are two issues here.

这里有两个问题。

A single ptr++ skips as many bytes as the type of element it points to.

单个ptr ++跳过与其指向的元素类型一样多的字节。

Here the type is int, so it would skip 4 bytes each time (assuming a 32 bit machine since integer is 4 bytes (32 bits) there).

这里的类型是int,所以每次跳过4个字节(假设32位机器,因为整数是4个字节(32位))。

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

如果要遍历所有40个字节(一次一个字节),请使用char数据类型进行迭代(或者将int *类型转换为char *然后递增)

The other problem is your loop termination.

另一个问题是你的循环终止。

There is no one putting a NULL at the end here, so your loop would keep running (and pointer advancing forward) until it runs into may be a null or goes out of your allotted memory area and crashes. The behavior is undefined.

没有人在这里放置一个NULL,所以你的循环将继续运行(并且指针向前推进),直到它遇到可能是null或者超出你分配的内存区域并崩溃。行为未定义。

If you allocated 40 bytes, you have to terminate at 40 bytes yourself.

如果分配了40个字节,则必须自己终止40个字节。

Update:

Based upon a comment cum down vote to the original question, it is worth mentioning that type casting the result of malloc is not a good idea in C. The primary reason is that it could potentially tamper a failed allocation. It is a requirement in C++ though. The details can be found in the exact same question on SO. Search "casting return value of malloc"

基于对原始问题的评论和结果投票,值得一提的是,类型转换malloc的结果在C中不是一个好主意。主要原因是它可能潜在地篡改失败的分配。但它是C ++的一项要求。详细信息可以在SO上的完全相同的问题中找到。搜索“malloc的返回值”

#2


6  

First of all, you should be allocating ints correctly:

首先,您应该正确分配ints:

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

Then you can use array subscripting:

然后你可以使用数组下标:

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++ ;
}

#3


1  

Arrays represent 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 though, there is no error checking by C on the bounds of the array, so if you walk off the end of the memory block, you can do all kinds of things that you didn't intend and more than likely will end up with some sort of memory fault or segmentation error. Since your int can be variable size, I would use this code instead:

数组表示连续的内存块。由于数组的名称基本上是指向第一个元素的指针,因此您可以使用数组表示法来访问块的其余部分。但请记住,C对数组的边界没有错误检查,所以如果你走出内存块的末尾,你可以做各种你不想要的事情,而且很可能最终会某种内存故障或分段错误。由于你的int可以是可变大小,我会使用这个代码:

int *start;
int i;

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

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

Something like that will work nicely. The way that you are doing it, at least from the code that you posted, there is no way to stop the loop because once it exceeds the memory block, it will keep going until it runs into a NULL or you get a memory fault. In other words, the loop will only exit if it runs into a null. That null may be within the block of memory that you allocated, or it may be way beyond the block.

这样的东西会很好用。你正在这样做的方式,至少从你发布的代码中,没有办法停止循环,因为一旦它超过内存块,它将继续运行,直到它遇到NULL或你得到内存错误。换句话说,循环只有在遇到null时才会退出。该null可能位于您分配的内存块中,或者可能超出块。

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

编辑:有一点我注意到我的代码。它将为40个整数分配空间,可以是4个字节,8个字节或其他东西,具体取决于您正在处理的机器的体系结构。如果你真的只想要40个字节的整数,那么做这样的事情:

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 a char data type or an unsigned char if you need to. One other thing that I noticed. The malloc function returns a void pointer type which is compatible with all pointers, so there is no need to do a typecast on a malloc.

或者,如果需要,可以使用char数据类型或unsigned char。我注意到的另一件事。 malloc函数返回一个与所有指针兼容的void指针类型,因此不需要对malloc进行类型转换。

#4


0  

Well arrays in C aren't bounded so, a few options, the most common:

C中的井阵列没有限制,一些选项,最常见的:

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 the closest to what you seem to mean to do:

最后一个是最接近你的意思:

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

while((*start) != -1)
    start++;

I would suggest reading more on how pointers in C work. You don't appear to have a very good grasp of it. Also, remember that C takes off the training wheels. Arrays aren't bounded or terminated in a standard way, and a pointer (address in memory) will never be NULL after iterating through an array, and the contents a pointer is pointing to could be anything.

我建议阅读更多有关C中指针如何工作的内容。你似乎没有很好地掌握它。另外,请记住C取下训练轮。数组不以标准方式绑定或终止,并且在遍历数组之后指针(内存中的地址)永远不会为NULL,并且指针指向的内容可以是任何内容。

#1


7  

There are two issues here.

这里有两个问题。

A single ptr++ skips as many bytes as the type of element it points to.

单个ptr ++跳过与其指向的元素类型一样多的字节。

Here the type is int, so it would skip 4 bytes each time (assuming a 32 bit machine since integer is 4 bytes (32 bits) there).

这里的类型是int,所以每次跳过4个字节(假设32位机器,因为整数是4个字节(32位))。

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

如果要遍历所有40个字节(一次一个字节),请使用char数据类型进行迭代(或者将int *类型转换为char *然后递增)

The other problem is your loop termination.

另一个问题是你的循环终止。

There is no one putting a NULL at the end here, so your loop would keep running (and pointer advancing forward) until it runs into may be a null or goes out of your allotted memory area and crashes. The behavior is undefined.

没有人在这里放置一个NULL,所以你的循环将继续运行(并且指针向前推进),直到它遇到可能是null或者超出你分配的内存区域并崩溃。行为未定义。

If you allocated 40 bytes, you have to terminate at 40 bytes yourself.

如果分配了40个字节,则必须自己终止40个字节。

Update:

Based upon a comment cum down vote to the original question, it is worth mentioning that type casting the result of malloc is not a good idea in C. The primary reason is that it could potentially tamper a failed allocation. It is a requirement in C++ though. The details can be found in the exact same question on SO. Search "casting return value of malloc"

基于对原始问题的评论和结果投票,值得一提的是,类型转换malloc的结果在C中不是一个好主意。主要原因是它可能潜在地篡改失败的分配。但它是C ++的一项要求。详细信息可以在SO上的完全相同的问题中找到。搜索“malloc的返回值”

#2


6  

First of all, you should be allocating ints correctly:

首先,您应该正确分配ints:

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

Then you can use array subscripting:

然后你可以使用数组下标:

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++ ;
}

#3


1  

Arrays represent 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 though, there is no error checking by C on the bounds of the array, so if you walk off the end of the memory block, you can do all kinds of things that you didn't intend and more than likely will end up with some sort of memory fault or segmentation error. Since your int can be variable size, I would use this code instead:

数组表示连续的内存块。由于数组的名称基本上是指向第一个元素的指针,因此您可以使用数组表示法来访问块的其余部分。但请记住,C对数组的边界没有错误检查,所以如果你走出内存块的末尾,你可以做各种你不想要的事情,而且很可能最终会某种内存故障或分段错误。由于你的int可以是可变大小,我会使用这个代码:

int *start;
int i;

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

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

Something like that will work nicely. The way that you are doing it, at least from the code that you posted, there is no way to stop the loop because once it exceeds the memory block, it will keep going until it runs into a NULL or you get a memory fault. In other words, the loop will only exit if it runs into a null. That null may be within the block of memory that you allocated, or it may be way beyond the block.

这样的东西会很好用。你正在这样做的方式,至少从你发布的代码中,没有办法停止循环,因为一旦它超过内存块,它将继续运行,直到它遇到NULL或你得到内存错误。换句话说,循环只有在遇到null时才会退出。该null可能位于您分配的内存块中,或者可能超出块。

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

编辑:有一点我注意到我的代码。它将为40个整数分配空间,可以是4个字节,8个字节或其他东西,具体取决于您正在处理的机器的体系结构。如果你真的只想要40个字节的整数,那么做这样的事情:

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 a char data type or an unsigned char if you need to. One other thing that I noticed. The malloc function returns a void pointer type which is compatible with all pointers, so there is no need to do a typecast on a malloc.

或者,如果需要,可以使用char数据类型或unsigned char。我注意到的另一件事。 malloc函数返回一个与所有指针兼容的void指针类型,因此不需要对malloc进行类型转换。

#4


0  

Well arrays in C aren't bounded so, a few options, the most common:

C中的井阵列没有限制,一些选项,最常见的:

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 the closest to what you seem to mean to do:

最后一个是最接近你的意思:

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

while((*start) != -1)
    start++;

I would suggest reading more on how pointers in C work. You don't appear to have a very good grasp of it. Also, remember that C takes off the training wheels. Arrays aren't bounded or terminated in a standard way, and a pointer (address in memory) will never be NULL after iterating through an array, and the contents a pointer is pointing to could be anything.

我建议阅读更多有关C中指针如何工作的内容。你似乎没有很好地掌握它。另外,请记住C取下训练轮。数组不以标准方式绑定或终止,并且在遍历数组之后指针(内存中的地址)永远不会为NULL,并且指针指向的内容可以是任何内容。