c - 变量上的地址没有改变
问题描述
我正在尝试使用变量的地址。但是,变量在每次运行时都具有相同的地址。既然我在不同的地址之间移动,为什么变量地址仍然相同?当我尝试复制结果时,它仍然给我相同的值。
typedef struct Node{
char *color; /* Color of the light */
int **details; /*holds the brightness and size*/
struct Node* next;
}Node;
Node * light_info(char *filename){
//Declaring two pointers so that the addresses are not lost while executing
//the program
Node *tempNode = NULL;
Node *tempHead = NULL;
//Parameters
FILE *fptr;
int i =0;
char fileInfo[MAXVALUE];
fptr = fopen(filename,"r");
if(fptr != NULL){
while(fgets(fileInfo,sizeof(fileInfo),fptr)!= NULL){
//Initalizing the value of token as NULL
char * token = NULL;
//Mallocs the space for the node
tempNode = malloc(sizeof(Node));
//Check condition
if(tempNode == NULL){
printf("Sorry cannot create a space on the heap;\n");
perror("Error at line 61");
exit(0);
}
printf("NODE : %p \n",tempNode);
// For the color
token = strtok(fileInfo,",");
char* tempValue= malloc(50);
tempValue = token;
printf("%s \n",tempValue);
printf("Temp value : %p \n",&tempValue);
tempNode->color = tempValue;
//Seprating 2 places for the details section
tempNode->details = malloc(sizeof(int)*2);
//For the brightness
token = strtok(NULL,",");
int tempValueOfBrightness = atoi(token);
printf("%p \n",&tempValueOfBrightness);
printf("%d \n",tempValueOfBrightness);
tempNode->details[0] = &tempValueOfBrightness;
//For the size
token = strtok(NULL,"\n");
int tempValueOfSize = atoi(token);
printf("%d \n",tempValueOfSize);
printf("%p \n",&tempValueOfSize);
tempNode->details[1] = &tempValueOfSize;
//Adding the current address to the head to the node
tempNode->next = tempHead;
//Making the head as the current address
tempHead = tempNode;
printf("Temp \n");
printf("NODE temp : %p \n",tempNode);
printf("NODE head: %p \n",tempHead);
i++;
}
}else{
printf("The file cannot be opened. :< \n");
perror("Error at line 50");
}
turnLights(tempHead);
return tempHead;
}
}
我得到以下结果:
NODE : 0x7fffe5462ac0
blue
Temp value : 0x7fffeda80d28
0x7fffeda80d1c
2
0
0x7fffeda80d20
Temp
NODE temp : 0x7fffe5462ac0
NODE head: 0x7fffe5462ac0
NODE : 0x7fffe5462b40
green
Temp value : 0x7fffeda80d28
0x7fffeda80d1c
1
0
0x7fffeda80d20
Temp
NODE temp : 0x7fffe5462b40
NODE head: 0x7fffe5462b40
NODE : 0x7fffe5462bc0
red
Temp value : 0x7fffeda80d28
0x7fffeda80d1c
1
0
0x7fffeda80d20
Temp
NODE temp : 0x7fffe5462bc0
NODE head: 0x7fffe5462bc0
NODE : 0x7fffe5462c40
yellow
Temp value : 0x7fffeda80d28
0x7fffeda80d1c
2
0
0x7fffeda80d20
Temp
NODE temp : 0x7fffe5462c40
NODE head: 0x7fffe5462c40
NODE : 0x7fffe5462cc0
blue
Temp value : 0x7fffeda80d28
0x7fffeda80d1c
1
0
0x7fffeda80d20
Temp
NODE temp : 0x7fffe5462cc0
NODE head: 0x7fffe5462cc0
变量 tempValue 在我的所有运行中具有相同的地址。这对于其他变量也是一样的。当我运行代码时,我只得到地址 0x7ffeda80d28 和 0x7ffffeda80d1c。如何修复此错误?
解决方案
您正在获取分配在堆栈(指针)上的变量的地址。除非您从调用堆栈中的其他位置(例如调用函数或另一个函数)调用,否则这些变量在函数调用中将具有相同的地址。指针的地址和它们指向的地址不相同。
为了说明差异(以及堆栈地址如何工作)......
#include <stdlib.h>
#include <stdio.h>
void f1(int x) {
int* p = &x;
printf("address of pointer (stack): %p\n", &p);
printf("pointer (stack): %p\n", p);
// change pointer to a heap address
p = malloc(sizeof(int));
printf("pointer (heap): %p\n", p);
printf("address of pointer (stack): %p\n", &p);
free(p);
}
void f2(int x) {
int y = x;
f1(x);
}
int main(int argc, char *argv[]) {
int x = 0;
printf("f1:\n");
f1(x);
printf("\nf1 (again):\n");
f1(x);
printf("\nf2:\n");
f2(x);
return 0;
}
输出...
f1:
address of pointer (stack): 0x7ffcdcc52df0
pointer (stack): 0x7ffcdcc52dfc
pointer (heap): 0x9486b0
address of pointer (stack): 0x7ffcdcc52df0
f1 (again):
address of pointer (stack): 0x7ffcdcc52df0
pointer (stack): 0x7ffcdcc52dfc
pointer (heap): 0x9486b0
address of pointer (stack): 0x7ffcdcc52df0
f2:
address of pointer (stack): 0x7ffcdcc52dd0
pointer (stack): 0x7ffcdcc52ddc
pointer (heap): 0x9486b0
address of pointer (stack): 0x7ffcdcc52dd0
请注意,当您分配tempValue
但为其分配值时,您也会泄漏内存strtok
。
token = strtok(fileInfo,",");
char* tempValue= malloc(50);
tempValue = token;
同样tempValue
是一个指针,您正在将值从堆地址更改为由strtok
. 所以返回的地址malloc
丢失了,内存泄漏了(因为它不能用 来返回到堆中free
)。通常,任何对 的调用都malloc
应该有对 的相应调用free
,否则您的程序正在泄漏内存。