传递给C函数不正确的指针值 [英] incorrect pointer value passed to a C function

查看:98
本文介绍了传递给C函数不正确的指针值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有哪些不正确的值被作为一个C程序的参数传递给函数的错误。它的工作方式是,我宣布一个静态指针的typedef-ED数据结构作为全局变量。还有就是这个变量初始化为初始化函数。此函数分配存储器,初始化数据字段和返回指针。事情是这样的:

 静态my_type * my_ptr;
...
无效的init(无效){
   my_ptr = init_my_type();
}

功能 init_my_type 是pretty直截了当:

 无效* init_my_type(无效){
   my_type * X =的malloc(sizeof的(my_type);
   x轴与GT; ARG1 = 0;
   ... //更多领域的初始化
   返回X;
}

后来我用 my_ptr 作为参数传递给另外一个函数:

 无效do_stuff(无效){
   FUNC(my_ptr);
}

我的问题是,我在赛格的胆量FAULT FUNC 当一些数据结构中的数据的 my_ptr 点进行访问。

当我运行调试我得到一个好看的十六进制值当我打破了 init_my_type

 (GDB)完成
返回的值是$ 26(无效*)0x79b6c0

后来, do_stuff 函数中, my_ptr 具有相同的十六进制值:

 (GDB)打印my_ptr
$ 26 =(my_type *)0x79b6c0

但是,当我在打破FUNC 它得到的说法有一个完全不同的值。

 断点2,FUNC(ARG = 0x1388)

我型双关的指针所有的地方,但我没有看到,应该改变的地址在内存中,他们指出,对不对?此外,功能 FUNC 声明在线,但为什么要影响呢?这一切都似乎是正确的我 - 这是完全可能的,我在做一些愚蠢的事,我没有看到

下面是简化code的一个完整的程序。在现实中,所有这些功能不要被所谓的主力,而是通过动态加载的辅助功能。不过,我没有看到地址如何通过 my_ptr 指出,当传递给 FUNC 应该改变。

 的#includestdlib.h中
的#includestdio.h中typedef结构{_type
  INT * I​​1;
  浮* F1;
} my_type;静态my_type * my_ptr;无效* init_my_type(无效){  my_type * X =的malloc(sizeof的(my_type));
  X-GT&; F1 =的malloc(sizeof的(浮动));
  X-GT&; I1 =的malloc(sizeof的(INT));
  x轴与GT; F1 [0] = 123.456;
  x轴与GT; I1 [0] = 789;  返回X;}无效的init(无效){  my_ptr = init_my_type();}内嵌无效FUNC(无效* ARG){  my_type * X =(my_type *)精氨酸;
  的printf(%d个%F \\ n,* X-GT&; I1,* x轴和GT; F1);}无效do_stuff(无效){  FUNC(my_ptr);}
诠释主要(无效){  在里面();
  做东西();
}


解决方案

下面是不是问题的原因(也不能,因为静态全局默认情况下初始化为零)。虽然基本思想仍然具有现实意义:传递的指针是否真的得到了初始化相同


胡乱猜测:

 静态my_type * my_ptr;

难道说这条线是一些头文件的一部分?因为你有一个全球性的 my_ptr 在包含这个头每一个源文件。

既然你写的,这是一个非常大的项目,我认为你分开code,放入多个源文件。假设初始化函数是比使用功能不同的源文件,那么这将意味着他们访问不同的指针。虽然有一个的init 与被初始化的交易中,有一个 FUNC 正在使用的未初始化。

要检查这一点,你应该打印全局指针变量的地址(&放大器; my_ptr )。在这两个功能

I have a bug in which an incorrect value gets passed as an argument to a function in a C program. The way it works is, I declare a static pointer to a typedef-ed data structure as a global variable. There is an initialization function where this variable is initialized. This function allocates memory, initializes data fields and returns the pointer. Something like this:

static my_type *my_ptr;
...
void init(void){
   my_ptr = init_my_type();
}

The function init_my_type is pretty straight forward:

void *init_my_type(void){
   my_type *x = malloc(sizeof(my_type);
   x->arg1 = 0;
   ... // more field initializations
   return x;
}

Later on I use my_ptr as an argument to another function:

void do_stuff(void){
   func(my_ptr);
}

The problem I have is that I seg fault in the guts of func when some of the data in the data structure that my_ptr points to is accessed.

When I run the debugger I get a nice looking hex value when I break on the init_my_type:

(gdb) finish
Value returned is $26 (void *) 0x79b6c0

Later, inside the do_stuff function, my_ptr has the same hex value:

(gdb) print my_ptr
$26 = (my_type *) 0x79b6c0

but, when I break on func the argument it gets has a totally different value.

Breakpoint 2, func(arg=0x1388)

I am type-punning pointers all over the place, but I don't see that that should change the address in memory that they point to, right? Also, the function func is declared inline but why should that affect this? This all seems correct to me -- it is entirely possible that I'm doing something stupid that I don't see.

Here is a complete program of the simplified code. In reality, all these functions don't get called by main, but by dynamically loaded helper functions. Still, I don't see how the address pointed to by my_ptr should change when passed to func.

#include "stdlib.h"
#include "stdio.h"

typedef struct _type{
  int *i1;
  float *f1;
}my_type;

static my_type *my_ptr;

void *init_my_type(void){

  my_type *x = malloc(sizeof(my_type));
  x->f1 = malloc(sizeof(float));
  x->i1 = malloc(sizeof(int));
  x->f1[0] = 123.456;
  x->i1[0] = 789;

  return x;

}

void init(void){

  my_ptr = init_my_type();

}

inline void func(void *arg){

  my_type *x = (my_type *)arg;
  printf("%d %f\n", *x->i1, *x->f1);

}

void do_stuff(void){

  func(my_ptr);

}


int main(void){

  init();
  do_stuff();


}

解决方案

The following is not the cause of the issue (and can't, since static globals are initialised to zero by default). Though the basic idea is still relevant: whether the passed pointer is really the same that got initialised.


A wild guess:

static my_type *my_ptr;

Could it be that this line is part of some header file? Because then you have a global my_ptr in every source file that includes this header.

Since you wrote that this is a very large project, I assume that you separated the code and put it into multiple source files. Assuming the init function is in a different source file than the using function, then this would mean they're accessing different pointers. While the one init deals with gets initialised, the one func is using is uninitialised.

To check this you should print the address of the global pointer variable (&my_ptr) in both functions.

这篇关于传递给C函数不正确的指针值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