Why is malloc'd char array not working but char p[256] is?

Inside a function, I create a char array which I pass to a library function that fills it with a value. I then pass this back to the caller. When I do it by creating an array with malloc (and then freeing it outside the function), it only fills the array with the first 3 characters. When i create it as a local array, it works (and simply warns me I'm passing a local array back out).

Yeah, I thought that sizeof() would be the same for a malloc'd array and a char[] array.

hmm... but if you are passing the array to fillArray(), the array decays to a pointer to its first element, so there would be no difference... unless your example of fillArray() excluded the size argument.

Originally Posted by Bjarne Stroustrup (2000-10-14)

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

Yeah, I thought that sizeof() would be the same for a malloc'd array and a char[] array.

It is not. Sizeof is resolved at compile time, and although the compiler may well understand the function malloc, it wouldn't understand if you actually want the size of the allocated memory or the size of the pointer. There are potentially non-standard functions that gives the size of the allocation, but you would be in an awkward position of dependancy on a particular compiler or C library if you start using them.

--
Mats

Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.

But of course you must know that returning a local variable like that is completely wrong, since the memory where that local variable resides is reused after the function returns and will eventually get overwritten with other stuff.

"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

But of course you must know that returning a local variable like that is completely wrong, since the memory where that local variable resides is reused after the function returns and will eventually get overwritten with other stuff.

As I pointed out in the third thread on the same subject by the same author - that's why having multiple threads is a bad thing.

--
Mats

Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.

Please don't ever do that ...elided... thing. Whenever you do that, you haven't done enough of what you should be doing to solve the problem yourself.

If you haven't eliminated the effects those parts have on your problem then you need to do more work to eliminate those parts as a possible cause before posting. Once you've determined that it isn't related to the problem, then your simplest complete compileable example that you post shouldn't contain it since it is no longer relevant. If you determine that it is part of the problem then you'd be wasting your time and ours by leaving it out.

Usually it is part of the problem. People have a habbit of leaving the part out that they think isn't the problem. If where they thought the problem was, actually was where the problem was, then they would already know the solution.

My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger

Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"