malloc

C API design: Who should allocate?

What is the proper/preferred way to allocate memory in a C API? I can see, at first, two options: 1) Let the caller do all the (outer) memory handling: myStruct *s = malloc(sizeof(s)); myStruct_init(s); myStruct_foo(s); myStruct_destroy(s); free(s); The _init and _destroy functions are necessary since some more memory may be alloc...

malloc only allocates 4 bits?

I'm trying to make some c programs, ut i'm stuck at the malloc command. This is my code: #include <stdlib.h> #include <iostream> #include "Oef1.h" using namespace std; some methode clled by main{ int ** q=NULL; int m=read(q); } int read(int ** q){ int m=3; int n=...

ipad scrollview causing malloc of 40x the image's size

Bear with me as I am new to obj-c but I have a UIScrollView and a segmented button that switches between 2 images that are presented in the scrollview. Each image is large and roughly 500Kb, but they are each causing allocations of 20+ MB and crashing my app. - (void)viewDidLoad { [bvpiscrollview setContentSize:CGSizeMake(768, 248...

What happens when there is a request for memory block which is not a power of 2?

Suppose we do a malloc request for memory block of size n where 2 ^k !=n for k>0. Malloc returns us space for that requestted memory block but how is the remainig buffer handled from the page. I read Pages are generally blocks of memory which are powers of two. Wiki states the following: Like any method of memory allocation, the heap...

Should I free allocated memory on abnormal termination?

Hello everybody! My program (a text-mode web browser) is dynamically allocating memory. I do free unneeded blocks during runtime, of course. And I do free everything before normal termination - so that memory leak checkers won't give me false positives (and to be flexible should major refactorings ever become needed). Now, what I do n...

electric-fence with pthread

I'm working on a multithreaded (pthread based) project. The project uses a library that I'm writing. In order to check it I linked it with -lefence and it gave me SIGSEGV. After a lot of time spent in figuring out what's wrong, I finally decided to search the error on the library, even if it's functionality is extremely simple. As test...

EXC_BAD_ACCESS While calling malloc function

hi I have the following function but sometimes it fails on the malloc function call and I don't know the reason,I thought that it may be due to lack of heap size but I have monitored the heap and I understood that I have enough space available for memory allocation when malloc fails ,can any one suggest anything to me char *substr(...

mmap regions allocating from reserved stack space?

In our product we use a malloc implementation that relies exclusively on mmap for memory allocation. We also do a fair use of allocaing. We've just encountered a problem where mmap will allocate regions that should be reserved to stack space (thus causing very bad things to happen when one of our larger alloca's spills into the malloc'...

About the fate of malloc()ed arrays of arrays.

Hello everyone - my first question on Stackoverflow. Let me start with a bit of code. It's a bit repetitive so I'm going to cut out the parts I repeat for different arrays (feel free to ask for the others). However, please ignore the code in preference to answering the Qs at the bottom. Firstly: thank you to answerers in advance. Se...

Concept of allocating free memory

Hello, I would like to know how memory allocation on the lowest levels works. For example if program wants to create some long array or anything else, how it will ask for the memory, how does the program ensure itself that it does not take memory same other program is using. I would be very grateful if someone would bring some light fo...

glibc detected *** ./a.out: free(): invalid pointer:

when i compiled(c++ program) in linux i am getting following error pls help me glibc detected *** ./a.out: free(): invalid pointer:0x0804878d *** ======= Backtrace: ========= /lib/libc.so.6[0xbd5f18] /lib/libc.so.6(__libc_free+0x79)[0xbd941d] /usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0x3233fe1] ./a.out(__gxx_personality_v0+0x100)[0x8048514]...

Segmentation fault when returning a struct

I am trying to do a pretty simple thing - it is reading a file and then turning it into a char** splitting it into lines. However when I return a struct containing the char** and size i get Segmentation fault. I read here: http://stackoverflow.com/questions/3047163/c-segmentation-fault-before-during-return-statement that it's probably "m...

Strange behavior (SEGFAULT) of a C program using stdargs (va_start)

Hi folks, I wrote a variadic C function which mission is to allocate the needed memory for a buffer, and then sprintf the args given to this function in that buffer. But I'm seeing a strange behavior with it. It works only once. If I have two calls for this function it segfaults. #include <stdio.h> #include <string.h> #include <stdlib....

Alligned memory allocation

I have the following assignment: Write a function in C that allocates block of memory and returns a pointer to the start of the memory under these conditions: All addresses in the block are divisible by 32 Allocated at least the number of the bytes required Every cell in the block is initialized to zero No global varia...

How to use a C++ string in a structure when malloc()-ing the same structure?

I wrote the following example program but it crashes with segfault. The problem seems to be with using malloc and std::strings in the structure. #include <iostream> #include <string> #include <cstdlib> struct example { std::string data; }; int main() { example *ex = (example *)malloc(sizeof(*ex)); ex->data = "hello world"; std::c...

Physical location aware user space memory allocation in Linux (memory controller affinity)

I want to test the performance variations that may happen if memory is allocated and accessed from different physical CPUs and from different embedded memory controllers for a 64 bit, 2 CPU, 16 core Intel Xeon 5500 CPU based server. (Dell T710) Looking at the vendor white paper i can see each physical CPU has 3 independent memory contro...

Using malloc() for unique pointers

So, I have some function returning a pointer, or NULL on error. Now I'd like to add another possible value for a different error/condition. I heard this mentioned in some other answer before - using malloc() to create a unique pointer that will serve as a possible value for such functions to return (so now the can return a proper pointer...

casting void pointers

Possible Duplicates: Do I cast the result of malloc? Should I explicitly cast malloc()'s return value? Hello, gcc 4.4.4 c89 Normally I don't cast the return result from a malloc call. int *int_ptr = NULL; int_ptr = malloc(sizeof(int)); However, I have read on here, that if you cast it can hide errors. How does it hide ...

Boolean array using "char"

Hi, I made an object that actually represents an array of 8 booleans stored in a char. I made it to learn something more about bitwise operators and about creating your own objects in C. So I've got two questions: Can I be certain if the below code always works? Is this a good implementation to make an object that can't get lost in C, ...

Non 8 Byte aligned memory access causes memory access violation on iPhone 3GS with iOS 4.0

Hi, consider the following Objective-C++ iPhone Application (TestMemAppDelegate.mm). It crashes with an EXC_BAD_ACCESS on the iPhone (3GS with iOS 4.0). It works fine in the Simulator. It is clearly a memory alignment thing, because it works fine on the iPhone if the "DataA" struct starts on a 8 Byte border. Can anyone explain the caus...