Practice11 - Dynamic Allocation Array variables have fixed...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Dynamic Allocation Array variables have fixed size, used to store a fixed and known amount of variables This size can’t be changed after compilation However, we don’t always know in advance how much space we would need for an array or a variable We would like to be able to dynamically allocate memory The malloc function void *malloc( unsigned int nBytes); The function malloc is used to dynamically allocate nBytes worth of space How to determine nBytes? malloc returns a pointer to the allocated area on success, NULL on failure You should always check whether memory was successfully allocated Remember to #include <stdlib.h> Example dynamic_reverse_array.c Why casting? The casting in y=( int *) malloc(n* sizeof ( int )); is needed because malloc returns void * : void *malloc( unsigned int nbytes); The type void * specifies a general pointer, which can be cast to any pointer type. What is this ‘sizeof’ ? The sizeof operator gets a variable or a type as an input and outputs its size in bytes: double x; s1= sizeof (x); /* s1 is 8 */ s2= sizeof ( int ) /* s2 is 4 */ Free the allocated memory segment void free( void *ptr); We use free(p) to free the allocated memory pointed to by p If p doesn’t point to an area allocated by malloc, a run-time error occurs Always remember to free the allocated memory once you don’t need it anymore Otherwise, you may run out of memory before you know it! Another example another_strcpy.c Exercise Implement the function my_strcat – Input – two strings, s1 and s2 Output – a pointer to a dynamically allocated concatenation (‘shirshur’) For example: The concatenation of “hello_” and “world!” is the string “hello_world!” Write a program that accepts two strings from the user and prints their concatenation Assume input strings are no longer than a 100 chars Solution my_strcat.c (my_strcat2.c) What’s wrong with this? char *my_strcat( char *str1, char *str2) { int len; char result[500]; /* Let’s assume this is large enough */ len = strlen(str1); strcpy(result, str1); strcpy(result+len, str2); return result; } Structures Often we want to be able to manipulate ‘logical entities’ as a whole For example, complex numbers, dates, student records, etc’ Each of these must be composed of more than one variable type, but are logically units A struct (short for structure) is a collection of...
View Full Document

This note was uploaded on 01/10/2010 for the course CS 463 taught by Professor Can'tsay during the Spring '09 term at Haaga - Helia University of Applied Sciences.

Page1 / 52

Practice11 - Dynamic Allocation Array variables have fixed...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online