Link to home
Start Free TrialLog in
Avatar of meetze
meetze

asked on

memory allocation

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.
Avatar of danny_pav
danny_pav

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.
Avatar of meetze

ASKER

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.
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.
the answer is simple:  malloc 10 bytes and treat the returned buffer as if it were 10 bytes.
Avatar of meetze

ASKER

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.
print it out as 10 bytes.
for (int i = 0; i < 10; ++i) {
printfunc(list[i]);
}
Avatar of meetze

ASKER

thanks danny_pav....you need to answer the questio for me to give you the points
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?
Avatar of meetze

ASKER

i am sure
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.  
ASKER CERTIFIED SOLUTION
Avatar of jhance
jhance

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.