I am working on some kind of private code that I cannot show, but I have made some code examples to describe my problem:
main.c:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint32_t (*FuncPtr)(char *s, uint32_t n);
uint32_t doSomething(char *s, uint32_t n){
printf("n is %d in doSomething\n", n);
printf("s is '%s' in doSomething\n\n", s);
return 0;
}
uint32_t func1(char *s, uint32_t n){
printf("n is %d in func1\n", n);
printf("s is '%s' in func1\n\n", s);
return doSomething(s, n);
}
uint32_t func2(char *s, uint32_t n){
printf("n is %d in func2\n", n);
printf("s is '%s' in func2\n\n", s);
return doSomething(s, n);
}
uint32_t func3(char *s, uint32_t n){
printf("n is %d in func3\n", n);
printf("s is '%s' in func3\n\n", s);
return doSomething(s, n);
}
void perform(FuncPtr fp, char *s, uint32_t n){
printf("fp is location in %p\n", fp);
printf("n is %d in perform\n", n);
printf("s is '%s' in perform\n\n", s);
fp(s, n);
}
int main(void) {
srand(time(NULL));
uint32_t r = (uint32_t)(rand() % 3 + 1);
uint32_t n = (uint32_t)(rand() %100);
char *s = "some string here";
printf("r is %d\n", r);
printf("n is %d in main\n", n);
printf("s is '%s' in main\n\n", s);
switch(r)
{
case 1:
perform(func1, s, n);
break;
case 2:
perform(func2, s, n);
break;
case 3:
perform(func3, s, n);
break;
}
return 0;
}
Here's the call stack:
0 func1(s=0x9abcdef0, n=0 << Dead >>) [..\main.c:9,1]
1 perform(fp=0x12345678, s=0x9abcdef0, n=42) [..\main.c:21,1]
2 main() [..\main.c:25,10]
As you can see from above, nwhich is passed in func1, << dead >>and does not match the value in perform, although the pointer salways rests at the same address, Why is this happening? I did not find a useful description of what a dead variable is anywhere. Is there any fix I can make for this code, or is it due to a lot of compiler and memory issues that I cannot implement here?