alloc(3) Library Functions Manual alloc(3)NAME
alloc - allocate memory
SYNTAX
#include <alloc.h>
char *alloc(new);
void alloc_free(x);
void alloc_re(&x,old,new);
char *x;
unsigned int old;
unsigned int new;
DESCRIPTION
alloc allocates enough space from the heap for new bytes of data, adequately aligned for any data type. new may be 0. alloc returns a
pointer to the space. If space is not available, alloc returns 0, setting errno appropriately.
alloc_free returns space to the heap.
alloc_re expands the space allocated to x from old bytes to new bytes. It allocates new space, copies old bytes from the old space to the
new space, returns the old space to the heap, and changes x to point to the new space. It then returns 1. If space is not available,
alloc_re returns 0, leaving the old space alone.
SEE ALSO sbrk(2), malloc(3), error(3)alloc(3)
Check Out this Related Man Page
DYNARR(3pub) C Programmer's Manual DYNARR(3pub)NAME
dynarr, dynarr_init, dynarr_resize, dynarr_free - simple dynamic arrays
SYNOPSIS
#include <publib.h>
void dynarr_init(struct dynarr *da, size_t elsize);
int dynarr_resize(struct dynarr *da, size_t newsize);
void dynarr_free(struct dynarr *da);
DESCRIPTION
These functions make it easier to use dynamic arrays, i.e., arrays that are allocated with malloc(3) and resized with realloc(3). Below is
a typical code fragment for implementing a dynamic array that is resized as more input is read.
char *p, *line;
size_t alloc, len;
len = 0;
alloc = 1024;
if ((line = malloc(alloc)) == NULL) abort();
while (fgets(line + len, alloc-len, stdin) != NULL) {
len = strlen(line);
alloc += 1024;
if ((p = realloc(alloc)) == NULL) abort();
alloc = p;
}
(The error handling is intentionally simplified.) Below is the above fragment with the dynarr(3).
struct dynarr da;
dynarr_init(&da);
while (fgets((char *)da.data + da.used, da.alloc-da.len, stdin) != NULL) {
da.used = strlen(da.data);
if (dynarr_resize(&da, da.alloc + 1024) == -1) abort();
}
The code is a bit simpler, and all the memory allocation details and most of the error checking code is hidden away.
The dynamic array is represented by a struct dynarr:
struct dynarr {
void *data;
size_t alloc, used;
};
The interface to the dynamic allocation has intentionally been made unopaque.
dynarr_init initializes a struct dynarr to be an empty array, dynarr_resize sets its size to be newsize, and dynarr_free frees the array
(it will become an empty array again).
RETURNS
dynarr_resize returns -1 if it failed, 0 if it succeeded. It does not change the array in any way if it failed.
SEE ALSO publib(3), malloc(3), realloc(3), strdup(3)AUTHOR
Lars Wirzenius (lars.wirzenius@helsinki.fi)
Publib C Programmer's Manual DYNARR(3pub)
Is it generally not a good idea in a multithreaded program to make lots of malloc calls (dynamic memory requests) because of the limited nature of the memory heap ?
Out of curiosity (warning it may sound silly), but would using mutexs (mutual exclusion) help to minimise chances of runtime errors... (12 Replies)
On SunOS 5.8 I get an error when starting a large Java process with over 2Gb memory.
Error occurred during initialization of VM
Could not reserve enough space for object heap
When stopping several other Java processes we can start this process. This seems to indicate that we don't have... (11 Replies)
Hi,
I am trying to process line by line of a file. But I should not be allocating static allocation for reading the contents of the file. The memory should be dynamically allocated. The confusion here is how do I determine the size of each line, put it into a buffer with the memory allocated... (11 Replies)
It is customary to use structs in c code. but it is suprising to obtain different results. Sometimes a pointer is returned. But a failure would appear at other attempts.
e.g.
struct _site {
int type;
char A;
char B;
.
.
... (12 Replies)
Hi,
I have the following problem: i must allocate a dynamic array from a subroutine which should return such array to main function. The subroutine has already a return parameter so i thought of pass the array as I/O parameter. I tried the following program but it doesn't work (segmentation... (11 Replies)
Hey guys,
I am somewhat new to Solaris - and very new when it comes to mounts.
My problem is that when I installed Solaris, I allocated way too little diskspace to my / mount (it first became obvious now, however, because of new needs).
bash-3.00# df -h
Filesystem size ... (25 Replies)
Actually for a process to run it needs text, stack , heap and data segments. All these find a place in the physical memory.
Out of these 4 only heap does exist after the termination of the process that created it. I want to know the exact reason why this happens. Also why the other process need to... (20 Replies)
Hello ,
i have read the manual page of alloca ,
it is documented there that "alloca works similar to the malloc but it allocates memory from stack and frees automatically".
i can't get how can i use that in a program .
if i'm not wrong can i use that like :
#include<stdio.h>... (24 Replies)
there seems not to be error in this segment. In some computers, it can work well. But in others, it will give a failure.
why it ocurrs and how to deal with it?
in a function:
if( *ver == NULL ) {
*ver = (vertex *) malloc(sizeof(vertex)); //this line
... (17 Replies)
Hi all
I'm trying to use someone else's software, which has a realloc that fails in it. This is probably due to memory limitations, as it only happens when I use this software on huge datasets.
First question : how to diagnose if it's a soft or hard limitation? I mean, if it's due to my... (10 Replies)
Firstly, I can't stress enough how much of a newb I am to AIX or Linux in general so please be very patient. I am strictly a MS man, however I now need to administer an IBM power series blade server which sits on top of an AIX VIOS. The VIOS version I am running is 2.1.2.0 and it is mirrored on two... (18 Replies)
I want to find the size of the total memory allocated on the heap for the following statement:
int* a = new int;How can I use the sizeof operator for this?
I used:
printf("\t===> %d\n",sizeof(*a));
Is this statement correct?
I have asked the question because when I checked the memory of... (13 Replies)
Not sure in which forum to post this. I'm trying here, in Programming.
I'm working on a PC with Intel Duo processor & 2GB of ram. OS is Ubuntu 10.04.
I'm having problems with a C++ program that makes extensive use of realloc(). It happens that as soon as the overall memory allocated(OS +... (14 Replies)
We are receiving the below error message when trying to encrypt or decrypt a file on AIX server :
gpg: out of memory while allocating 8192 bytes
gpg process was working for years on the server until the day we started to see this.
This same gpg encryption is working on an other AIX server... (17 Replies)
I am facing a problem of memory corruption. The loop runs for the first time but does not go through the second time. What could be the problem?
for(int z=0;z<2;z++)
{
fp=fopen("poly.dat","r");
/*do something which reads this file into a 2D array*/
fclose(fp);
... (10 Replies)