473,378 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Basic Use of Malloc

Hi. I am an amature c hobbyist and I wish to begin using malloc as I
have been told that running executables from executables is a less
than elegant way to manage memory use. I need basic examples so me and
my compiler can get familliar with malloc. Please give examples that
do not contain nested assignments and the like as I will get lost
quite easily. As an example of my rudimentary style take a look at how
I create an array and assign a pointer to it:

unsigned long arraysize = 50;
char chArray[arraysize];
char *ptrChArray;
ptrChArray = chArray;

I need a few examples of malloc's proper use that are in the same
rudimentary style. Thank you so much for your time. kentinjapan
Nov 13 '05 #1
21 34342
kent lavallie wrote:
Hi. I am an amature c hobbyist and I wish to begin using malloc as I
have been told that running executables from executables is a less
than elegant way to manage memory use. I need basic examples so me and
my compiler can get familliar with malloc. Please give examples that
do not contain nested assignments and the like as I will get lost
quite easily. As an example of my rudimentary style take a look at how
I create an array and assign a pointer to it:

unsigned long arraysize = 50;
char chArray[arraysize];
char *ptrChArray;
ptrChArray = chArray;

I need a few examples of malloc's proper use that are in the same
rudimentary style. Thank you so much for your time. kentinjapan


Also, you might try

http://www.c-for-dummies.com/lessons/chapter.17

I found the _C for Dummies_ material helpful when I was new to C.

--Steve

Nov 13 '05 #2

"kent lavallie" <ke**********@yahoo.ca> wrote in message
news:d6**************************@posting.google.c om...
Hi. I am an amature c hobbyist and I wish to begin using malloc as I
have been told that running executables from executables is a less
than elegant way to manage memory use. I need basic examples so me and
my compiler can get familliar with malloc. Please give examples that
do not contain nested assignments and the like as I will get lost
quite easily. As an example of my rudimentary style take a look at how
I create an array and assign a pointer to it:

unsigned long arraysize = 50;
char chArray[arraysize];
char *ptrChArray;
ptrChArray = chArray;

I need a few examples of malloc's proper use that are in the same
rudimentary style. Thank you so much for your time. kentinjapan


This is an example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
char *buffer;
char *string1 = "Hello ";

buffer = malloc(30);
strcpy(buffer, string1);
strcat(buffer, "World.");

puts(buffer);
return 0;
}

--
Jeff
Nov 13 '05 #3
On Tue, 09 Sep 2003 20:56:56 -0700, kent lavallie wrote:
I need a few examples of malloc's proper use that are in the same
rudimentary style. Thank you so much for your time. kentinjapan


#include <stdlib.h>
#include <stdio.h>

int main()
{
/* Declare a pointer, TYPE can be anything: int struct double whatever */
TYPE *array;

/* Allocate memory to hold 50 of whatever the type array points to */
array = malloc(50 * sizeof(*array));
if (! array) {
printf("Couldn't allocate memory\n");
exit(1);
}

do_something(array);

/* We need a bigger array */
TYPE *tmp;
/* Realloc allocates a new block, and copies the contents
(as far as possible */
tmp = realloc(array, 70 * sizeof(*array));
if (! tmp) {
printf("Couldn't reallocate memory\n");
exit(1);
}

array = tmp;

do_something_else(array);

/* release the memory when we're done */
free(array);

return 0;
}

hth

--
NPV
"Linux is to Lego as Windows is to Fisher Price." - Doctor J Frink

Nov 13 '05 #4
Jeff wrote:
buffer = malloc(30);
strcpy(buffer, string1);
strcat(buffer, "World.");


Not a point of critisism to the example, but something to watch out for: be
aware that this allocates space for 30 _bytes_, so don't go and try to store
30 integers in that memory. If that's what you want, try this:

int *buffer;
buffer = malloc(30 * sizeof(int));

Good luck,

--
Martijn
http://www.sereneconcepts.nl
Nov 13 '05 #5
Hi Kent,

good to see you found your way to c.l.c. :)

