Free memory for a dynamic table array

Ok , so these are my structures: struct Student { int id; char* name; }; struct HashTable { int size; int noElements; Student** elements; }; And here I allocate memory for the dynamic array of arrays ht.elements = (Student**)malloc(size*sizeof(Studen

Is c ++ delete [] applicable to primitive types

In C++, if I have a dynamically allocated array of primitive type, is it necessary to use delete [] to prevent memory leakage? For example, char * x = new char[100]; delete x; // is it required to call delete [] x? struct A { ... }; A *p = new A[30];

allocate memory with malloc ()

In the program below I am trying to make the function insert() to allocate memory by calling malloc() to create a new struct (person)...but I get the following warning: assignment from incompatible pointer type [enabled by default]...How should I use

Dynamic Buffer Fills in C

I've been searching on how to allocate a dynamic buffer using fgets, but I can't seem to get it on this example. The file has two numbers of unknown length separated by a white-space. For every line it reads each character until ' ' and \n and prints

catch std :: allocation functions at compile time

I want to only allow use of std::function in my code base if it does not do any allocations. To this end I can write something like the function below and only use it to create my function instances: template< typename Functor> std::function<Func

Array of nested structures

I have huge arrays of nested structures which makes it impossible to allot that kind of space and forces me to use heap. But I am facing difficulties using malloc. The gist of the problem is below. struct year_of_joining { struct district { struct co

Char * Array Memory Leak

I am having issues de-allocating memory that I used in my char* array. In my code snippet below, I am creating a char* array named input that holds pointers to single words at a time followed by a pointerNULL at the end of the array. This is the only

How to fix this memory leak in my resize array method?

I've created a simple integer array class: #ifndef INTARRAY_H #define INTARRAY_H class IntArray { public: // Constructors / Destructor IntArray(); IntArray(int size); IntArray(const IntArray& rhs); ~IntArray(); // Methods int size(); void resize(int

How to allocate the character pointer for concatenation?

I wrote the function to tokenize the entire string and concatenate space and string length of each token . my functions is main function char *final_buff = NULL; data_token(databuf,&final_buff); after that I call free(final_buff); function: int data_

Linux really allocating memory it should not be in C ++ code

In Linux, the kernel doesn't allocate any physical memory pages until we actually using that memory, but I am having a hard time here trying to find why it does in fact allocate this memory: for(int t = 0; t < T; t++){ for(int b = 0; b < B; b++){ Ma

allocate random no. bytes in c

This question already has an answer here: 64 bit large mallocs 9 answers How can I dynamically allocate random no. of bytes in C? (say) I want to allocate 10 GB of memory, how can I do that and which function would be better to use here, as in malloc

Using the Assembly x86 call brk ()

i am trying to dynamically allocate memory into the heap and then assign values in those memory addresses. I understand how to allocate the memory but how would i assign for example the value in a register to that first dynamic memory address? This i

Dynamic memory allocation for input?

I am having a lot of trouble starting my project. Here are the directions: "Complete counts.c as follows: Read characters from standard input until EOF (the end-of-file mark) is read. Do not prompt the user to enter text - just read data as soon as t

Use strcpy () with dynamic memory

My code runs properly and has no memory leaks. However, I am getting valgrind errors: ==6304== 14 errors in context 4 of 4: ==6304== Invalid write of size 1 ==6304== at 0x4A0808F: __GI_strcpy (mc_replace_strmem.c:443) ==6304== by 0x401453: main (calc

How can I release a linked list in C?

In the C language, I have something like: typedef struct bucket { int value; struct bucket *next; } Bucket; typedef struct table { int size; Bucket **buckets; } Table; Now I do Table *t = malloc(sizeof(Table)); And t->buckets = calloc(10, sizeof(Buck

malloc to allocate memory to a list

I have a custom struct typedef struct node { struct node *next; int vertex; }node; typedef struct { int numberOfNodes; int *visited; int numberOfEdges ; node **ppLists; } adjacencyList; I am trying to allocate memory for a adjacency list with the fol

Realloc equivalent in C ++

Yes, another realloc vs. std::vector question. I know what you're going to say, and I agree, forget manual memory allocation, and just use a std::vector. Well unfortunately my professor has forbidden me to use anything from the STL for this assignmen

work with dynamically allocated memory (pointer)

I was playing around with pointers and dynamic memory as I'm trying to learn C++ and I keep getting this error when I compile. error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no accept

2D dynamic array allocation and C reference pass

Can someone wiser than I please explain to me why the following code segment faults? There is no problem allocating the memory by reference, but as soon as I try to assign anything or free by reference, segfault occurs. I'm sure I'm missing some fund

Store data of unknown size in C ++

I've been using PHP for about 4 years, however I've come across a problem that requires something with slightly (:P) better performance and so I've chosen C++. The program I'm writing is a Linux daemon that will scan a MySQL database for URLs to load