malloc

UIImageWriteToSavedPhotosAlbum with malloc_error

I have an NIB file with a button. When I click this button, the setWallpaper: selector is called. Everything works as expected (the image is saved), excepte by the error thrown by malloc. malloc: *** error for object 0x184d000: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug I've set a breakpo...

Is there any good reason to use 'malloc' in C++, when there is 'new'?

Possible Duplicate: In what cases do I use malloc vs new? Duplicate of: http://stackoverflow.com/questions/184537/in-what-cases-do-i-use-malloc-vs-new/ and http://stackoverflow.com/questions/808148/when-to-use-malloc-instead-of-new Does anyone have real life programming scenarios where using "malloc" instead of "new" in C++ w...

How do you dynamically allocate a contiguous 3D array in C?

In C, I want to loop through an array in this order for(int z = 0; z < NZ; z++) for(int x = 0; x < NX; x++) for(int y = 0; y < NY; y++) 3Darray[x][y][z] = 100; How do I create this array in such a way that 3Darray[0][1][0] comes right before 3Darray[0][2][0] in memory? I can get an initialization to work that g...

C malloc increase buffer size

this is part of my code which reads an http response. It's supposed to increase the buffer size if it runs out of room. But i keep getting access violations. It happens when copying the data to the new buffer: memcpy(tmp_alloc, rec, ResponseLength); Any help/suggestions are appreciated. #define SERVER_CHUNK 1024 char *rec = new char[10...

C-array to NSData and back

I'm attempting to save a c-style array of Vertex3D structs to an NSData object and get them back when reloading the app: NSData *vert = [NSData dataWithBytes:&vertices length:(sizeof(Vertex3D) * NUM_OF_VERTICES)]; This data is then saved and attempted to be read back into my c-array thusly: vertices = malloc(sizeof(Vertex3D) * NU...

Dynamic memory allocation for 3D array

Possible Duplicates: Malloc a 3-Dimensional array in C? dynamic allocation/deallocation of 2D & 3D arrays How can i allocate 3D arrays using malloc? ...

Getting EXC_BAD_ACCESS without any helpful msg from NSZombieEnabled

Hello guys. I'm a newbie to iphone development and I've been struggling with an EXC_BAD_ACCESS error I got a couple of days ago. I'm basically taking the Stanford iphone class independently and I am trying to pass an array of NSManagedObjects to a TableViewController that's supposed to display them. The application launches in the si...

how to find out the page size of a processor using C program and malloc?

is it possible to find out the page size of a processor using C program and malloc? and not by using sysconf() call? ...

C Programming: malloc and free within a loop

Hi all, I just started out with C and have very little knowledge about performance issues with malloc() and free(). My question is this: if I were to call malloc() followed by free() inside a while loop that loops for, say, 20 iterations, would it run slower compared to calling free() outside the loop? I am actually using the first met...

Cannot understand how new or malloc does work with BYTE*

I trying to allocate memory for 10 bytes BYTE* tmp; tmp = new BYTE[10]; //or tmp = (BYTE*)malloc(10*sizeof(BYTE)); But after new or malloc operation length of *tmp more than 10 (i.e. '\0' char not in 10 position in tmp array) Why? ...

How to dynamically expand a string in C

Hi - I have a function that recursively makes some calculations on a set of numbers. I want to also pretty-print the calculation in each recursion call by passing the string from the previous calculation and concatenating it with the current operation. A sample output might look like this: 3 (3) + 2 ((3) + 2) / 4 (((3) + 2) / 4) x 5 (((...

Unusual heap size limitations in VS2003 C++

I have a C++ app that uses large arrays of data, and have noticed while testing that it is running out of memory, while there is still plenty of memory available. I have reduced the code to a sample test case as follows; void MemTest() { size_t Size = 500*1024*1024; // 512mb if (Size > _HEAP_MAXREQ) TRACE("Invalid S...

Resources for memory management in embedded application

How should I manage memory in my mission critical embedded application? I found some articles with google, but couldn't pinpoint a really useful practical guide. The DO-178b forbids dynamic memory allocations, but how will you manage the memory then? Preallocate everything in advance and send a pointer to each function that needs alloc...

How I return the size of the pointer that I have allocate with malloc?

See this example! int main( int argc, char ** argv ) { int *ptr = malloc(100 * sizeof (int)); printf("sizeof(array) is %d bytes\n", sizeof(ptr)); } The printf function return only 4 bytes! What is wrong? Thanks so much!!! ...

Wrapping malloc - C

I am a beginner in C. While reading git's source code, I found this wrapper function around malloc. void *xmalloc(size_t size) { void *ret = malloc(size); if (!ret && !size) ret = malloc(1); if (!ret) { release_pack_memory(size, -1); ret = malloc(size); if (!ret && !size) ret = mal...

C Programming: calling free() on error?

Hi all, This a follow up on my previous question. link here. My question is: Let's say I have the following code.. char* buf = (char*) malloc(1024); ... for(; i<20; i++) { if(read(fd, buf, 1024) == -1) { // read off a file and store in buffer perror("read failed"); return 1; } ... } free(buf); what...

how to properly free a char **table in C

Hello, I need your advice on this piece of code: the table fields options[0], options[1] etc... doesn't seem to be freed correctly. Thanks for your answers int main() { .... char **options; options = generate_fields(user_input); for(i = 0; i < sizeof(options) / sizeof(options[0]); i++) { free(options[i]); options[i] = N...

Dynamic memory inside a struct

Hello, I'm editing a piece of code, that is part of a big project, that uses "const's" to initialize a bunch of arrays. Because I want to parametrize these const's I have to adapt the code to use "malloc" in order to allocate the memory. Unfortunately there is a problem with structs: I'm not able to allocate dynamic memory in the struct...

malloc()/free() behavior differs between Debian and Redhat

I have a Linux app (written in C) that allocates large amount of memory (~60M) in small chunks through malloc() and then frees it (the app continues to run then). This memory is not returned to the OS but stays allocated to the process. Now, the interesting thing here is that this behavior happens only on RedHat Linux and clones (Fedor...

c struct pointer issues

I'm trying to cast a struct into another struct but I'm having incompatible pointer issues within the cast and the malloc under some_func (structs layout are the same) struct stu1 **some_func(struct stu1 *my_struct) { my_struct = (struct stu1 **)malloc(sizeof(struct stu1 *)*total_size); for(i=0;i<20;i++){ my_struct[i] = (st...