Sponsored Content
Full Discussion: array dynamic allocation
Top Forums Programming array dynamic allocation Post 302301316 by jim mcnamara on Thursday 26th of March 2009 01:32:46 PM
Old 03-26-2009
Also: main always returns an int. It is never void. If your compiler does not complain, it is probably an ancient one. There are a lot of reasons here are two

1. You will get random garbage returned to the calling program or shell

2. Some compilers implement the stack frame for void functions differently than for int functions - for optimization reasons - resulting in a almost impossible to debug crash when main() ends.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

creating a dynamic array in ksh

Hi, Is it possible to create a dynamic array in shell script. I am trying to get the list of logfiles that created that day and put it in a dynamic array. I am not sure about it. help me New to scripting Gundu (3 Replies)
Discussion started by: gundu
3 Replies

2. Shell Programming and Scripting

Dynamic Array Issue

Could one of you, please, provide some input regarding my problem below and it is as follows: I have 2 files that I need to make sure are identical before processing: First, I sort both files Second, I do a diff file1 file2 > File 3 This provides me with the difference. Now, I need to... (6 Replies)
Discussion started by: ddedic
6 Replies

3. Shell Programming and Scripting

creating a dynamic array

i want to create an array the array elements are populated depending upon the number of entries present in a data file The data file is created dynamically how to achieve the same thanks (1 Reply)
Discussion started by: trichyselva
1 Replies

4. Programming

Dynamic memory allocation

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

5. Programming

Creating an array to hold posix thread ids: Only dynamic array works

I am facing a strange error while creating posix threads: Given below are two snippets of code, the first one works whereas the second one gives a garbage value in the output. Snippet 1 This works: -------------- int *threadids; threadids = (int *) malloc (num_threads * sizeof(int)); ... (4 Replies)
Discussion started by: kmehta
4 Replies

6. 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

7. 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

8. Shell Programming and Scripting

dynamic index for array in while loop

Hi, I'm just trying to use a dynamic index for some array elements that I'm accessing within a loop. Specifically, I want to access an array at variable position $counter and then also at location $counter + 1 and $counter + 2 (the second and third array positions after it) but I keep getting... (0 Replies)
Discussion started by: weak_code-fu
0 Replies

9. Shell Programming and Scripting

Assigning values for a dynamic array for an input

Hello, Can somebody please give me a snippet for the below requirement. I want to assign the values separeted by a comma to be assigned to a dynamic array. If I give an input (read statement) like abc1,abc2,abc3,abc4,abc5, all these strings abc* should be assigned to an array like below... (2 Replies)
Discussion started by: suneelj
2 Replies

10. 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
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)
All times are GMT -4. The time now is 07:16 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy