N
neojia
hi,
I encountered such a strange problem and worked on it for days.
Although I can see what is going on in my code, I still cannot provide
a perfect solution for this problem.
The problem happens when you are going to assign the return value of
function F2 to a local variable V1 of function F1. F1 calls F2. And V1
will points another chunk of memory by calling function F3, which will
be called inside F2. The return value is actrually assigned to the
original address before calling F2.
I think compiler should allow callee to modify the caller's local
variable as they want and also use it as a left-hand variable of the
assignment of the return function.
The following chunk of code also has the same problem.
Does anyone encounter such problem before? Is there any perfect
solution?
Thanks,
Neo
#include <stdio.h>
#include <malloc.h>
#include <string.h>
unsigned long * Root[1];
struct STR {
int size;
unsigned long * data[1];
};
void bar() {
if (Root[0] != NULL) {
*Root[0] = (unsigned long)malloc(sizeof(struct STR) + 5 *
sizeof(char *));
printf("Root changes to 0x%[%08lx]\n", *Root[0]);
}
}
unsigned long * foo() {
unsigned long * ch = (unsigned long *)malloc(sizeof(32));
bar();
if (ch != NULL) {
printf("foo returns address 0x[%08lx]\n", ch);
return ch;
}
}
int main(int argc, char ** argv) {
struct STR * str_ptr;
int len = 5, i = 0;
str_ptr = malloc(sizeof(struct STR) + 5 * sizeof(char *));
memset(str_ptr, 0, sizeof(struct STR) + 5 * sizeof(char *));
Root[0] = &str_ptr;
for (i = 0; i < len; i++) {
unsigned long * tmp = NULL;
printf("************** Loop %d***************\n", i);
printf("Before calling foo(), str_ptr points 0x[%08lx]\n",
str_ptr);
printf("Before calling foo(), str_ptr->data[%d] points to
%08lx\n", i, str_ptr->data);
tmp = &(str_ptr->data);
printf("Before calling foo(), str_ptr->data[%d] address is
%08lx\n", i, tmp);
str_ptr->data = foo();
printf("After calling foo(), str_ptr points 0x[%08lx]\n",
str_ptr);
printf("After calling foo(), str_ptr->data[%d] points to
%08lx\n", i, str_ptr->data);
printf("After calling foo(), data at address %08lx is %08lx\n",
tmp, *tmp);
}
I encountered such a strange problem and worked on it for days.
Although I can see what is going on in my code, I still cannot provide
a perfect solution for this problem.
The problem happens when you are going to assign the return value of
function F2 to a local variable V1 of function F1. F1 calls F2. And V1
will points another chunk of memory by calling function F3, which will
be called inside F2. The return value is actrually assigned to the
original address before calling F2.
I think compiler should allow callee to modify the caller's local
variable as they want and also use it as a left-hand variable of the
assignment of the return function.
The following chunk of code also has the same problem.
Does anyone encounter such problem before? Is there any perfect
solution?
Thanks,
Neo
#include <stdio.h>
#include <malloc.h>
#include <string.h>
unsigned long * Root[1];
struct STR {
int size;
unsigned long * data[1];
};
void bar() {
if (Root[0] != NULL) {
*Root[0] = (unsigned long)malloc(sizeof(struct STR) + 5 *
sizeof(char *));
printf("Root changes to 0x%[%08lx]\n", *Root[0]);
}
}
unsigned long * foo() {
unsigned long * ch = (unsigned long *)malloc(sizeof(32));
bar();
if (ch != NULL) {
printf("foo returns address 0x[%08lx]\n", ch);
return ch;
}
}
int main(int argc, char ** argv) {
struct STR * str_ptr;
int len = 5, i = 0;
str_ptr = malloc(sizeof(struct STR) + 5 * sizeof(char *));
memset(str_ptr, 0, sizeof(struct STR) + 5 * sizeof(char *));
Root[0] = &str_ptr;
for (i = 0; i < len; i++) {
unsigned long * tmp = NULL;
printf("************** Loop %d***************\n", i);
printf("Before calling foo(), str_ptr points 0x[%08lx]\n",
str_ptr);
printf("Before calling foo(), str_ptr->data[%d] points to
%08lx\n", i, str_ptr->data);
tmp = &(str_ptr->data);
printf("Before calling foo(), str_ptr->data[%d] address is
%08lx\n", i, tmp);
str_ptr->data = foo();
printf("After calling foo(), str_ptr points 0x[%08lx]\n",
str_ptr);
printf("After calling foo(), str_ptr->data[%d] points to
%08lx\n", i, str_ptr->data);
printf("After calling foo(), data at address %08lx is %08lx\n",
tmp, *tmp);
}