Sponsored Content
Full Discussion: Dynamic memory allocation
Top Forums Programming Dynamic memory allocation Post 302230697 by matrixmadhan on Saturday 30th of August 2008 08:05:20 PM
Old 08-30-2008
this wont work for lines that are greater than size 1024

instead, parse through the line and then do a dynamic allocation and copy the contents, free that.

But throughput will suffer here as memory is allocated and deallocated each time for parsing a line. Instead of a big buffer size like 1KB or 2 KB ask Vino suggested can be safely used.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

memory allocation

I would like to know how I could allocate some more memory to a process. Please note that I am not the root user. (1 Reply)
Discussion started by: sagar
1 Replies

2. Programming

array dynamic allocation

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)
Discussion started by: littleboyblu
11 Replies

3. Programming

Memory allocation problem

I have a program that will fetch some particular lines and store it in a buffer for further operations.The code which is given below works but with some errors.I couldn't trace out the error.Can anybody help on this plz?? #include <stdio.h> #include <stdlib.h> #include<string.h> #define... (1 Reply)
Discussion started by: vigneshinbox
1 Replies

4. Programming

global variables and dynamic allocation

Hi, is it possible in C to allocate dynamically a global variable?? (3 Replies)
Discussion started by: littleboyblu
3 Replies

5. Programming

Is there a problem with the memory allocation???

I have a scenario like the client has to search for the active server.There will be many servers.But not all server are active.And at a time not more than one server will be active. The client will be in active state always i.e, it should always search for an active server until it gets one.I... (1 Reply)
Discussion started by: vigneshinbox
1 Replies

6. Programming

dynamic allocation vs static allocation in c

i wrote a tiny version of tail command using a large buffer statically allocated but, in a second time, i found another version in which i use a bidimensional array dynamically allocated. here is the first version /*my tiny tail, it prints the last 5 line of a file */ #include<stdio.h>... (4 Replies)
Discussion started by: lucasclaus
4 Replies

7. Programming

Memory allocation in C

Hi Experts I need some help in static memory allocation in C. I have a program in which I declared 2 variables, one char array and one integer. I was little surprised to see the addresses of the variables. First: int x; char a; printf("%u %u\n', &x, a); I got the addresses displayed... (2 Replies)
Discussion started by: unx_freak
2 Replies

8. Programming

Dynamic Memory Allocation

Hello Guys I have a small confusion in the dynamic memory allocation concept. If we declare a pointer say a char pointer, we need to allocate adequate memory space. char* str = (char*)malloc(20*sizeof(char)); str = "This is a string"; But this will also work. char* str = "This... (2 Replies)
Discussion started by: tene
2 Replies

9. Shell Programming and Scripting

memory allocation to a variable

hello all.. i'm a beginner in shell scripting. I need to know what is really happening when we are creating a variable in shell scripting? how memory is allocated for that variable? (3 Replies)
Discussion started by: aarathy
3 Replies

10. Programming

C++/ROOT Memory Allocation?

Hello, I am new to C++ programming, so I'm still getting a feel for things. I recently wrote a simple C++ program (to be used as a ROOT Macro) to conduct a statistical analysis of a varied version of the Monty Hall problem (code below). Basically, the programs runs a few simple calculations to... (7 Replies)
Discussion started by: Tyler_92
7 Replies
XtRealloc()															       XtRealloc()

Name
  XtRealloc - change the size of an allocated block of storage.

Synopsis
  char *XtRealloc(ptr, num);
	 char *ptr;
	 Cardinal num;

Inputs
  ptr	    Specifies a pointer to memory allocated with XtMalloc(), XtCalloc(), or XtRealloc(), or NULL.

  num	    Specifies the new number of bytes of memory desired in the block.

Returns
  A pointer to allocated memory.

Description
  XtRealloc() changes the size of the block of allocated memory pointed to by ptr to be at least num bytes large.  In order to make this size
  change, it may have to allocate a new block of memory and copy the contents of the old block (or as much as will fit) into the  new  block.
  If  it  allocates a new block of memory, it frees the old block.  In either case, it returns a pointer to a block of memory which is of the
  requested size.  If there is insufficient memory to allocate the new block, XtRealloc() terminates by calling XtErrorMsg().

  If ptr is NULL, XtRealloc() simply calls XtMalloc() to allocate a block of memory of the requested size.

Usage
  Note that XtRealloc() may move the contents of your allocated memory to a new location; the return value may or may not be the same as ptr.
  Not  all  memory can be safely reallocated.  If there are multiple pointers to a block of memory scattered through out an application (such
  as pointers to a widget record), then reallocating that memory is not safe, because all pointers to it cannot  be  updated.	Other  memory
  (such  as  the array of children maintained privately by the Composite widget class) can be safely updated because there should be only one
  pointer to it in the application (in this case the pointer is the composite.children field of the widget).  These cautions are no different
  than those required with the standard realloc() function.

  In most cases, you will have to cast the return value of XtRealloc() to an appropriate pointer type.

  Note that because XtRealloc() behaves like XtMalloc() when passed a NULL pointer, (something that realloc() does not do), you don't have to
  write special case code to allocate the first chunk of memory with XtMalloc() and subsequent chunks with XtRealloc(); you  can  simply  use
  XtRealloc() everywhere.

  Memory  allocated with XtRealloc() must be deallocated with XtFree().  The function XtRealloc() is implemented by the Toolkit independently
  of the particular environment, so programs ported to a system not supporting malloc will still work.

See Also
  XtCalloc(1), XtFree(1), XtMalloc(1), XtNew(1), XtNewString(1).

Xt - Memory Allocation														       XtRealloc()
All times are GMT -4. The time now is 05:49 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy