调整一个char [X]到char [Y]在运行时 [英] Resizing a char[x] to char[y] at runtime

查看:153
本文介绍了调整一个char [X]到char [Y]在运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OK,我希望我正确地说明这一点。
我有一个结构:

OK, I hope I explain this one correctly. I have a struct:

typedef struct _MyData
{
   char Data[256];
   int  Index;
} MyData;

现在,我遇到一个问题。大部分时间MyData.Data的是具有256行,但在某些情况下,我需要扩大字符的它可以容纳不同尺寸的量。
我不能用一个指针。
有什么办法在运行时调整数据?怎么样?
code是AP preciated。

Now, I run into a problem. Most of the time MyData.Data is OK with 256, but in some cases I need to expand the amount of chars it can hold to different sizes. I can't use a pointer. Is there any way to resize Data at run time? How? Code is appreciated.

感谢

编辑:

虽然我所有的意见非常感谢,对也许试试这个...或做,或者你是东什么是错的......评论没有帮助。 code是这里的帮助。请,如果你知道答案张贴code。

While I am very thankful for all the comments, the "maybe try this..." or "do that", or "what you are dong is wrong..." comments are not helping. code is the help here. PLease if you know the answer post the code.

1不能使用指针。请不要试图找出为什么,我只是不能
2-结构体被注入到另一个程序的存储器。这就是为什么。没有指针。

1- cannot use pointers. please don't try to figure out why, i just can't 2- the struct is being injected into another program's memory. that's why. no pointers.

,作为一个有点粗糙这里抱歉,但我在这里问的问题,因为我已经尝试了所有认为可能工作不同的方法。
再次,我期待code。在这一点上,我不感兴趣可能​​工作......或你有没有考虑这个......

sorry for being a bit rough here but i asked the question here because I already tried all the different approaches that thought might work. Again, i am looking for code. At this point I am not interested in "might work..." or " have you considered this..."

再次感谢你和我的道歉

编辑2

为什么这个集合回答?

推荐答案

您可以使用一个灵活的数组成员

You can use a flexible array member

typedef struct _MyData
{
   int  Index;
   char Data[];
} MyData;

这样你就可以分配的空间适量

So that you can then allocate the right amount of space

MyData *d = malloc(sizeof *d + sizeof(char[100]));
d->Data[0..99] = ...;

后来,你可以免费的,分配的内存另一块,使一个指针迈德特指向它,此时你将在多/少的元素灵活的数组成员(的realloc )。请注意,您必须保存在某个地方长了。

Later, you can free, and allocate another chunk of memory and make a pointer to MyData point to it, at which time you will have more / less elements in the flexible array member (realloc). Note that you will have to save the length somewhere, too.

在pre-C99次,没有一个灵活的数组成员: CHAR数据[] 简单地视为不完整的类型的数组,而编译器会抱怨这一点。在这里,我建议你两种可能的方式在那里

In Pre-C99 times, there isn't a flexible array member: char Data[] is simply regarded as an array with incomplete type, and the compiler would moan about that. Here i recommend you two possible ways out there


  • 使用指针:的char *数据并使其指向分配的内存。这会不会是因为使用嵌入式阵列一样方便,因为你可能会需要有两个分配:一个是结构,一个用于存储的指针指向。您也可以在栈上分配的,而不是结构,如果在你的程序的情况允许这样做。

  • 使用 CHAR数据[1] 代替,但把它当作好像它是更大的,因此,它覆盖了整个分配的对象。这是正式不确定的行为,而是一种常见的技术,所以它可能是安全与你的编译器使用。

  • Using a pointer: char *Data and make it point to the allocated memory. This won't be as convenient as using the embedded array, because you will possibly need to have two allocations: One for the struct, and one for the memory pointed to by the pointer. You can also have the struct allocated on the stack instead, if the situation in your program allows this.
  • Using a char Data[1] instead, but treat it as if it were bigger, so that it overlays the whole allocated object. This is formally undefined behavior, but is a common technique, so it's probably safe to use with your compiler.

这篇关于调整一个char [X]到char [Y]在运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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