Re: Strange problem after changing local variable of caller in the callee !!!
- From: "Rod Pemberton" <do_not_have@xxxxxxxxxxxxxxxxxxx>
- Date: Mon, 20 Feb 2006 16:47:49 -0500
<neojia@xxxxxxxxx> wrote in message
news:1140466197.132010.158970@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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'm confused by this. But, I think I understand from your program.
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?
I think the main problem is the way you defined Root.
Try out the following modified version of your program to see if it works
correctly.
Rod Pemberton
//----
#include <stdio.h>
#include <malloc.h>
#include <string.h>
//no array unsigned long * Root[1];
unsigned long * Root;
struct STR {
int size;
unsigned long * data[1];
};
void bar() {
//no array if (Root[0] != NULL) {
if (Root != NULL) {
//no cast *Root[0] = (unsigned long)malloc(sizeof(struct STR) + 5 *
sizeof(char *));
Root = malloc(sizeof(struct STR) + 5 * sizeof(char *));
//extra % printf("Root changes to 0x%[%08lx]\n", *Root[0]);
printf("Root changes to 0x[%08lx]\n", (unsigned long)Root);
}
}
unsigned long * foo() {
unsigned long * ch = (unsigned long *)malloc(sizeof(32));
bar();
if (ch != NULL) {
//cast printf("foo returns address 0x[%08lx]\n", ch);
printf("foo returns address 0x[%08lx]\n", (unsigned long)ch);
//misplaced return 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 *));
//cast,no array,no address Root[0] = &str_ptr;
Root = (unsigned long *)str_ptr;
for (i = 0; i < len; i++) {
unsigned long * tmp = NULL;
printf("************** Loop %d***************\n", i);
//cast printf("Before calling foo(), str_ptr points 0x[%08lx]\n",
str_ptr);
printf("Before calling foo(), str_ptr points 0x[%08lx]\n", (unsigned
long)str_ptr);
//cast printf("Before calling foo(), str_ptr->data[%d] points to
%08lx\n", i, str_ptr->data[i]);
printf("Before calling foo(), str_ptr->data[%d] points to %08lx\n",
i, (unsigned long)str_ptr->data[i]);
//cast tmp = &(str_ptr->data[i]);
tmp = (unsigned long *)&(str_ptr->data[i]);
//cast printf("Before calling foo(), str_ptr->data[%d] address is
%08lx\n", i, tmp);
printf("Before calling foo(), str_ptr->data[%d] address is %08lx\n",
i, (unsigned long)tmp);
str_ptr->data[i] = foo();
//cast printf("After calling foo(), str_ptr points 0x[%08lx]\n",
str_ptr);
printf("After calling foo(), str_ptr points 0x[%08lx]\n", (unsigned
long)str_ptr);
//cast printf("After calling foo(), str_ptr->data[%d] points to
%08lx\n", i, str_ptr->data[i]);
printf("After calling foo(), str_ptr->data[%d] points to %08lx\n",
i, (unsigned long)str_ptr->data[i]);
//cast printf("After calling foo(), data at address %08lx is
%08lx\n", tmp, *tmp);
printf("After calling foo(), data at address %08lx is %08lx\n",
(unsigned long)tmp, *tmp);
}
//missing return(0);
return(0);
}
.
- Follow-Ups:
- Re: Strange problem after changing local variable of caller in the callee !!!
- From: neojia@xxxxxxxxx
- Re: Strange problem after changing local variable of caller in the callee !!!
- From: Al Balmer
- Re: Strange problem after changing local variable of caller in the callee !!!
- References:
- Prev by Date: Re: Seriously struggling with C
- Next by Date: Re: Question to malloc
- Previous by thread: Strange problem after changing local variable of caller in the callee !!!
- Next by thread: Re: Strange problem after changing local variable of caller in the callee !!!
- Index(es):
Relevant Pages
|