LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-07-2020, 09:53 PM   #16
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869

TL;DR there is now `new` in C, use malloc/free/realloc.
Code:
int myalloc(Elem **retp, unsigned elements) {
    Elem *p= malloc (sizeof(Elem)*elements);
    if (!p) exit(20);
    *retp= p;
    return 0;
}

int main (void) {
    Elem *arrayptr= NULL;
    myalloc(&arrayptr, 21);
    ...
    free(arrayptr);
    return 0;
}
 
Old 07-08-2020, 01:44 AM   #17
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
Quote:
Originally Posted by bkelly View Post
Yes, it mostly makes sense. I think you explained it well. I was thinking along the lines of **. Combining the * & gives the compiler more information, I presume. And yes, the code does work for me.
Thank you for your time and patience.
I used to say & means (when using here) I pass the variable by its location, so the function will take/handle the original variable. Without & a copy will be passed to the function and that copy will be used inside the function (and will be lost at the end of the function).
All the other things (like: float *farr) are the type and the name of the variable.
 
Old 07-08-2020, 01:58 AM   #18
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
2 comments to post #16 (and in general) - although probably they are not really important here:
1: You can return with an error code from myalloc, and handle that error in main (exiting inside a function is not always fortunate).
2: before malloc the previous content of retp should be freed or will be leaked.
 
1 members found this post helpful.
Old 07-08-2020, 03:43 AM   #19
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Well, in most cases, there's no sensible action when malloc fails, so maybe one should go with some `emalloc/ecalloc/erealloc` functions:
Code:
void *erealloc (void *oldp, size_t newsize) {
    void *newp= realloc (p, newsize);
    if (newp!=NULL || newsize==0) return newp;
    fprintf (stderr, "*** realloc(%p, %lu) failed, exiting\n", oldp, (unsigned long)newsize);
    exit(20);
}
 
Old 07-08-2020, 10:06 AM   #20
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
It can be useful if it is a server. Unwinding the stack, freeing memory as you go may keep the system up and allow for alarms to be issued and responded to - without the server crashing completely. It may also allow for freeing resources other then memory before the server shuts down. Personally I'd prefer to make the decision somewhere other then in an allocation routine for most applications.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Array's.... Making An array bigger??? Tru_Messiah Programming 11 08-17-2016 01:45 PM
BASH-Adding array element: Naming issue using array[${#array[*]}]=5 calvarado777 Programming 8 07-26-2013 09:48 PM
Bash array Add function example using indirect array reference as function argument bobywelsh Programming 10 07-05-2010 04:44 AM
[SOLVED] Threaded function cannot call a function with extern "C" but nonthreaded function can morty346 Programming 16 01-12-2010 05:00 PM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration