Solved

memory allocation

Posted on 1998-08-12
12
270 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

757 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

17 Experts available now in Live!

Get 1:1 Help Now