I want to store 5 names without wasting 1byte , so how can allocate memory using malloc
-
3Do you consider the null character at the end of the string a "waste" – Karthik T Dec 26 '12 at 05:38
-
What kind of environment requires you to do this? On embedded processors I could see, but on normal computers, but it's hardly necessary since the wastage would be insignificant for only 5 names. – Gustavo Litovsky Dec 26 '12 at 05:51
-
@nagaradderKanetesh - Your problem can't be solved using malloc() because, if you call malloc(1) then malloc() allocates 16 bytes of memory (16 in my machine) so malloc() in my machine always allocates memory in multiples of 16. so if I want 45 bytes to store 5 names then malloc allocates 48 bytes (16 * 3); Using malloc or calloc or simply dynamic allocation of memory may waste certain bytes which is unavoidable. – Viswesn Dec 26 '12 at 07:14
8 Answers
That's for all practical purposes impossible, malloc
will more often than not return blocks of memory bigger than requested.

- 80,396
- 20
- 159
- 169
-
okay without malloc how can we store 5 names without memory wastage – nagaradderKantesh Dec 26 '12 at 05:40
-
-
@nagaradderKantesh: Pointers that point where? How would you know that your declaration does not cause _padding_ bytes to be added somewhere else? – K-ballo Dec 26 '12 at 05:43
-
like this , char*a[5] = {"stack", "Overflow", "hello", "world", "India"} here each pointer is pointing to corresponding name i think here no memory wastage . – nagaradderKantesh Dec 26 '12 at 05:46
-
@nagaradderKantesh: I guess you want to ask something slightly different than what you actually asked... There you are wasting 20 or 40 bytes depending on your platform pointer size. This would waste even less (but still waste): `char *bunchoftext = "stack\0Overflow\0hello\0world\0India";` – K-ballo Dec 26 '12 at 05:47
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21698/discussion-between-nagaradderkantesh-and-k-ballo) – nagaradderKantesh Dec 26 '12 at 05:52
-
@nagaradderKantesh: Assuming 64bit pointers, your version takes `73 bytes` while mine takes only `41 bytes`... – K-ballo Dec 26 '12 at 05:52
-
let me explain clearly, suppose if we declare a[5][11] , it will allocate 55 bytes and if the string length is less than 11 bytes, let me say "Hello" 6 bytes are going to be waste and if the string size is more than 11 bytes we cant store, in that approach [char*a[5]] there is no wastage of memory na, – nagaradderKantesh Dec 26 '12 at 05:55
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.

- 9,258
- 4
- 36
- 53
#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;
}

- 146
- 1
- 8
-
[Please don't cast the return value of `malloc()`, in C](http://stackoverflow.com/a/605858/28169). Also, `sizeof (char)` is silly. – unwind Feb 19 '13 at 16:52
If you don't want to waste any byte to store names, you should dynamically allocate a double array (char) in C.
A double array in C can be implemented as a pointer to a list of pointers.
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);
....
Now you can save the name to its corresponding position in the array without allocating more space, even though names might have different lengths.

- 985
- 8
- 12
You have to take double pointer concept and then have to put your name character by character with increment of pointer address and then you are able to save all 5 names so as you are able to save your memory.
But as programmer you should not have to use this type of tedious task you have to take array of pointers to store names and have to allocate memory step by step. This is only for the concept of storing names but if you are dealing with large amount of data then you have to use link list to store all data.

- 39,603
- 20
- 94
- 123

- 302
- 1
- 8
When you malloc a block, it actually allocates a bit more memory than you asked for. This extra memory is used to store information such as the size of the allocated block.

- 30,829
- 42
- 119
- 173
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.

- 69,361
- 7
- 100
- 138
-
memory wastage means suppose if we declare two dimensional array to store 5 strings i.e char a[5][11] here for each string we are reserving 11 bytes it means total 55 bytes , suppose if the string is less than 11 bytes say "Hello", here 6 bytes will be waste to avoid this we will declare array of pointers like this , char*a[5] = {"stack", "Overflow", "hello", "world", "India"} here each pointer is pointing to corresponding name i think here no memory wastage – nagaradderKantesh Dec 26 '12 at 06:26
-
Why would you declare a 2d array 5x11, knowing you're not going to fill it up with all data? Just do a ragged array and be done with it. – Edwin Buck Dec 26 '12 at 23:12
-
no, suppose if i don't know the string size and if i am reading the strings of random sizes then how can i store them without wasting the memory .... – nagaradderKantesh Dec 27 '12 at 13:08