Re: Strange problem after changing local variable of caller in the callee !!!




<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);
}



.



Relevant Pages

  • Strange problem after changing local variable of caller in the callee !!!
    ... original address before calling F2. ... Does anyone encounter such problem before? ... unsigned long * foo() { ... unsigned long * tmp = NULL; ...
    (comp.lang.c)
  • Re: FindNextFile skippes web directories
    ... I encounter a problem that directories that serve also as a local web site ... root, in my file system are skipped by the above function. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: KTH Kerberos 4 wont compile
    ... There is one way in which building stuff as root is more reliable: ... > the use of LD_LIBRARY_PATH is blocked for the root user, ... >> function call and prepare to encounter many similar problems elsewhere ... My previous encounters with the Kerberos authors and this kind ...
    (comp.os.linux.security)
  • Re: chords and notes how to find the root and name of the chord
    ... is when I encounter a group of notes that I'm trying to interpret in terms ... I start by treating each note as the root. ... which one might be a more likely prospect than the others. ...
    (alt.guitar)
  • Re: [SLE] K3b permissions under SuSE 9.1
    ... >>I'm one of the many folks to encounter the problem of not being able to run ... > 2.6.8 kernel, which may have some backporting done in the kernel in ... With the suid root, why would it also be necessary to change the group, I ...
    (SuSE)