malloc

Problem in code with File Descriptors. C (Linux)

I've written code that should ideally take in data from one document, encrypt it and save it in another document. But when I try executing the code it does not put the encrypted data in the new file. It just leaves it blank. Someone please spot what's missing in the code. I tried but I couldn't figure it out. I think there is something...

How to use asprintf with the Boehm GC?

As far as I can tell, asprintf calls malloc. If I replace malloc with the Boehm GC, a call to asprintf still calls the traditional malloc - at least that's what valgrind is telling me: Here's the malloc macro: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdbool.h> #include <gc.h> #define malloc(n) GC_MALLO...

C Program: square brackets when printing 2D character array

Hello, I am having a confusing issue -- for me at least, this may be very simple and I am missing something. I am trying to initialize a 2D array of variable size, The array is a part of a struct. I am having no issue allocating memory for the array but when I try to assign characters to the array, or print them I receive unexpected resu...

Can you mix free and constructor in C++?

Possible Duplicate: Is there any danger in calling free() or delete instead of delete[]? I was reading this question: http://stackoverflow.com/questions/184537/in-what-cases-do-i-use-malloc-vs-new/ Someone raised that one reason to use malloc was if you were going to use free. I was wondering: Is it valid to mix a free call ...

Is there any way to improve this function which replaces occurrences of substrings with another string in an malloc allocated string?

Hello. I'm very new to C and I decided to make a function called str_replace which replaces strings inside strings which have been made using malloc. It appears to work but can anyone find any room for improvements. Any advice will be appreciated. I'd like to know if people think finding the number of occurrences to calculate the new s...

allocating memory for a structure

Hello, gcc 4.4.4 c89 I am keep getting a "Cannot dereference to incomplete type". However, I am sure I have my structure type complete. I return the Network_t instance that is the pointer to the allocated memory. I should be able to dereference that memory. Many thanks for any advice, I have this in my header file: driver.h typedef...

"pointer being freed was not allocated" Memory block will not deallocate for some reason.

Hello. I'm struggling to find why I can't free a memory block. Something must be wrong with the pointer. A memory block for a structure is made in a function and the pointer used is stored in an array. Later the pointer is taken from the array to be used to free the memory. I've figured out which free it is. I've placed "//This one" nex...

Released object crashes App -- problem with alloc/init memory management

I have a flipView class that I allocate and initiate. But, when I release it the app crashes. If I don't release it, the app works fine, so it seems. The error message I receive when I release it is: Malloc - error for object: object pointer being freed was not allocated. If you could please assist me, I would be grateful. - (IBA...

Flexible array of pointers to char in a struct - Please help!!!

Hi all, I'm trying to implement a ring buffer with the following struct /*head, tail are indexes of the head and tail of ring buffer *count is the number of elements; size is the max size of buffer *rbArray is an array to pointer of char used to store strings */ struct rb{ int head; int tail; int count; int size; char...

using malloc for the life of the program

Hello, gcc 4.4.4 c89 I have always thought of using malloc for the life of the project for being the scope. But I am just wondering if my idea is the best practice. For example, I initalize an instance of the struct in main. And create 2 functions for creating and destroying. I am just wondering if this is the right thing to do. I h...

cannot free memory

Hello, gcc 4.4.4 c89 I have the following function but I cannot free the memory. The message I get in Valgrind is suspecting the getline function. However, I am free the file pointer at the end of the function. So it cannot be that. I have a global array of pointers to char 'candidate_names'. However, I haven't allocated any memory f...

C - How can I save structs to a malloc'd section of memory?

My question's pretty basic, but it's been a while. I'm reading in a text file and saving numbers in the text to a struct 'Record'. After I read text to my Record buffer, I want to place it in an area of memory. typedef struct { int line_status[64]; float line_data[64], relativetime; unsigned long blkhdr_ticks; } Record; Record *sto...

C - accessing a struct from dynamic memory

I'm writing a program with struct Record. As I read in records from text in a loop, I assign them to buffer before saving buffer into the array. nRange is just the total number of records being read. Record *storage; storage = (Record*)malloc(nRange*sizeof(Record)); Record buffer; storage[i] = buffer; I want to access storage[i] in...

How best to store VERY large 2D list of floats in c++? Error-handling?

I'm migrating some code from c to c++. I've changed some malloc calls for structure memory allocation to new calls. Basically before the code I'm porting was malloc'ing arrays that contain multiple sets of frame coords, each a couple hundred thousand floats in length -- so the total array length could be in the tens of millions of co...

Behaviour after using malloc in C++

Does the following code invoke undefined behaviour? As far as I know we should always use new to create user defined objects dynamically because new in addition to malloc calls the constructor too. #include <cstdio> #include <cstdlib> struct om { int a; void fun() { a=10;b=10; } private : int b; } s1; // st...

C Programming - Malloc Structs and undefined arrays

I am writing a program in C and I am trying to create these structs. I have three of them: the first consisting of 2 ints and 2 chars, the second consisting of an int and an undefined array of pointers to the first one, and a third which contains 4 longs and 2 ints along with an undefined array of pointers to the second. I am having ...

simple c malloc

While there are lots of different sophisticated implementations of malloc / free for C/C++, I'm looking for a really simple and (especially) small one that works on a fixed-size buffer and supports realloc. Thread-safety etc. are not needed and my objects are small and do not vary much in size. Is there any implementation that you could ...

about malloc() and free() in C

I have the following C-code: #include<stdio.h> #include<stdlib.h> typedef struct node { int a; }node; int main() { node * n; printf("\n%d\n",n->a); n = (node *) malloc ( sizeof( node )); printf("\n%d\n",n->a); n->a = 6; printf("\n%d\n",n->a); free(n); printf("\n%d\n",n->a); n->a = 4; pri...

Would this be considered a memory leak?

Consider this pointless program: /* main.c */ #include <stdlib.h> #include <unistd.h> int main(int argc, char **argv) { int i; for (i = 0; i < 1024; i++) { int pid = fork(); int status; if (pid) { wait(&status); } else { char *ptr = (char *)malloc(1024*sizeof(char...

malloc fails catastrophically

I am trying to implement a Queue in C. Coming from Java and other managed languages, I am really struggling with memory management. Here is the enqueue() function: int enqueue(Queue q, int value) { Node newNode = malloc(sizeof(Node)); /*newNode->value = value; if (q->size == 0) q->head = newNode; else q...