在C++中,在栈上,为一个简单的变量分配一个内存地址,这样我们就可以使用一个指针来包含这个内存来指向它; 那么指针也分配了一个内存地址?
如果是的话,我们可以指针指针吗?
NSDictionary *topic = [spaces objectAtIndex:i];
NSInteger topicid = [topic valueForKey:@"TOPICID"];
Run Code Online (Sandbox Code Playgroud)
当我运行这个并打印出主题时,我得到以下内容:
Printing description of topic:
<CFDictionary 0xdb2a70 [0x30307a00]>{type = mutable, count = 2, capacity = 12, pairs = (
10 : <CFString 0xdb5300 [0x30307a00]>{contents = "TOPICID"} = 29
12 : <CFString 0xdb53a0 [0x30307a00]>{contents = "TOPICNAME"} = <CFString 0xdb5360 [0x30307a00]>{contents = "zzzzzzzz"}
)}
Run Code Online (Sandbox Code Playgroud)
但是,当我查看topicid时,值始终是内存地址.我究竟做错了什么?
最近我离开学校几天,想在C++中做一个处理内存地址的小程序实验.
我想看看,如果当前正在运行的程序(让它称之为程序A)创建了一个指向堆中int对象的指针,可以被另一个程序看到并进行修改(程序B).
所以对于程序A,这是我的基本代码:
// Program A
#include <iostream>
using namespace std;
int main()
{
// Pointer to an int object in the heap
int *pint = new int(15);
// Display the address of the memory in heap
cout << pint << endl;
// Display the value stored in that address
cout << *pint << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
计划A的产出:
0x641030
15
Run Code Online (Sandbox Code Playgroud)
对于程序B,我查看了如何通过以下链接分配特定的内存地址:http: //www.devx.com/tips/Tip/14104
计划B的代码是:
// Program B
#include <iostream>
using namespace std;
int main()
{
// assign address …Run Code Online (Sandbox Code Playgroud) 虽然字符串是动态的,所以它没有任何确定的大小,因此当我在s [0]之前得到s [1]时,C++将如何计算其偏移地址.
例如int a [2]
0000:1000 a[0]
0000:1004 a[1]
Run Code Online (Sandbox Code Playgroud)
程序:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
string s[2];
cin>>s[1];
cout<<s[1]<<endl;
cin>>s[0];
cout<<s[0]<<endl;
}
Run Code Online (Sandbox Code Playgroud) 是否有一个标准的C++函数,它类似于Address-of运算符&,我可以用作stl实体的函数对象,如std :: transform和std :: compose1?
为什么“无效”实际上返回“ 6”?
void Void (int &ref){
ref++;
}
int main () {
int test= 5;
Void(test);
cout << test; // is 6
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不太了解这里发生了什么。使用Void(test)我没有通过测试的地址。为什么不使用“ Void(&test);”?为什么ref ++将1加到值5?不应该是“ * ref ++”吗?
我正在以自学成才的方式学习C编程.我知道必须始终静态或动态地初始化数字指针地址.
但是,我还没有读到有关初始化char指针地址的强制要求.
例如,这段代码是正确的,还是需要指针地址初始化?
char *p_message;
*p_message = "Pointer";
Run Code Online (Sandbox Code Playgroud) 我正在学习Python,并希望确认Objective-C和Swift中的某种行为.
测试如下:
蟒蛇
def replace(list):
list[0] = 3
print(list)
aList = [1, 2, 3]
print(aList)
replace(aList)
print(aList)
Run Code Online (Sandbox Code Playgroud)
Objective-C的
- (void)replace:(NSMutableArray *)array {
array[0] = @1;
NSLog(@"array: %@, address: %p\n%lx", array, array, (long)&array);
}
NSMutableArray *array = [@[@1, @2, @3] mutableCopy];
NSLog(@"original: %@, address: %p \n%lx", array, array, (long)&array);
[self replace:array];
NSLog(@"modified: %@, address: %p \n%lx", array, array, (long)&array);
Run Code Online (Sandbox Code Playgroud)
迅速
var numbers = [1, 2, 3]
let replace = { (var array:[Int]) -> Void in
array[0] = 2
print("array: \(array) address:\(unsafeAddressOf(array …Run Code Online (Sandbox Code Playgroud) 这对我来说不是一个问题,但我刚开始考虑它,我想我会问.为什么每次运行程序时返回不同的值(0x3759F8B0 - 0x100)?
有一次它说00AFFD00而下一个说006FFD48
test = 0x3759F8B0 - 0x100;
cout << &test << endl;
Run Code Online (Sandbox Code Playgroud) memory-address ×10
c++ ×6
pointers ×4
memory ×2
allocation ×1
arrays ×1
bit ×1
c ×1
functor ×1
hex ×1
ios ×1
iphone ×1
nsdictionary ×1
nsinteger ×1
objective-c ×1
offset ×1
parameters ×1
reference ×1
stl ×1
string ×1
swift ×1