Sponsored Content
Full Discussion: double pointer usage
Top Forums Programming double pointer usage Post 302095223 by sajinkoroth on Sunday 5th of November 2006 06:18:11 AM
Old 11-05-2006
I think i made a mistake

Quote:
Originally Posted by axes
Sajin,

I think using single pointer will not cater my requirement, we need to use double pointer,

because str parameter in prep_string function is different from
str pointer variable in main function

CORRECT me if i am wrong
I think the following serves ur purpose if any thing again is wrong with that pls correct me , actually i just typed that code in a hurry that's the reason..
char * prep_string();

int main()
{
char *str; // The dynamicallly allocated string u could use it like any other string
// Like str[30] ,str[i] is valid since it is charachter string

if( (str =prep_string() ) == NULL)
//code to handle not enough memory condition
else
// Now use the str as any other charachter string

return 0;
}

char * prep_string()
{
int size ;
printf("\nEnter the size of the string ");
scanf("%d",&size);
str =(char *) malloc(size * sizeof(char));
if(str == NULL)
// Not enough memory to satisfy the request
//Place the code to handle the situation
return NULL;
else
return str; // Now both pointers point to the same location
}
 

10 More Discussions You Might Find Interesting

1. Programming

Monitor CPU usage and Memory Usage

how can i monitor usages of CPU, Memory, Hard disk etc. under SUN Solaries through a c program or java program i want to store that data into database so i can show it graphically thanks in advance (2 Replies)
Discussion started by: Gajanad Bihani
2 Replies

2. HP-UX

how can I find cpu usage memory usage swap usage and logical volume usage

how can I find cpu usage memory usage swap usage and I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times my final destination is monitor process logical volume usage above X % and number of Logical voluage above can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies

3. Programming

Pointer to a struct (with pointers) *** glibc detected *** double free

I am using a structure defined as follows struct gene_square { double *x; double *y; };I have class, with a member function which is a pointer of this type: gene_square* m_Genes;I am allocating memory in the constructors like this: m_Genes = new gene_square; for (ii=0;... (1 Reply)
Discussion started by: jatoo
1 Replies

4. Solaris

current CPU usage, memory usage, disk I/O oid(snmp)

Hi, I want to monitor the current cpu usage, monitor usage , disk I/o and network utlization for solaris using SNMP. I want the oids for above tasks. can you please tell me that Thank you (2 Replies)
Discussion started by: S_venkatesh
2 Replies

5. Programming

pass a pointer-to-pointer, or return a pointer?

If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it: (1) one is to pass a pointer-to-pointer parameter, like: int my_malloc(int size, char **pmem) { *pmem=(char *)malloc(size); if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies

6. AIX

How to monitor the IBM AIX server for I/O usage,memory usage,CPU usage,network..?

How to monitor the IBM AIX server for I/O usage, memory usage, CPU usage, network usage, storage usage? (3 Replies)
Discussion started by: laknar
3 Replies

7. Shell Programming and Scripting

Confused with the usage of one variable usage

Hi All I am not able to understand the usage of d# in the below variable declaration. FILE_LOC contains the directory path And also help me to know about what will be saved in the variable j. Thanks!!! j=${d#${FILE_LOC}/} (2 Replies)
Discussion started by: mohanm
2 Replies

8. UNIX for Dummies Questions & Answers

Command to display the space usage (memory usage) of a specific directory.

Hi all, Can you please tell me the command, with which one can know the amount of space a specific directory has used. df -k . ---> Displays, the amount of space allocated, and used for a directory. du -k <dir name> - gives me the memory used of all the files inside <dir> But i... (2 Replies)
Discussion started by: abhisheksunkari
2 Replies

9. Programming

double pointer allocation

hi guys how can I allocate a double char pointer? I want an array of pointers to char objects. the objects the double pointer points to are : char b; (1 Reply)
Discussion started by: vlm
1 Replies

10. UNIX for Dummies Questions & Answers

Memory usage per user,percent usage,sytem time in ksh

Let's say i have 20 users logged on Server. How can I know how much memory percent used each of them is using with system time in each user? (2 Replies)
Discussion started by: roy1912
2 Replies
libtalloc_dts(3)						      talloc							  libtalloc_dts(3)

NAME
libtalloc_dts - Chapter 3: Dynamic type system Dynamic type system Generic programming in the C language is very difficult. There is no inheritance nor templates known from object oriented languages. There is no dynamic type system. Therefore, generic programming in this language is usually done by type-casting a variable to void* and transferring it through a generic function to a specialized callback as illustrated on the next listing. void generic_function(callback_fn cb, void *pvt) { /* do some stuff and call the callback */ cb(pvt); } void specific_callback(void *pvt) { struct specific_struct *data; data = (struct specific_struct*)pvt; /* ... */ } void specific_function() { struct specific_struct data; generic_function(callback, &data); } Unfortunately, the type information is lost as a result of this type cast. The compiler cannot check the type during the compilation nor are we able to do it at runtime. Providing an invalid data type to the callback will result in unexpected behaviour (not necessarily a crash) of the application. This mistake is usually hard to detect because it is not the first thing which comes the mind. As we already know, every talloc context contains a name. This name is available at any time and it can be used to determine the type of a context even if we lose the type of a variable. Although the name of the context can be set to any arbitrary string, the best way of using it to simulate the dynamic type system is to set it directly to the type of the variable. It is recommended to use one of talloc() and talloc_array() (or its variants) to create the context as they set its name to the name of the given type automatically. If we have a context with such as a name, we can use two similar functions that do both the type check and the type cast for us: o talloc_get_type() o talloc_get_type_abort() Examples The following example will show how generic programming with talloc is handled - if we provide invalid data to the callback, the program will be aborted. This is a sufficient reaction for such an error in most applications. void foo_callback(void *pvt) { struct foo *data = talloc_get_type_abort(pvt, struct foo); /* ... */ } int do_foo() { struct foo *data = talloc_zero(NULL, struct foo); /* ... */ return generic_function(foo_callback, data); } But what if we are creating a service application that should be running for the uptime of a server, we may want to abort the application during the development process (to make sure the error is not overlooked) and try to recover from the error in the customer release. This can be achieved by creating a custom abort function with a conditional build. void my_abort(const char *reason) { fprintf(stderr, "talloc abort: %s0, reason); #ifdef ABORT_ON_TYPE_MISMATCH abort(); #endif } The usage of talloc_get_type_abort() would be then: talloc_set_abort_fn(my_abort); TALLOC_CTX *ctx = talloc_new(NULL); char *str = talloc_get_type_abort(ctx, char); if (str == NULL) { /* recovery code */ } /* talloc abort: ../src/main.c:25: Type mismatch: name[talloc_new: ../src/main.c:24] expected[char] */ Version 2.0 Tue Jun 17 2014 libtalloc_dts(3)
All times are GMT -4. The time now is 05:41 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy