如何知道指针参数是否已在C上作为静态指针或动态指针传递? [英] How to know if a pointer parameter has been passed as static pointer or dynamic pointer, on C ?

查看:87
本文介绍了如何知道指针参数是否已在C上作为静态指针或动态指针传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道C语言或C ++,如果我能在函数内知道正式指针输入/输出参数的真实参数,让我们说一个字节数组或字符数组,已经使用了一个定义大小的数组(静态指针)或用调用者的calloc(动态指针)创建的指针。形式参数可以被定义为数组或指针。



我需要知道,因为在第二种情况下我可以重新分配内存以返回,相同的参数,与原始分配空间相比更大的字符串。



我必须这样做因为我将C#上的现有库转换为C,并且首先它必须和原版完全一样,所以不要责骂我。



我道歉,因为我不知道正确的名称是什么我称之为真实参数的类型有人可能会感到困惑,但我不知道。



示例>

I want to know on C language or C++, if I can know within a function if the real parameter of a formal pointer input/output parameter, let''s say to a byte array or char array, has been passed using an array of defined size (static pointer) or a pointer created with calloc (dynamic pointer) from the caller. The formal parameter could have been defined as an array or as a pointer.

I need to know that because in the second case I could reallocate the memory to return, on this same parameter, a bigger size string compared to the original allocated space.

I have to do this because I am converting an existing library on C# to C, and at first it has to be exactly as the original, so dont scold at me.

I apologize because I dont know the right names for what I called the types of real parameters and someone can be confused but I don know.

Example >

void FunctionName( char sCadenaHexa[], int SizeOfCadena )
{
  ... How I know how was called .... 
}



使用数组定义的大小或静态调用指针:


Called using "array defined size" or "static pointer" :

char sRealCadena[100] = "something";
FunctionName(  sRealCadena, 100 );



使用指针或动态指针调用:


Called using a pointer or "dynamic pointer" :

char *sRealCadena = malloc( 100, sizeof(char) );
FunctionName( sRealCadena, 100 );



当然我会尝试重新设计添加的功能另一个参数作为输出字符串paremeter,但作为第一种方法我必须坚持当前的设计。



如果线程重复我抱歉,但我没找到它。



提前Thanx


Of course I will try to redesign the function adding another parameter as the output string paremeter, but as first approach I have to stick to the current design.

If the thread is repeated Im sorry, but I didnt find it.

Thanx in advance

推荐答案

这是一个非常有趣的问题,但这不是C的方式或者C ++指针有效。没有一种可靠的方法可以知道指针是否指向内存的静态区域堆(例如某些平台中的数据段)。原则上,有可能是一些(通常是原始的)平台,但没有通用的方式,这种方式不可能存在。



如果你想一想,你会发现这实际上是好的,正确的决定。这些语言是从内存类型中抽象出来的。它允许实现语言和使用这些语言编译的应用程序是自定义的。它还允许将相同的算法应用于不同类型的指针。用户可以使用不同类型的分配和释放,尤其是在C ++中,用户可以在其中定义自定义 new delete 运营商。如果通常使用子分配器和其他自定义内存管理工具。



而不是找到指出指针之间区别的方法,你应该更好地设计你的代码正常。如果某个方法作为分配的结果返回指针,则它应该是用于释放内存的方法的补充。做分配的一方应该总是解除分配。您不应该允许诸如在创建具有某些方法的库时分配的情况,以及使用该库的代码解除分配。这是一个常见的设计错误,完全杀死平台兼容性并带来其他威胁。避免它。



-SA
This is a pretty interesting question, but this is not how C or C++ pointers work. There is no a reliable way of knowing if the pointer points to the heap of static region of memory (such as "data segment" in some platforms). In principle, it is possible is some (usually primitive) platforms, but there is no a universal way, and such way cannot exist.

If you think about it, you will see that this is actually good, a right decision. These languages are abstracted from the "types of memory". It allows for implementation of the languages and the applications compiled using those languages to be custom. It also allows to apply the same algorithms to different kinds of pointers. A user can use different types of allocation and deallocation, especially in C++, where the user can define custom new and delete operators. If is very usual to use sub-allocators and other custom memory management facilities.

Instead of finding the method of telling the difference between pointers, you should better design your code properly. If some method returns pointer as a result of allocation, it should be complement by the method used to deallocate memory. The side doing allocation should always deallocate. You should not allow the situations such as when you create a library with some methods are allocating, and the code using the library deallocates. This is a usual design mistakes which totally kills platform compatibility and presents other threats. Avoid it.

—SA


从未考虑过它 - 这是一个令人讨厌的要做的事情,但我可以看到你的问题。



这有帮助吗? [ ^ ]
Never thought about it before - it''s a nasty thing to do but I can see your problem.

Does this help? [^]


甚至不是rtti会帮助你,正如你所看到的:



Even not rtti would help you, as you can see:

// rtti.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <malloc.h>
#include <stdlib.h>
#include <typeinfo>

int _tmain(int argc, _TCHAR* argv[])
{
    char buffer[256];
    char *p1=buffer;
    char *p2=(char *)malloc(512);
    char *p3=new char[1024];

    //std::type_info
    _tprintf(_T("typename %S\n"),typeid(buffer).name());
    _tprintf(_T("typename %S\n"),typeid(p1).name());
    _tprintf(_T("typename %S\n"),typeid(p2).name());
    _tprintf(_T("typename %S\n"),typeid(p3).name());

    free(p2);
    delete [] p3;
    return 0;
}


这篇关于如何知道指针参数是否已在C上作为静态指针或动态指针传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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