简体   繁体   English

如果我增加一个数组变量会怎么样?

[英]What happens if I increment an array variable?

I know that it isn't safe to change a pointer's address if it lays on the heap because freeing it later would cause some trouble, but is it safe to do that if the pointer is declared on the stack? 我知道如果将指针的地址放在堆上是不安全的,因为以后释放它会导致一些麻烦,但如果在堆栈上声明指针,那么这样做是否安全?

I'm talking about something like this: 我在谈论这样的事情:

char arr[] = "one two three";
arr++;
//or arr--;

I hope I got that right by referring to a char array as a pointer. 我希望通过引用char数组作为指针来实现这一点。

you cannot change the address of an array. 你不能改变数组的地址。 It will give a compile time error. 它会给出编译时错误。 have a look: http://codepad.org/skBHMxU0 看看: http//codepad.org/skBHMxU0

EDIT: 编辑:
the comments made me realize your true intent: something like: 评论让我意识到你的真实意图:类似于:

char *ptr = "one two three";
ptr++;

There is no problem with it. 它没有问题。 the string "one two three" is a constant, and you can freely modify ptr , but note you might have troubles later finding the start of this string again... [but memory leak will not occur] 字符串“一二三”是一个常量,你可以自由地修改ptr ,但请注意你以后再找到这个字符串的开头可能有麻烦... [但不会发生内存泄漏]

As a thumb rule, you are responsible for the memory you specifically allocated using malloc/new, and the compiler is responsible for the rest. 作为一个拇指规则,您负责使用malloc / new专门分配的内存,编译器负责其余部分。

As written, your code won't work because the operand of ++ must be a modifiable lvalue, and array expressions are not modifiable lvalues. 如上所述,您的代码将无法工作,因为++的操作数必须是可修改的左值,并且数组表达式不是可修改的左值。

What you can do is something like this: 可以做的是这样的:

char arr[] = "one two three";
char *ptr = arr;  // ptr points to the leading 'o'
...
ptr++; // ptr now points to 'n'

As far as safety is concerned, you can still run into problems if the result of incrementing or decrementing ptr causes it to point to memory outside of the array, which may or may not be safe to access or modify. 就安全性而言,如果增加或减少ptr的结果导致它指向阵列外部的内存,则可能仍会遇到问题,这可能是也可能不安全访问或修改。

The line: 这条线:

char arr[] = "one two three";

creates an array (which means its location is FIXED), it is not the same thing as a pointer as a pointers location can be moved. 创建一个数组(这意味着它的位置是固定的),它与指针不同,因为指针位置可以移动。 The array is default-initialized with the contents "one two three"; 数组默认初始化,内容为“一二三”; You can change the contents of the array as log as it doesn't grow in size, but you can't move arr. 您可以将数组的内容更改为日志,因为它不会增大,但您无法移动arr。

arr++;

would thus be an error. 因此会出错。 You could, however, do: 但是,您可以这样做:

char* ptr = arr;
ptr++;

to get to the second character of the arr array. 到达arr数组的第二个字符。

You can not increment an array variable / array name, however you can access any element of the array by using array name / array variable. 您不能递增数组变量/数组名称,但是您可以使用数组名称/数组变量来访问数组的任何元素。 That is the reason why pointers came in to picture,. 这就是指针进入图片的原因。 Array addresses are unmodifiable For example, 数组地址是不可修改的例如,

int k[3]={1,4,3};
printf("%d", *(k+1));  // compiles without any warning o/p is 4
printf("%d", *k++); //Will throw an error, Trying to modify an unmodifiable value

here in above snippet, Line 2: We are not incrementing array variable, however we are fetching the value of 1st indexed element in the array by using array address. 在上面的代码片段中,第2行:我们没有递增数组变量,但是我们使用数组地址获取数组中第一个索引元素的值。

It's not where the pointer lives (heap or stack), but where the memory the pointer points to lives. 它不是指针所在的位置(堆或堆栈),而是指针指向的内存所在的位置。

Memory on the stack is cleaned up automatically, you have to remember (keep pointers to) memory on the heap, because it's your responsibility to clean it up. 堆栈上的内存会自动清理,你必须记住堆上的内存(保持指针),因为清理它是你的责任。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 初始化大小为变量的数组时会发生什么? - What happens when I initialize an array having the size as a variable? 数组[1]会发生什么 - What happens to array[1] 如果我调用等待通知的条件变量会发生什么 - What happens if i call wait on a notified condition variable 如果我不删除动态创建的变量会怎样? - What happens if I do not delete a dynamically created variable? 如果我得到了一个与符号一起传递的变量的引用,将会发生什么? - What happens if I get the reference of a variable passed with an ampersand? 如果我通过引用捕获局部变量会发生什么,并且它超出范围? - What happens if I capture a local variable by reference, and it goes out of scope? 当我在 C++ 中打印一个未初始化的变量时会发生什么? - What happens when I print an uninitialized variable in C++? 如果我将负值赋给无符号变量会怎样? - What happens if I assign a negative value to an unsigned variable? 如果在动态/静态分配的双精度数组中输入字符,会发生什么情况? - What happens if I enter a character to a dynamically/statically allocated double array? 当我访问数组元素时,硬件级别会发生什么? - What happens at a hardware level when I access an element of an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM