Questions tagged [malloc]

The malloc function performs dynamic memory allocation in C and is part of the standard library. Use this tag for questions about usage, behavior and implementations of malloc.

The tag is used for questions related to the use of the malloc() function, part of the C standard library.

malloc(), along with free(), calloc(), and realloc(), is used to explicitly manage memory in C.

malloc() returns the address of a newly allocated block of memory, or NULL if memory allocation fails. The memory returned by malloc is uninitialized, and should be assumed to be filled with random data.

Wikipedia

References

Related Tags

9145 questions
2
votes
3 answers

populating dynamically allocated array of strings?

I have the following code to populate an array of strings, but every time i change the value, the entire array changes (instead of a single string the array)
Bonk
  • 1,859
  • 9
  • 28
  • 46
2
votes
5 answers

Why does the debugger show only one element from my array pointer?

First off: I know that new is the C++ way of doing this. I am simply showing that there is more than one way to reproduce this error, and both are incredibly frustrating. I have two forms of this source file. I'm trying to debug yet another…
Tasuret
  • 155
  • 1
  • 5
2
votes
3 answers

Why is the argument to malloc i+1 in this C code?

Usually a malloc contains sizeof , but this one doesn't and has i+1 instead: int main () { int i,n; char * buffer; printf ("How long do you want the string? "); scanf ("%d", &i); buffer = (char*) malloc (i+1); if (buffer==NULL) exit…
Caffeinated
  • 11,982
  • 40
  • 122
  • 216
2
votes
3 answers

Instantiating a class, using a pointer allocated by malloc()

Possible Duplicate: C++'s “placement new” Help with a c++ statement I work in a product, where most of the modules have been written in C. Among them, one or two modules are written in C++. I find the below code in a C++ module, which I could not…
Alexander
  • 450
  • 5
  • 16
2
votes
4 answers

new and malloc allocates extra 16 bytes

I'm writing on c++ in VS2010 Windows 7. I try to read file of size 64 bytes. Here's the code: BYTE* MyReadFile(FILE *f) { size_t result; BYTE *buffer; long lSize; if (f == NULL) { fputs ("File error", stderr); …
forik
  • 149
  • 4
  • 15
2
votes
1 answer

using linux user space memory for caching

I would like to implement an in memory cache (of some type) in my user space linux application. what i'm after is essentially the same behavior as linux's filesystem buffer cache : whatever memory isn't used by anything else, is used by my processes…
Michael Xu
  • 557
  • 1
  • 5
  • 14
2
votes
2 answers

allocating memory per thread in a parallel_for loop

I originally have a single-threaded loop which iterates over all pixels of an image and may do various operation with the data. The library I am using dictates that retrieving pixels from an image must be done one line at a time. To this end I…
Rotem
  • 21,452
  • 6
  • 62
  • 109
2
votes
1 answer

Is there a work around for global variable size limit in visual c?

How can I work around the Visual Studio Visual C #C2148 error? Here is the code that produces the error: #define ACOUNT 2000 #define BCOUNT 9000 #define CCOUNT 195 struct s_ptx { int pvCount[ACOUNT][BCOUNT][CCOUNT]; } ; This produces a…
PaeneInsula
  • 2,010
  • 3
  • 31
  • 57
2
votes
7 answers

malloc and calloc

I know this question may be marked as a duplicate of difference between malloc and calloc but still i would like to ask it. i know calloc initilizes the memory block,here my question is not focusussing on that part. my question is the definition of…
haris
  • 2,003
  • 4
  • 25
  • 24
2
votes
2 answers

Totally random malloc errors in Qt App on Mac OSX

#include int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); a.setApplicationName("xxx"); char bb[25] = {10, 1, 64, 18, 20, 116, 97, 114, 97, 110, 103, 105, 108, 108, 51, 64, 103, 109, 97, 105,…
Tarandeep Gill
  • 1,506
  • 18
  • 34
2
votes
5 answers

C - Freeing 'inherited' structure

In the following code, will freeing Dairy free Yogurt as well? Both point to the same address as far as I know. Also, is this style of coding a bad practice? Say if I only kept pointers to the Dairy and indirectly freed the Yogurt and Cheese as…
G M
  • 67
  • 3
2
votes
7 answers

How to write a simple malloc function in c

As an assignment in operating systems we have to write our own code for malloc and free in C programming language, I know if i asked the code for it there is no point of me to study. i'm facing the problem of not knowing where to include…
Zeemaan
  • 333
  • 3
  • 7
  • 17
2
votes
5 answers

Improper freeing of the heap aborts C program

When trying to free either the same malloced heap twice or the unallocated heap, you get the fatal errors "double free or corruption(fasttop)" and "invalid pointer" respectively, hence the running program aborts immediately. Although doing so is…
sof
  • 9,113
  • 16
  • 57
  • 83
2
votes
3 answers

Malloc initializing a null pointer

Hi I ran in to this situation. I am using malloc to give me an array of 10 pointers. When I see the test pointers in gdb, one of them(the third )points to 0x0. Sometimes the code segfaults when using apple[2]->string = "hello". Why does malloc do…
kevin
  • 173
  • 3
  • 10
2
votes
3 answers

Trying to return a string from a queue in C/free problems

I've been working on a lab for a CSC class for a while, and unfortunately I'm a bit rusty with C (as you'll probably notice from the code). I'm encountering two particular problems, both related to memory management. 1) In the dequeue operation,…
user1159398
1 2 3
99
100