2

我想在不浪费 1byte 的情况下存储 5 个名称,那么如何使用 malloc 分配内存

4

8 回答 8

2

出于所有实际目的,这是不可能的,malloc通常会返回比请求更大的内存块。

于 2012-12-26T05:32:51.627 回答
1

If you know the cumulative length of the five names, let's call it length_names, you could do a

void *pNameBlock = malloc(length_names + 5);

Then you could store the names, null terminated (the +5 is for the null termination), one right after the other in the memory pointed to by pNameBlock.

char *pName1 = (char *) pNameBlock;

Store the name data at *pName1. Maybe via char *p = *pName1; You can then write byte by byte (following is pseudo-codeish).

*p++ = byte1;
*p++ = byte2;
etc.

End with a null termination:

*p++ = '\0';

Now set

char *pName2 = p;

and write the second name using p, as above.

Doing things this way will still waste some memory. Malloc will internally get itself more memory than you are asking for, but it will waste that memory only once, on this one operation, getting this one block, with no overhead beyond this once.

Be very careful, though, because under this way of doing things, you can't free() the char *s, such as pName1, for the names. You can only free that one pointer you got that one time, pNameBlock.

If you are asking this question out of interest, ok. But if you are this memory constrained, you're going to have a very very hard time. malloc does waste some memory, but not a lot. You're going to have a hard time working with C this constrained. You'd almost have to write your own super light weight memory manager (do you really want to do that?). Otherwise, you'd be better off working in assembly, if you can't afford to waste even a byte.

I have a hard time imagining what kind of super-cramped embedded system imposes this kind of limit on memory usage.

于 2012-12-26T05:56:37.180 回答
1
#include <stdio.h>
#include<stdlib.h>
int main()
{
    int n,i,c;
    char *p[5];/*declare a pointer to 5 strings for the 5 names*/
    for(i=0;i<5;i++)
    {
        n=0;
        printf("please enter the name\n" );/*input name from the user*/
        while((c=getchar())!='\n')
        n++;/*count the total number of characters in the name*/



        p[i]= (char *)malloc(sizeof(char)*n);/*allocate the required amount of memory for a name*/
        scanf("%s",p[i]);
    }
    return 0;
}
于 2012-12-26T08:55:55.803 回答
1

您必须采用双指针概念,然后必须逐个字符地输入您的名称,并增加指针地址,然后您就可以保存所有 5 个名称,这样您就可以节省内存。

但是作为程序员,您不应该使用这种乏味的任务,您必须使用指针数组来存储名称,并且必须逐步分配内存。这仅适用于存储名称的概念,但是如果您要处理大量数据,则必须使用链接列表来存储所有数据。

于 2012-12-27T15:54:01.753 回答
1

如果您不想浪费任何字节来存储名称,则应在 C 中动态分配一个双精度数组(char)。

C 中的双精度数组可以实现为指向指针列表的指针。

char **name; // Allocate space for a pointer, pointing to a pointer (the beginning of an array in C)
name = (char **) malloc (sizeof(char *) * 5); // Allocate space for the pointer array, for 5 names
name[0] = (char *) malloc (sizeof(char) * lengthOfName1); // Allocate space for the first name, same for other names
name[1] = (char *) malloc (sizeof(char) * lengthOfName2);
....

现在,您可以将名称保存到数组中的相应位置,而无需分配更多空间,即使名称可能具有不同的长度。

于 2012-12-26T09:48:09.903 回答
0

当你 malloc 一个块时,它实际上分配的内存比你要求的多一点。这个额外的内存用于存储信息,例如分配块的大小。

于 2012-12-26T05:35:10.927 回答
0

将名称编码为二进制并将它们存储在字节数组中。

于 2012-12-26T05:40:29.143 回答
0

What is "memory waste"? If you can define it clearly, then a solution can be found.

For example, the null in a null terminated string might be considered "wasted memory" because the null isn't printed; however, another person might not consider it memory waste because without it, you need to store a second item (string length).

When I use a byte, the byte is fully used. Only if you can show me how it might be done without that byte will I consider your claims of memory waste valid. I use the nulls at the ends of my strings. If I declare an array of strings, I use the array too. Make what you need, and then if you find that you can rearrange those items to use less memory, decide that the other way wasted some memory. Until then, you're chasing a dream which you haven't finished.

If these five "names" are assembly jump points, you don't need a full string's worth of memory to hold them. If the five "names" are block scoped variables, perhaps they won't need any more memory than the registers already provide. If they are strings, then perhaps you can combine and overlay strings; but, until you come up with a solution, and a second solution to compare the first against, you don't have a case for wasted / saved memory.

于 2012-12-26T06:01:04.620 回答