gpt4 book ai didi

c - 在 D 中使用 apr_array_push 追加数组

转载 作者:太空宇宙 更新时间:2023-11-04 02:18:21 25 4
gpt4 key购买 nike

我正在使用 svn 在 D 中编写一个程序,我遇到了一些我无法弄清楚如何转换为 D 语法的东西。我尝试了一下,但它出现了段错误。

我试图在 C 中完成的示例:

svn_auth_provider_object_t provider;
providers = apr_array_make(pool, 1, sizeof(svn_auth_provider_object_t*));
svn_auth_get_simple_provider2(&provider, null, null, pool);
*(svn_auth_provider_object_t**)apr_array_push (providers) = provider;
svn_auth_open(&auth_baton, providers, pool);

据我所知,这段代码工作得很好。我发现几个例子几乎完全相同。这是我在 D 中复制它的尝试:

svn_auth_provider_object_t provider;
providers = apr_array_make(pool, 1, svn_auth_provider_object_t.sizeof);
svn_auth_get_simple_provider2(&provider, null, null, pool);
void* newSlot = apr_array_push(m);
newSlot = provider;
svn_auth_open(&auth_baton, providers, pool);

这会在 svn_auth_open 上引发段错误。我最好的猜测是 provider 的内容不会在 newSlot 持有的指针处结束。我不确定为什么会这样。

补充代码:

/// Code taken from Apache's APR libary which is licensed under Apache License, Version 2.0

struct apr_array_header_t {
apr_pool_t* pool;
int elt_size;
int nelts;
int nalloc;
char* elts;
};

struct svn_auth_provider_object_t
{
svn_auth_provider_t *vtable;
void *provider_baton;
}

APR_DECLARE(void *) apr_array_push(apr_array_header_t *arr)
{
if (arr->nelts == arr->nalloc) {
int new_size = (arr->nalloc <= 0) ? 1 : arr->nalloc * 2;
char *new_data;

new_data = apr_palloc(arr->pool, arr->elt_size * new_size);

memcpy(new_data, arr->elts, arr->nalloc * arr->elt_size);
memset(new_data + arr->nalloc * arr->elt_size, 0,
arr->elt_size * (new_size - arr->nalloc));
arr->elts = new_data;
arr->nalloc = new_size;
}

++arr->nelts;
return arr->elts + (arr->elt_size * (arr->nelts - 1));
}

#define APR_ARRAY_IDX(ary,i,type) (((type *)(ary)->elts)[i])
#define APR_ARRAY_PUSH(ary,type) (*((type *)apr_array_push(ary)))

void
svn_auth_open(svn_auth_baton_t **auth_baton,
apr_array_header_t *providers,
apr_pool_t *pool)
{
svn_auth_baton_t *ab;
svn_auth_provider_object_t *provider;
int i;

/* Build the auth_baton. */
ab = apr_pcalloc(pool, sizeof(*ab));
ab->tables = apr_hash_make(pool);
ab->parameters = apr_hash_make(pool);
ab->creds_cache = apr_hash_make(pool);
ab->pool = pool;

/* Register each provider in order. Providers of different
credentials will be automatically sorted into different tables by
register_provider(). */
for (i = 0; i < providers->nelts; i++)
{
provider_set_t *table;
provider = APR_ARRAY_IDX(providers, i, svn_auth_provider_object_t *);

/* Add it to the appropriate table in the auth_baton */
table = apr_hash_get(ab->tables,
provider->vtable->cred_kind, APR_HASH_KEY_STRING);
if (! table)
{
table = apr_pcalloc(pool, sizeof(*table));
table->providers
= apr_array_make(pool, 1, sizeof(svn_auth_provider_object_t *));

apr_hash_set(ab->tables,
provider->vtable->cred_kind, APR_HASH_KEY_STRING,
table);
}
APR_ARRAY_PUSH(table->providers, svn_auth_provider_object_t *)
= provider;
}

*auth_baton = ab;
}

我最好的猜测是段错误发生在 APR_ARRAY_IDX 中。其他所有内容都有一个有效的内存地址。

附加信息:这也不起作用:

providers = apr_array_make(pool, 1, svn_auth_provider_object_t.sizeof);
svn_auth_provider_object_t* newSlot = cast(svn_auth_provider_object_t*) apr_array_push(providers);
svn_auth_get_simple_provider2(&newSlot, null, null, pool);
svn_auth_open(&auth_baton, providers, pool);

所以这排除了我的理论,即数据没有到达正确的位置。但是,如果我注释掉数组推送行并将数组发送为空,它就可以正常工作。所以,这不是段错误:

providers = apr_array_make(pool, 1, svn_auth_provider_object_t.sizeof);
svn_auth_open(&auth_baton, providers, pool);

我也知道它不是 svn_auth_get_simple_provider2 因为这也是段错误...

providers = apr_array_make(pool, 1, svn_auth_provider_object_t.sizeof);
svn_auth_provider_object_t* newSlot = cast(svn_auth_provider_object_t*) apr_array_push(providers);
svn_auth_get_ssl_server_trust_file_provider(&newSlot, pool);
svn_auth_open(&auth_baton, providers, pool);

最佳答案

乍一看,对 newSlot 的第一个赋值已失效,因为下一行将其覆盖,将其设置为 null。尝试:

auto newSlot = cast(svn_auth_provider_object_t**)apr_array_push(m);
*newSlot = provider;

如果不是 void*,这将导致编译时错误。

我认为 C 版本中唯一无效的 D 部分是强制转换。

关于c - 在 D 中使用 apr_array_push 追加数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4198635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com