ke**********@yahoo.ca (kent lavallie) wrote:
Hi. I am an amature c hobbyist and I wish to begin using malloc as I
have been told that running executables from executables is a less
than elegant way to manage memory use. I need basic examples so me and
my compiler can get familliar with malloc. Please give examples that
do not contain nested assignments and the like as I will get lost
quite easily. As an example of my rudimentary style take a look at how
I create an array and assign a pointer to it:

unsigned long arraysize = 50;
char chArray[arraysize];
char *ptrChArray;
ptrChArray = chArray;

I need a few examples of malloc's proper use that are in the same
rudimentary style. Thank you so much for your time. kentinjapan


Besides the examples I gave to you on c.l.c.moderated and by mail,
here is one more:

/* -------8<----------------- */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TEXT_SIZE 1000

int main( void )
{
char *text, *copy, *cp;
int i, len, ecount;

/* Allocate some memory, checking for error:
*/
text = malloc( TEXT_SIZE );
if ( text == NULL )
{
printf("Memory allocation for 'text' failed!");
exit( EXIT_FAILURE );
}

/* Read some user input into text:
*/
printf( "\nPlease enter some text: " );
fgets( text, TEXT_SIZE, stdin );

/* Now we step through text, counting 'e's
** as we go, using a pointer-to-char:
*/
ecount = 0;
for ( cp = text; *cp != '\0'; cp++ )
{
if ( *cp == 'e' )
ecount++;
}
printf( "The text you entered contains %d 'e's.\n",
ecount );

/* Now we use array syntax instead, changing
** all 'u's to 'x's as we go:
*/
for ( i = 0; i < TEXT_SIZE; i++ )
{
if ( text[ i ] == 'u' )
text[ i ] = 'x';
}
printf( "'u' changed to 'x': %s", text );

/* Now we allocate just enough space to hold
** the text the user entered (note that we have
** to add 1 to hold the terminating '\0'):
*/
len = strlen( text ) + 1;
copy = malloc( len );
if ( copy == NULL )
{
printf("Memory allocation for 'copy' failed!");
exit( EXIT_FAILURE );
}

/* Copy from 'text' to 'copy':
*/
strcpy( copy, text );
printf( "The copy reads: %s", copy );

return EXIT_SUCCESS;
}

/* -------8<----------------- */

Regards

Irrwahn
--
What does this red button do?
Nov 13 '05 #6
Martijn wrote:

Jeff wrote:
buffer = malloc(30);
strcpy(buffer, string1);
strcat(buffer, "World.");


Not a point of critisism to the example, but something to watch out for: be
aware that this allocates space for 30 _bytes_, so don't go and try to store
30 integers in that memory. If that's what you want, try this:

int *buffer;
buffer = malloc(30 * sizeof(int));

Good luck,

--
Martijn
http://www.sereneconcepts.nl


The clc canon eschews magic numbers and unnecessary type names. Regard..

int *buffer, size = 30;
buffer = malloc( size * sizeof *buffer );
--
Joe Wright mailto:jo********@earthlink.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #7
Irrwahn Grausewitz <ir*****@freenet.de> writes:
printf("Memory allocation for 'text' failed!");
exit( EXIT_FAILURE );
It is implementation-defined whether or not the last line written to
a text stream requires a terminating new-line character. For maximal
portability, you might want to write a '\n' as the last character.
printf( "\nPlease enter some text: " );
fgets( text, TEXT_SIZE, stdin );
Add `fflush (stdout);' after the `printf' function call. Otherwise, the
text is not guaranteed to be written before the `fgets' call.
printf("Memory allocation for 'copy' failed!");
exit( EXIT_FAILURE );
Same comment as the first one.
printf( "The copy reads: %s", copy );
return EXIT_SUCCESS;


Same comment as immediately above.

Martin
Nov 13 '05 #8
Martin Dickopp <ex*************@zero-based.org> wrote:
Irrwahn Grausewitz <ir*****@freenet.de> writes:
printf("Memory allocation for 'text' failed!");
exit( EXIT_FAILURE );
It is implementation-defined whether or not the last line written to
a text stream requires a terminating new-line character. For maximal
portability, you might want to write a '\n' as the last character.

Got me. :)
printf( "\nPlease enter some text: " );
fgets( text, TEXT_SIZE, stdin );
Add `fflush (stdout);' after the `printf' function call. Otherwise, the
text is not guaranteed to be written before the `fgets' call.

Again. Doh!
printf("Memory allocation for 'copy' failed!");
exit( EXIT_FAILURE );
Same comment as the first one.

You're so right... sigh...
printf( "The copy reads: %s", copy );
return EXIT_SUCCESS;


Same comment as immediately above.

Ah, IMHO /this one/ is OK, because I left the '\n' in 'text'
and therefore in 'copy', thus I should be safe in this case,
shouldn't I?

Irrwahn
--
What does this red button do?
Nov 13 '05 #9
Irrwahn Grausewitz <ir*****@freenet.de> writes:
Martin Dickopp <ex*************@zero-based.org> wrote:
Irrwahn Grausewitz <ir*****@freenet.de> writes:
printf( "The copy reads: %s", copy );
return EXIT_SUCCESS;


Same comment as immediately above.


Ah, IMHO /this one/ is OK, because I left the '\n' in 'text' and
therefore in 'copy', thus I should be safe in this case, shouldn't I?


The `fgets' function only stores a newline character if it encounters one
while reading the number of characters it is supposed to read maximally.
If the user enters more characters, or the end-of-file condition or an
error occurs, no newline character is stored.

