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
Let's say I have the following structure: typedef struct { int length ; int * data ; } Array ; And in my main function I do the following: int length = 10; char * filename = " data "; // create an object Array * arrptr1 = NULL ; printf (" s
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];
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
This question already has an answer here: Dynamic memory access only works inside function 1 answer I wanted to write a function that changes the size of dynamic array and allows user to fill it at once. I know that I should do it with using of "real
I'm new/noob programmer of C++, and I've this problem. I want to pass a pointer of double to a function (which will process some data on it) and read (after the process) a fixed value of that "array". I've do this: void ReadDoubles(double* sampl
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
I want max to have the content of tmp_max. max is dynamically allocated. tmp_max size is known. Copying the values is working correctly when I hardcode it but it doesn't work when I create a function to copy the values. Why is that? //This code works
So, I was giving some examples of how to use dynamically-allocated 2d arrays and was about to send code that was essentially the following: int size = 5; int* arr = new int[ size ]; for( int i = 0; i < size; i++ ) arr[ i ] = i; delete[] arr; size = 1
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
First off, when returning an unnamed object such as: Object* func() { return new Object(); } is that an anonymous variable, temporary variable, neither, or both? Does having it unnamed cause it to be temporary- as in it goes out of scope once func re
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
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
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
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_
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
For example, I have to create a struct in a function with the parameters: a function pointer with the format: void (*func)(void *) and an int id. The struct to create is the following: typedef struct example { int id; void (*func)(void *); } Example;
In short, I was assigned the task of creating a class that dynamically allocates memory to form a matrix of int values. Part of the class are member functions that perform the basic matrix calculations -- addition, subtraction, and multiplication. Ev
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
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
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
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
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
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
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
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
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
This is an attempt to rewrite some old homework using STL algorithms instead of hand-written loops and whatnot. I have a class called Database which holds a Vector<Media *>, where Media * can be (among other things) a CD, or a Book. Database is the
Suppose I have a C function: void myFunction(..., int nObs){ int myVec[nObs] ; ... } Is myVec being dynamically allocated? nObs is not constant whenever myFunction is called. I ask because I am currently programming with this habit, and a friend was
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