malloc

Can some tell me why I am seg faulting in this simple C program?

I keep on getting seg faulted after I end my first for loop, and for the life of me I don't why. The file I'm scanning is just 18 strings in 18 lines. I thinks the problem is the way I'm mallocing the double pointer called picks, but I don't know exactly why. I'm am only trying to scanf strings that are less than 15 chars long, so I do...

enabling guard malloc for Iphone device or for store kit api?

I am using store kit api for an application on iphone. I would like to know if it is possible to enable Malloc Guard while running on the device and see if there are any memory leaks. Could someone let me know if it is possible? ...

Memory allocated with malloc does not persist outside function scope?

Hi, I'm a bit new to C's malloc function, but from what I know it should store the value in the heap, so you can reference it with a pointer from outside the original scope. I created a test program that is supposed to do this but I keep getting the value 0, after running the program. What am I doing wrong? int f1(int * b) { b = mal...

Using fscanf with dynamically allocated buffer.

Hi, I got the following code: char buffer[2047]; int charsRead; do { if(fscanf(file, "%2047[^\n]%n%*c", buffer, &charsRead) == 1) { // Do something } } while (charsRead == 2047); I wanted to convert this code to use dynamically allocated variables so that when calling this code often I won't get heavy memory leakage....

What's the outcome if I use free with new or delete with malloc?

It is a compiler error or runtime error? The code below can be compiled! class Base{ void g(); void h(); }; int main() { Base* p = new Base(); free(p); return 0; } However it can't be compiled with a virtual function if I declare the class Base like this class Base{ virtual void g(); void h(); }; The code below can be co...

linux new/delete, malloc/free large memory blocks

Hi folks, We have a linux system (kubuntu 7.10) that runs a number of CORBA Server processes. The server software uses glibc libraries for memory allocation. The linux PC has 4G physical memory. Swap is disabled for speed reasons. Upon receiving a request to process data, one of the server processes allocates a large data buffer (usin...

C++ Memory Allocation & Linked List Implementation

I'm writing software to simulate the "first-fit" memory allocation schema. Basically, I allocate a large X megabyte chunk of memory and subdivide it into blocks when chunks are requested according to the schema. I'm using a linked list called "node" as a header for each block of memory (so that we can find the next block without tediou...

Why malloc memory in a function and free it outside is a bad idea?

if this is a bad idea, how to allocate memory in the function? ...

Examining C/C++ Heap memory statistics in gdb

I'm trying to investigate the state of the C/C++ heap from within gdb on Linux amd64, is there a nice way to do this? One approach I've tried is to "call mallinfo()" but unfortunately I can't then extract the values I want since gdb doesn't deal with the return value properly. I'm not easily able to write a function to be compiled into...

malloc and delete in C++, opinions

In C++ using delete to free memory obtained with malloc() doesn't necessarily cause a program to blow up. Do you guys think a warning or perhaps even an assertion failure should be produced if delete is used to free memory obtained using malloc()?? Why do you think that Stroustrup did not had this feature on C++? ...

Why new()/delete() is slower than malloc()/free()?

Why new()/delete() is slower than malloc()/free()? EDIT: Thanks for the answers so far. Please kindly point out specifications of standard C++ implementation of new() and delete() if you have them thanks! ...

malloc: error checking and freeing memory

Hi, I'm using malloc to make an error check of whether memory can be allocated or not for the particular array z1. ARRAY_SIZE is a predefined with a numerical value. I use casting as I've read it's safe to do so. long double *z1 = (long double *)malloc(sizeof (long double) * ARRAY_SIZE); if(z1 == NULL){ printf("Out of memory\n...

Enable mtrace (MALLOC_TRACE) for binary program

Hello How can I enable mtrace() (and MALLOC_TRACE env) for a binary program without sources? Thanks ...

Adobe After Effects Plugin With Cocoa (Overriding malloc)

Messing about a bit, i have a working Adobe After Effects plugin with a bit of Obj-c / Cocoa in it (NSArray and custom objects - not ui stuff). The SDK guide states:- Always use After Effects memory allocation functions. In low-memory conditions (such as during RAM preview), it’s very important that plug-ins not compete with After Ef...

Malloc inside another function (ANSI C)

Hi I'll go straight to it. I'm working on an assignment, where I suddenly ran into trouble. I have to allocate a struct from within another function, obviously using pointers. I've been staring at this problem for hours and tried in a million different ways to solve it. This is some sample code (very simplified): ... some_struct s; pr...

calloc v/s malloc and time efficiency

I've read with interest the post C difference between malloc and calloc. I'm using malloc in my code and would like to know what difference I'll have using calloc instead. My present (pseudo)code with malloc: Scenario 1 int main() { allocate large arrays with malloc INITIALIZE ALL ARRAY ELEMENTS TO ZERO for loop //say 100...

dynamic memory allocation problem

I am working on a program that requires me to make use of 4 matrices sized [1000][1000]. I have created them using malloc(), but when I try running the program it just crashes and the memory usage shoots up to 2.5 GB. Please suggest any solution as soon as possible. I would be grateful.. ...

Switching from C++ (with a lot of STL use) to C for interpreter building

I'm switching from C++ to C because I'm rebuilding my toy interpreter. I was used to vectors for dynamic allocation of objects like tokens or instructions of my programs, stacks and mainly strings with all their aspects. Now, in C I'm not going to have all these anymore. I know that I will have to use a lot of memory management, too. ...

why pointer to pointer is needed to allocate memory in function

Hi I have a segmentation fault in the code below, but after I changed it to pointer to pointer, it is fine. Could anybody give me any reason? void memory(int * p, int size) { try { p = (int *) malloc(size*sizeof(int)); } catch( exception& e) { cout<<e.what()<<endl; } } it does not work in the main funct...

Iphone memory leak with malloc

Hello. I have memory leak, found by instruments and it is supposed to be in this line of code: indices = malloc( sizeof(indices[0]) * totalQuads * 6); This is actually a code snippet from a tutorial, something which i think is leak-free so to say. Now I reckon, the error is somewhere else, but I do not know, where. These are the la...