Which reminds me: Since you don't check the return value of `fgets', it
could also happen that `text' remains unchanged (if end-of-file occurs
before any characters have been read) or becomes indeterminate (if an
error occurs). In both cases, you invoke undefined behavior. :)

Martin
Nov 13 '05 #10
Martin Dickopp <ex*************@zero-based.org> wrote:
Irrwahn Grausewitz <ir*****@freenet.de> writes:
Martin Dickopp <ex*************@zero-based.org> wrote:
>Irrwahn Grausewitz <ir*****@freenet.de> writes:
>
>> printf( "The copy reads: %s", copy );
>> return EXIT_SUCCESS;
>
>Same comment as immediately above.


Ah, IMHO /this one/ is OK, because I left the '\n' in 'text' and
therefore in 'copy', thus I should be safe in this case, shouldn't I?


The `fgets' function only stores a newline character if it encounters one
while reading the number of characters it is supposed to read maximally.
If the user enters more characters, or the end-of-file condition or an
error occurs, no newline character is stored.

Which reminds me: Since you don't check the return value of `fgets', it
could also happen that `text' remains unchanged (if end-of-file occurs
before any characters have been read) or becomes indeterminate (if an
error occurs). In both cases, you invoke undefined behavior. :)


Oh dear! =%O

Hm, at least I wrote: int main( void )

:)

--
What does this red button do?
Nov 13 '05 #11
In 'comp.lang.c', "Martijn" <su*********************@hotNOFILTERmail.com>
wrote:
Jeff wrote:
buffer = malloc(30);
strcpy(buffer, string1);
strcat(buffer, "World.");


Not a point of critisism to the example, but something to watch out for:
be aware that this allocates space for 30 _bytes_, so don't go and try
to store 30 integers in that memory. If that's what you want, try this:

int *buffer;
buffer = malloc(30 * sizeof(int));


or simply:

any_type_except_void *buffer = malloc (30 * sizeof *buffer);

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #12
In 'comp.lang.c', Joe Wright <jo********@earthlink.net> wrote:
The clc canon eschews magic numbers and unnecessary type names. Regard..

int *buffer; size_t const size = 30; buffer = malloc( size * sizeof *buffer );


--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #13

"Steve Zimmerman" <st******@sonic.net> wrote in message
news:3F**************@sonic.net...
Also, you might try

http://www.c-for-dummies.com/lessons/chapter.17

I found the _C for Dummies_ material helpful when I was new to C.


uhoh, skimmed through it.

"void main()", I guess it will always be there
Nov 13 '05 #14
On 10 Sep, in message <d6**************************@posting.google.com >
ke**********@yahoo.ca (kent lavallie) wrote:
Hi. I am an amature c hobbyist and I wish to begin using malloc as I
have been told that running executables from executables is a less
than elegant way to manage memory use. I need basic examples so me and
my compiler can get familliar with malloc. Please give examples that
do not contain nested assignments and the like as I will get lost
quite easily.


This should be a simple one to understand:

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
char* c;
if ((c = malloc( 10)) == NULL)
{
fprintf (stderr, "Unable to allocate memory.\n");
exit (EXIT_FAILURE);
}

exit (EXIT_SUCCESS);
}

Don't be confused about the if and malloc being on the same line - look
at it from the inside pair of brackets outwards:

( 10) /* 10 is the argument passed to malloc */
(c = malloc( 10)) /* malloc's return value is assigned to c */
if ((c = malloc( 10)) == NULL) /* if malloc's return value is NULL
then the condition is true /*

Yours,

Phil L.
--
http://www.philipnet.com http://director.sourceforge.net

Nov 13 '05 #15

"Joe Wright" <jo********@earthlink.net> wrote in message
news:3F***********@earthlink.net...
Martijn wrote:

Jeff wrote:
buffer = malloc(30);
strcpy(buffer, string1);
strcat(buffer, "World.");


Not a point of critisism to the example, but something to watch out for: be aware that this allocates space for 30 _bytes_, so don't go and try to store 30 integers in that memory. If that's what you want, try this:

int *buffer;
buffer = malloc(30 * sizeof(int));

Good luck,

--
Martijn
http://www.sereneconcepts.nl


The clc canon eschews magic numbers and unnecessary type names. Regard..

int *buffer, size = 30;
buffer = malloc( size * sizeof *buffer );


Yes, we shoudn't put magic number in the code. Also, i didn't show a
"free(buffer)" in my code to indicate that we should free the memory we
allocated.
--
Jeff

Nov 13 '05 #16
Steve Zimmerman wrote:
Also, you might try

http://www.c-for-dummies.com/lessons/chapter.17
Only, perhaps, as an example of how /not/ to use malloc.

Allow me to post some code from that site, which I'll "enquote" to show that
it's not my code (but please understand that the quote characters do not
mean that I'm claiming you wrote it!):
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
Non-standard header.
#include <string.h>

void main()
Undefined behaviour.
{
struct pres {
char name[25];
struct pres *next;
};
Generally speaking, it's poor style to define a type inside main(), because
a serious program would expect to do little of its work in main() itself.
This, however, is not a serious program, so we'll let it off.

struct pres *president;

/* Create the first item in the list */

president = (struct pres *)malloc(sizeof(struct pres));
Unnecessary cast, clumsy sizeof.

president = malloc(sizeof *president);

strcpy(president->name,"George Washington");
Completely failed to check the return value from malloc.

Hmmm, "George Washington" sounds familiar. Have you posted this elsegroup
recently?
president->next = (struct pres *)malloc(sizeof(struct pres));
Same comments apply as to previous malloc.

/* Stop here and display the results */

printf("The first structure has been created:\n");
printf("president->name = %s\n",president->name);
printf("next structure address = %i\n",president->next);
This last line exhibits undefined behaviour. Even if we assume president is
not NULL, it should read:

printf("next structure address = %p\n", (void *)president->next);

}


No return value from a function that is required to return int.

In short, the code sucks. The book makes no attempt to teach the C language,
but rather the author's personal, flawed understanding of that language.
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #17
Richard Heathfield wrote:
The book makes no attempt to teach the C language,
but rather the author's personal,
flawed understanding of that language.


He's one of those, who has deduced what C is,
from his experience with his compiler.

"Note that it's "might" screw things up. In fact, even the void main()
thing is an iffy. I've gotten complaints from various
self-proclaimed C Lords, some of which are even USENET C Jerks,
who proclaim the evils of void main() and
that I'm an ass for every propogating such nonsense.

Perhaps I am an ass. But not one of these C poobahs has
yet to provide a specific instance of void main() or the lack of a
return statement actually screwing anything up.
No, they just speak in generalities, which is typical
(most likely because their knowledge of C is weak
and their self-importance overrated)."

--
pete
Nov 13 '05 #18
I'm far from an experienced C programmer, but if you ask me, ignoring standards
because your compiler happens to generate the right instructions from something
incorrect, is sheer folly. A good example of where this is deadly is with HTML
(and the browsers' interpreters). The HTML writers among us all know of the
torment that the different browsers place on us by differing standards and
behaviours, and as such I learned everything I know of HTML from specifications
-- not by example. I'm sure similar things occur between C compilers.

If I were learning the C language (which to a certain extent I still am), I
would make sure my code is standards-compliant, not only because it would be
more easily ported to different platforms and compilers, but also it looks much
more impressive when your boss reads through your code, assuming of course
he/she knows the difference.
If I were an employer, an employee would not be presenting himself as a good
programmer if I the first thing I saw was void main(), am I right?

Just like a piano student must learn good technique in order to be able to play
a piece impressively, so should the student of C strive to write correct code.
Remember: unlike the pianist, us programmers cannot fall back upon 'artistic
license'...

My two cents,
Koster.

On Thu, 11 Sep 2003 11:27:42 GMT, pete <pf*****@mindspring.com> wrote:

|Richard Heathfield wrote:
|
|> The book makes no attempt to teach the C language,
|> but rather the author's personal,
|> flawed understanding of that language.
|
|He's one of those, who has deduced what C is,
|from his experience with his compiler.
|
|"Note that it's "might" screw things up. In fact, even the void main()
| thing is an iffy. I've gotten complaints from various
| self-proclaimed C Lords, some of which are even USENET C Jerks,
| who proclaim the evils of void main() and
| that I'm an ass for every propogating such nonsense.
|
| Perhaps I am an ass. But not one of these C poobahs has
| yet to provide a specific instance of void main() or the lack of a
| return statement actually screwing anything up.
| No, they just speak in generalities, which is typical
|(most likely because their knowledge of C is weak
| and their self-importance overrated)."

Nov 13 '05 #19
ke**********@yahoo.ca (kent lavallie) wrote in message news:<d6**************************@posting.google. com>...
Hi. I am an amature c hobbyist and I wish to begin using malloc as I
have been told that running executables from executables is a less
than elegant way to manage memory use. I need basic examples so me and
my compiler can get familliar with malloc. Please give examples that
do not contain nested assignments and the like as I will get lost
quite easily. As an example of my rudimentary style take a look at how
I create an array and assign a pointer to it:

unsigned long arraysize = 50;
char chArray[arraysize];
char *ptrChArray;
ptrChArray = chArray;

I need a few examples of malloc's proper use that are in the same
rudimentary style. Thank you so much for your time. kentinjapan


#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#define ARRSIZE 50 /* same for all arrays */

int main (void)
{
char *ptrChArray = NULL;

ptrChArray = malloc (ARRSIZE * sizeof *ptrChArray);
if (!ptrChArray)
{
printf ("malloc() failed for %d elements of size %lu\n",
ARRSIZE, sizeof *ptrChArray);
exit (EXIT_FAILURE);
}

/* do stuff with ptrChArray here */
strcpy (ptrChArray, "This is a test");
printf ("%s", ptrChArray);

/* free the memory when we're done with it */
free (ptrChArray);

return 0;
}

Slightly more complicated example: allocating and freeing 2D arrays of int.

#include <stdlib.h>
#include <stdio.h>

/* allocate int arr[d0][d1] */
int **new2Darr (size_t d0, size_t d1)
{
int **arr = NULL;

/* allocate an array of d0 elements of type int* */
arr = malloc (d0 * sizeof *arr);
if (arr)
{
size_t i;
for (i = 0; i < d0; i++)
{
/* allocate an array of d1 elements of type int */
arr[i] = malloc (d1 * sizeof **arr);
if (!arr[i])
{
break;
}
}

/*
** If all of the above malloc() operations succeed,
** i should equal d0. If it's less, then a call
** to malloc has failed. If that happens, we free
** all the memory that has been allocated to this
** point and return NULL.
*/
if (i < d0)
{
while (--i >= 0)
{
free (arr[i]);
}
free (arr);
arr = NULL;
}
}
return (arr);
}

void delete2Darr (int ***arr, size_t d0)
{
int i;
if (*arr)
{
for (i = 0; i < d0; i++)
{
free (*arr[i]);
}
free (*arr);
*arr = NULL;
}
}

int main (void)
{
int **a1 = NULL;

a1 = new2Darr (10, 20); /* int a1[10][20] */

if (a1)
{
/* do fun stuff with a1 */
a1[0][0] = ...;
a1[0][1] = ...;

/*
** When we're done with a1, we should explicitly
** free the memory. Note that we have to remember
** at least the first dimension of the array to
** properly deallocate all of the 1d arrays; there's
** no way to retrieve this information from the pointer
** itself.
*/
delete2Darr (&a1, 10);
}

exit (0);
}
Nov 13 '05 #20
On Wed, 10 Sep 2003 13:35:01 GMT, Joe Wright
<jo********@earthlink.net> wrote in comp.lang.c:
Martijn wrote:

Jeff wrote:
buffer = malloc(30);
strcpy(buffer, string1);
strcat(buffer, "World.");


Not a point of critisism to the example, but something to watch out for: be
aware that this allocates space for 30 _bytes_, so don't go and try to store
30 integers in that memory. If that's what you want, try this:

int *buffer;
buffer = malloc(30 * sizeof(int));

Good luck,

--
Martijn
http://www.sereneconcepts.nl


The clc canon eschews magic numbers and unnecessary type names. Regard..


....and the clc cannon is always ready to fire on those who violate the
canon...

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #21
pete <pf*****@mindspring.com> wrote in message news:<3F***********@mindspring.com>...
Richard Heathfield wrote:
The book makes no attempt to teach the C language,
but rather the author's personal,
flawed understanding of that language.


He's one of those, who has deduced what C is,
from his experience with his compiler.

"Note that it's "might" screw things up. In fact, even the void main()
thing is an iffy. I've gotten complaints from various
self-proclaimed C Lords, some of which are even USENET C Jerks,
who proclaim the evils of void main() and
that I'm an ass for every propogating such nonsense.

Perhaps I am an ass. But not one of these C poobahs has
yet to provide a specific instance of void main() or the lack of a
return statement actually screwing anything up.
No, they just speak in generalities, which is typical
(most likely because their knowledge of C is weak
and their self-importance overrated)."


I've nver known void main() to screw things up either, but then for
the tiny effort of int main() and a return, why bother stocking with
void main?

As i got used to the int main() advice here (and elsewhere) i went
along with it. It may not seem to make a difference with the popular
computer OSes, but C is used all over the place, why rely on the
platform to sort it out?

When clc regulars like Richard give advice based on the standard it
comes at no personal cost, an often with very little rework of your
existing code needed to get it just that bit more right. I don't see
a reason to complain.

On malloc i did all that uncessary casting stuff too, once.

Thing is, you could buy that book, get by on your own system and then
one day encounter a subtle bug that you just don't understand, and you
don't get it because you'd followed some bad practice and don;t know
better. Pretty frustrating outcome i hope you'll agree.

( i enjoyed C Programming: A Modern Approach, King, as a learning
tool, hoping that's a good example! )
Nov 13 '05 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: steve | last post by:
I'm trying to create a structure of three pointers to doubles. For which I have: typedef struct { double *lst_t, *lst_vc, *lst_ic; } last_values; I then need to allocate space for...
9
by: shan_rish | last post by:
Hi CLCers, I coded a function to allocate memory and i am passing a pointer to the function. The code is compiling but throws error and closes while executing. The program is as below:...
21
by: ramu | last post by:
Hi, Will the memory allocated by malloc and realloc be contiguous? regards
25
by: Why Tea | last post by:
Thanks to those who have answered my original question. I thought I understood the answer and set out to write some code to prove my understanding. The code was written without any error checking....
58
by: Jorge Peixoto de Morais Neto | last post by:
I was reading the code of FFmpeg and it seems that they use malloc just too much. The problems and dangers of malloc are widely known. Malloc also has some overhead (although I don't know what is...
1
by: Kevin | last post by:
Hi all, I clearly have an issue with some pointers, structures, and memory allocation. Its probably pritty basic, but I'm a little stuck. Any help would be greatly appreciated. I'd like...
71
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a...
173
by: Marty James | last post by:
Howdy, I was reflecting recently on malloc. Obviously, for tiny allocations like 20 bytes to strcpy a filename or something, there's no point putting in a check on the return value of malloc....
26
by: Muzammil | last post by:
whats the beauty of "malloc" over "new" why its helpful for programmer.for its own memory area.??
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.