Solved

memory allocation

Posted on 1998-08-12
12
271 Views
Last Modified: 2010-04-15
I am allocation memory using the following code

char *list;
list = (char *)malloc(sizeof(char)*10);

What is returned to me is is 14 instead of ten....If I use any number I get 4 extra characters.  So when I go to print out the contents of list I get four characters that I can not use.  The only I have found a way around this is to make a temp char array and store my values in there then strcpy them into the char *.  The problem is that I am bound to a static array again.  Does anyone have any ideas on how or why this happens and how I might resovle the issue??  Thanks.
0
Comment
Question by:meetze
12 Comments
 
LVL 3

Expert Comment

by:danny_pav
ID: 1252032
So you are given 14 bytes.  How do you know that you are given 14 bytes?  When you allocate memory like this, it is up to you to use it without overwriting the end of the buffer.  You have to keep track of its size.  Being given 14 bytes should not be a problem.
0
 

Author Comment

by:meetze
ID: 1252033
Well the problem is that if I print out the contents of list those extra bytes show up and I am using this to write info out to files and the extra characters show up.  I know that I am given 14 bytes cause I can see it in the debugger.
0
 
LVL 5

Expert Comment

by:ecw
ID: 1252034
I take it you mean malloc is returning enough storage for 14 characters rather than 10.  Not an unreasonable thing for malloc to do.  Memory returned by malloc must be aligned on a boundary suitable for storing the CPU's most restrictive type.  Often this is a double, though *86's can (I believe,) access unaligned data, albeit slower than aligned data.
This alignment restriction, implies that the smallest amount of memory malloc can return is a multiple of the alignment bounday (8 bytes), plus malloc has its own overhead, it needs somewhere to keep track of the size of each memory region allocated, typically this is hidden in front of the address returned, which places further restrictions on the size of the smallest allocatable chunk.
When using malloc, always regard the size of the chunk returned to be the same as what you requested, never larger.
Further, there are no guarantees regarding the contents of a memory chunk returned by malloc, treat it as junk.  It's up to you what you put in there, but until you do the contents are meaningless.
0
 
LVL 3

Expert Comment

by:danny_pav
ID: 1252035
the answer is simple:  malloc 10 bytes and treat the returned buffer as if it were 10 bytes.
0
 

Author Comment

by:meetze
ID: 1252036
I try to treat as if it were ten bytes but when I print out the contents of the pointer those extra four bytes show up as garbage and the only way I can get the pointer to print out the valid characters is to store my info in a static arrary temporaliy then strcpy it into the pointer but then i run into the staic arrary problem again.
0
 
LVL 3

Expert Comment

by:danny_pav
ID: 1252037
print it out as 10 bytes.
for (int i = 0; i < 10; ++i) {
printfunc(list[i]);
}
0
Do email signature updates give you a headache?

Constantly trying to correctly format email signatures? Spending all of your time at every user’s desk to make updates? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today!

 

Author Comment

by:meetze
ID: 1252038
thanks danny_pav....you need to answer the questio for me to give you the points
0
 
LVL 3

Expert Comment

by:rmichels
ID: 1252039
I just want to jump in here.  Curious.  How are you printing your buffer?  Are you sure you are not having a problem with a missing NULL terminator for a string?
0
 

Author Comment

by:meetze
ID: 1252040
i am sure
0
 
LVL 2

Expert Comment

by:kellyjj
ID: 1252041
I think that if you clear out the buffer before you put any values into then you should be ok.  I , for example,  when dealing with strings  always clear them out.  strcpy(mystr,"");

this usually does the job.  Plus I know what is there.  
0
 
LVL 32

Accepted Solution

by:
jhance earned 50 total points
ID: 1252042
When you do:

char *list;
list = (char *)malloc(sizeof(char)*10);

The C standard says that the malloc must return a buffer capable of containing no less than the amount requested otherwise an error is to be returned.  So the run-time library might give you a megabyte when you ask for 10 chars.  Most run-times have a minimum size that they give you even if you only ask for 1 byte.  It's done for efficiency purposes.  If you need to be sure that your string you are storing doesn't run over the end, you can just fill the buffer with 0 before using it:

char *list;
list = (char *)malloc(sizeof(char)*10);
if(list != NULL)
    memset(list, 0, sizeof(char)*10);


or you could even use calloc() instead of malloc() to get a pre-cleared buffer.
0
 
LVL 1

Expert Comment

by:newexpert
ID: 1252043
Static arrays are cleared to 0 on initialisation.

Both Static array and memory returned from malloc uses the same memory so you might as well stick with static array.

As long as you have a terminating '\0' characters at the end of the string the garbage will not appear.  Otherwise even if your malloc returns 10 chars, when you print the string it will go on until it hits a '\0'.

Remember you are using C.  So if you get char *c=(char *)malloc(sizeof(char)*2); and go strcpy(c, "long long long long long string"); the computer will do it and quite happily destroy other data in memory.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now