malloc

How does sbrk() work in C++?

Where can I read about sbrk() in some detail? How does it exactly work? In what situations would I want to use sbrk() instead of the cumbersome malloc() and new()? btw, what is the expansion for sbrk()? ...

Minimizing the amount of malloc() calls improves performance?

Consider two applications: one (num. 1) that invokes malloc() many times, and the other (num. 2) that invokes malloc() few times. Both applications allocate the same amount of memory (assume 100MB). For which application the next malloc() call will be faster, #1 or #2? In other words: Does malloc() have an index of allocated locations in...

Very strange malloc error.

Ok. So I have this function, init(): void init() { fstream file; int index = 0; char temp_list[60000][15]; listlen = 0; current_index = 0; file.open("en_US.dic"); while(!file.eof()) { file >> temp_list[index]; index++; } listlen = index; file.close(); file.open("en_US.dic"); word_list = new char*[listlen]; int count = 0...

Segmantation fault when adding elements to a structure

Why do I get segmentation fault in this function: #include <stdio.h> #include <stdlib.h> #include "math.h" vec_t mtrx_multiple (sparse_mat_t a, vec_t c) { vec_t result; int i; result.n = a.n; printf("result.n: %d\n", result.n); result.vec = malloc(a.n * sizeof *result.vec); for(i=0; i<a.n; i++) resu...

MallocDebug "Unable to read malloc information from (null)"

I have a certain .app built in xcode (version 3.2.1) as a Release build. I open MallocDebug and browse to the build directory, selecting the Release build and clicking the .app file. I then hit "Launch". MallocDebug then seems to freeze, giving me the spinning beachball for a minute or so, followed by the error message: Read Data "Un...

Array Elements Overwritten with Last in C

I'm trying to create a program which takes in a set number of strings (the user is asked to put in the number of strings they will enter), once it has these strings, they are placed in an array, using dynamic memory. The ouput would be something like this: # of Strings: 3 Cat Dog Elephant Cat Dog Elephant Heres a snippet of my code...

allocate matrix in C

hey, i want to allocate a matrix. is this the only option: int** mat = (int**)malloc(rows * sizeof(int*)) for (int index=0;index<row;++index) { mat[index] = (int*)malloc(col * sizeof(int)); } thanks ...

memory allocation in C

I have a question regarding memory allocation order. In the following code I allocate in a loop 4 strings. But when I print the addresses they don't seem to be allocated one after the other... Am I doing something wrong or is it some sort of defense mechanism implemented by the OS to prevent possible buffer overflows? (I use Windows Vist...

Can I designate a Java-like 'constructor' in c?

I want to 'construct' (read: malloc and memset) my hashtable in c. To do this, I created a function as follows: int maketable(struct hash_entry **table, int size){ table = (struct hash_entry **)malloc(size*sizeof(struct hash_entry *)); int i = 0; for (; i<size; i++) { memset(table[i], '\0', sizeof(struct hash_entry...

Is it safe to allocate memory for buffers on external dll and use it on main application?

As in topic.. I've found something like this in one application. In main C application we have declaration: void* buff = NULL; and later there is a call: ReadData(&buff); SaveToFile(buff); SaveToFile() is a C function from main function. ReadData(void* * ) is a C++ function from external dll. In this function memory for buffer is...

Uninitialised value was created by a heap allocation

I have been chasing this bug around, and I just don't get it. Have I forgotten some basic C or something? ==28357== Conditional jump or move depends on uninitialised value(s) ==28357== at 0x4C261E8: strlen (mc_replace_strmem.c:275) ==28357== by 0x4E9280A: puts (ioputs.c:36) ==28357== by 0x400C21: handlePath (myshell.c:105) ==2...

C stdlib realloc issue

The realloc reference says: The function may move the memory block to a new location, in which case the new location is returned. Does it mean that if I do this: void foo() { void* ptr = malloc( 1024 ); unsigned char* cptr = ( unsigned char* )ptr+256; ptr = realloc( ptr, 4096 ); } then cptr may bec...

Freeing a character pointer

I have a function which is called multiple times during the program's execution. In said function, I have a dynamic character pointer which I resize numerous times. My question is: do I need to free this pointer before the end of the function? void functionName() { char *variable = (char *) malloc(0); //variable is resized with re...

Allocating memory for a Structure in C

I'm tasked to create a program which dynamically allocates memory for a structure. normally we would use x=malloc(sizeof(int)*y); However, what do I use for a structure variable? I don't think its possible to do struct st x = malloc(sizeof(struct)); Could someone help me out? Thanks! ...

Return Structure from Function (C)

I need to create a structure inside a function (dynamically with malloc) Then i need to be able to send it to my main, and use it there. I have no problems creating it, I simply need help sending it to my main, and I'm also unsure of how to access it once I get it there. struct retValue * fn() { struct retValue { int number; }; ...

Defining a Structure in C with Malloc

I asked a question earlier on defining a structure using malloc. This was the answer I was given by the majority: struct retValue* st = malloc(sizeof(*st)); I was showing a friend my code, and we came to a stumbling block. Could someone please explain why this code works? From my viewpoint, *st hasn't been defined when you malloc it,...

malloc behaves differently on different machines

Hi, I see totally different behavior when running a piece of program that tries to exceed RSS on different machines. The code is something like: ... char** s = (char**)malloc(10000*sizeof(char*)); for (i = 0; i < 10000; i++){ s[i] = (char*)malloc(1000*1000*sizeof(char)); if (s[i] == NULL) { printf("cannot allocate me...

How to free() a malloc()'d structured correctly?

Hi! i have a structure malloc()'d, and after using them, i want to free() it, but my program freezes out here. Can anyone tell me, what i am doing wrong? Here is my code: struct data { char *filename; char *size; }; //primarypcs is a long type variable struct data *primary = (struct data *)malloc( primarypcs * sizeof( stru...

C: Is there something better than a FIFO Queue implementation for this requirement?

In one of my programs, there are multiple clients and each client has its own buffer. In an infinite loop, I check if any of the client has any data to be written to disk. If it does, then I do the same and continue. Now, because the client writes data that is not really in my control (result of some computations), I need a dynamic buf...

Why can't I access this struct through its pointer?

#include <stdio.h> #include <stdlib.h> typedef int element; struct cell { element e; struct cell *p; }; typedef struct cell* CELL; int main() { CELL* p; p = (CELL*) malloc (sizeof(struct cell)); p->e = 8; /* This ain't working */ *p.e = 8; /* This doesn't help anything either */ return 0; } I...