c中一个有趣的问题 [英] an interesting problem in c

查看:76
本文介绍了c中一个有趣的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我的程序中有以下结构


struct sample

{

char * string;

int string_len;

};


struct sample ** ptr; //作为二维数组


现在,基于运行时输入,我将为ptr

分配内存,这样我就有了1d指向结构示例的指针数组,每个

成员都会持有一个字符串,现在问题是

以下是运行时输入,比如说一个文件,


string1

string2

string3


现在,我需要分配ptr来保存三组struct sample和

将每个字符串存储在一个成员的字符串中(分配后),

类似


struct sample ptr =(struct sample **)malloc(< value> * sizeof(struct

sample *));

for (i = 0; i< value; i ++)

{

struct sample ptr [i] =(struct sample *)malloc(sizeof(struct sample));

sample [i] .string =(char *)malloc(100); //每个都会拿着字符串

}

我面临的问题是我不知道提前的价值

(我将在文件结束后才知道),请给我任何

建议如何做到这一点?

解决方案

sa*****@yahoo.co.in 写道:


我的程序中有以下结构


struct sample {

char * string;

int string_len;

};


struct sample ** ptr; //作为二维数组


现在,基于运行时输入,我将为ptr

分配内存,这样我就有了1d指向结构示例的指针数组,每个

成员都会持有一个字符串,现在问题是

以下是运行时输入,比如说一个文件,


string1

string2

string3


现在,我需要分配ptr来保存三组struct sample和

将每个字符串存储在一个成员的字符串中(分配后),

类似


struct sample ptr =(struct sample **)malloc(< value> * sizeof(struct sample *));

for(i = 0; i< value ; i ++){

struct sample ptr [i] =(struct sample *)malloc(sizeof(struct sample));

sample [i] .string =(char *)malloc的(100); //每个都会拿着字符串

}


我面临的问题是我不知道提前的价值
(我将在文件结束后才知道),请给我任何

建议如何做到这一点?



我已经编写了一个完整的库来解决这个问题(和其他人一起讨论)。你可以在这里阅读:<

http://bstring.sf.net/


-

Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/



sa ***** @ yahoo.co.in 写道:


大家好,


我的程序中有以下结构


struct sample

{

char * string;

int string_len;

};


struct sample ** ptr; //作为二维数组


现在,基于运行时输入,我将为ptr

分配内存,这样我就有了1d指向结构样本的指针数组,每个

成员将持有一个字符串,...



[snip]


我面临的问题是我不知道预付的价值

(我只会在结束后知道文件),请给我任何

建议如何做?



你可以看一下涉及realloc()的东西 - 为指针数组分配一个初始的

合理值并增加一个合适的

当它溢出时的因素。


你可以看一下两遍方法 - 要么读一次文件要

确定如何您拥有的数据量很大,数组大小然后再次读取;

或最初读取数据到另一个结构 - 比如一个链接的

列表 - 并将其转换为数组一旦掌握了所有数据。



>

你可以看看涉及realloc()的东西 - 为指针数组分配一个初始的

合理值,当它溢出时增加一个合适的

因子。


您可以查看两遍方法 - 读取文件一次

确定您拥有多少数据,调整数组大小然后读取ag ain;

或最初将数据读入另一个结构 - 比如一个链接的

列表 - 并在获得所有数据后将其转换为数组。



我无法负担两次解析文件的费用,我也无法拥有

临时列表来保存所有值,首先是时间

预定,第二个是内存预定的代价很高......所以我猜b $ b我只剩下realloc(),我从未使用过。 ..确实

当我决定

重新分配时,它确保保留旧块的值?


Hi Everyone,

I have the following structure in my program

struct sample
{
char *string;
int string_len;
};

struct sample **ptr; //serves as two-dimentional array

Now, based on run-time input, i will be allocating the memory for ptr
such that i have a 1d array of pointers to structure sample, and each
member will be holding a string, now the problem i have is that the
following is the run time input, say in a file,

string1
string2
string3

now, i need to allocate ptr to hold three set of struct sample and
store each of the string in one member''s string (after allocation),
something like

struct sample ptr = (struct sample**)malloc(<value>*sizeof(struct
sample*));
for(i=0;i<value;i++)
{
struct sample ptr[i] = (struct sample*)malloc(sizeof(struct sample));
sample[i].string = (char*)malloc(100); //each will hold the string
}
The problem i''m facing is that i don''t know the value in advance
(which i will know only after the end of the file), please give me any
suggestions as to how to do it?

解决方案

sa*****@yahoo.co.in wrote:

I have the following structure in my program

struct sample {
char *string;
int string_len;
};

struct sample **ptr; //serves as two-dimentional array

Now, based on run-time input, i will be allocating the memory for ptr
such that i have a 1d array of pointers to structure sample, and each
member will be holding a string, now the problem i have is that the
following is the run time input, say in a file,

string1
string2
string3

now, i need to allocate ptr to hold three set of struct sample and
store each of the string in one member''s string (after allocation),
something like

struct sample ptr = (struct sample**)malloc(<value>*sizeof(struct sample*));
for(i=0;i<value;i++) {
struct sample ptr[i] = (struct sample*)malloc(sizeof(struct sample));
sample[i].string = (char*)malloc(100); //each will hold the string
}

The problem i''m facing is that i don''t know the value in advance
(which i will know only after the end of the file), please give me any
suggestions as to how to do it?

I''ve written an entire library that solves this exact problem (and
others.) You can read about it here:

http://bstring.sf.net/

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/



sa*****@yahoo.co.in wrote:

Hi Everyone,

I have the following structure in my program

struct sample
{
char *string;
int string_len;
};

struct sample **ptr; //serves as two-dimentional array

Now, based on run-time input, i will be allocating the memory for ptr
such that i have a 1d array of pointers to structure sample, and each
member will be holding a string, ...

[snip]

The problem i''m facing is that i don''t know the value in advance
(which i will know only after the end of the file), please give me any
suggestions as to how to do it?

You could look at something involving realloc() - allocating an initial
reasonable value for the array of pointers and increasing by a suitable
factor when it overflows.

You could look at a two-pass approach - either read the file once to
determine how much data you have, size the array and then read again;
or read the data initially into a different structure - say a linked
list - and transform it to an array once you have all the data.


>
You could look at something involving realloc() - allocating an initial
reasonable value for the array of pointers and increasing by a suitable
factor when it overflows.

You could look at a two-pass approach - either read the file once to
determine how much data you have, size the array and then read again;
or read the data initially into a different structure - say a linked
list - and transform it to an array once you have all the data.

I can''t afford parsing the file two times and neither can i have a
temp list to hold all the values, first one is costly from time
prespective and second one is costly from memory prespective... so i
guess i''m left out with only realloc(), i have never used one... does
it make sure that old block''s values are retained when i decide to
re-alloc?


这篇关于c中一个有趣的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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