Questions tagged [dynamic-allocation]

Use for questions related to *dynamic allocation of memory*, such as `malloc()` in C, `new` in C++, etc. . Please notice that the tag is not language specific.

From Wikipedia: "C dynamic memory allocation refers to performing manual memory management for dynamic memory allocation in the C programming language via a group of functions in the C standard library, namely malloc, realloc, calloc and free."

Please notice that the tag is not language specific.

506 questions
-2
votes
2 answers

C: read an unknown number of lines of unknown length from a file

I am trying to write a program that reads an unknown number of lines of an unknown length (using malloc) and saves them into a structure. When I try to print what I've saved in the structure, I get a Segmentation fault. Code: #include…
Allemis
  • 47
  • 1
  • 10
-2
votes
4 answers

Dynamically-allocated two-dimensional array in C

I've got stuck on compiling two-dimensional array in C using malloc(); Trying to compile my code in Microsoft Visual Studio Express 2013 I keep getting error 'a value of type "int **" cannot be assigned to an entity of type "int *"' Here is my…
happyN
  • 3
  • 1
-2
votes
1 answer

Getting segmentation fault:11 in C. Why?

The following code is a solution to a problem of finding amicable pairs between input numbers. I don't yet know if the algorithm is the best it can be, but what my problem is exactly is that the code below keeps on returning segmentation fault:11. I…
Kinga
  • 1
-2
votes
1 answer

Segmentation fault when dynamically allocating array

I am writing a function to dynamically allocate a string array from my dictionary file that will be accessed through command line arguments. I keep getting segmentation fault 11 and cannot figure out why int allocateArray(int count) { int…
-2
votes
1 answer

understanding the base-to-derive conversion

Hello I am new to C++ and learning the conversion from a base class pointer to a derived class pointer. class Base{ public: virtual void method(){ std::cout << "this is a base class" << std::endl; } }; class Derived:public…
cgao
  • 155
  • 4
  • 15
-2
votes
1 answer

Deleting an array works on CodeBlocks but not on Visual

I'm building a class and at some point I call a delete. In codeblocks it works and in Visual Studio 2013 it doesn't. In my class I have: private: bool sign; // 0 if positive, 1 if negative int NumberSize; int VectorSize; …
-2
votes
3 answers

why we use sizeof using malloc?

What is purpose of using sizeof using malloc in C? Why its necessary? I mean we can use sizeof but we already know the size(=1000 bytes) what we are allocating in memory? I am beginner in C so. This might be a very obvious question.
Deepak
  • 1,503
  • 3
  • 22
  • 45
-2
votes
1 answer

Search dynamically allocated vector for specific data? C++

I have to create a dynamically allocated vector of objects from the Resistor class, which only has two private variables: resistance and tolerance. After that, a function must be created in order to search the vector for those objects that have…
Flo
  • 69
  • 2
  • 6
-3
votes
2 answers

Why does my free() function does not working(Code was given below)

Please go to line 29 and read the comment. #include #include #include int main() { char* messenges[5]; char* allstrings; register int i; int total = 0; for (i = 0; i < 5; i++) { …
-3
votes
3 answers

How can I ensure that I am not missing something in malloc and free?

char **test() { char **res = (char **)malloc(sizeof(char *) * 5); for (int cur = 0; cur < 3; cur++) { char *str = (char *)malloc(10); strcpy(str, "Maneger"); res[cur] = (char *)malloc(strlen(str)); …
-3
votes
1 answer

OpenCV.3.0 error : 0xC0000005: Access violation reading location 0x00000000

My code is working just fine in debug mode and here is the Output. Once I tried it in release mode I got this error : Unhandled exception at 0x5E3ADF2C (msvcp120d.dll) in Project4.exe: 0xC0000005: Access violation reading location…
-3
votes
1 answer

Converting Integer to dynamically allocated Char array, digit by digit using pointers

I need to convert Integer to Char, I can use only pointers without array indexes. Char array must be dynamically allocated. Can anybody review my code and tell my what am I doing wrong? #include #include int main(){ int…
Paweł Dymowski
  • 840
  • 7
  • 14
-3
votes
2 answers

Why does scanf not working with regex and dynamic allocation

I'm trying to use scanf with a char pointer to store a string. In case of ENTER key stroke, I want it to be catched. I know I could use fgets but I'm curious to know if it's possible so I managed to write the following code: #include…
Amin NAIRI
  • 2,292
  • 21
  • 20
-3
votes
2 answers

calloc and non contiguous memory blocks and void pointer

calloc function used to reserve memory and gives starting address of memory block but it is said that it may not allocate in contiguous address space and rather it my allocate different different non contiguous blocks but starting address we get as…
-3
votes
4 answers

Swapping names(malloced ones) using non malloced pointer variable

I am getting confused with pointers. Here is a code to swap two names. Please see the code. Consider Input : hellohai(for d) and asd(for e). Output i am getting : asd 1ellohai 1ellohai #include #include int main() { …
Shikhar Deep
  • 253
  • 2
  • 8
  • 19
1 2 3
33
34