Re: allocating mem in a function and assigning a ptr to the first byte of that mem array...
- From: Martin Ambuhl <mambuhl@xxxxxxxxxxxxx>
- Date: Wed, 28 Feb 2007 15:47:04 -0500
mast2as@xxxxxxxxx wrote:
I almost apologize to ask this question but I have been starting at
this code for a bit of time and don't understand what's wrong with it.
I am not arguing about the fact it's good or not coding, it's actually
a shorter version of something more complex that i am doing.
Anway the idea is that there's a func Test which takes a pointer to
void argument, allocate some memory, set this memory with some cotent,
assign the ptr to a void to the first byte of this new alloc memory
and returuns. Normally because our argument points to the first byte
of that memory array * AND THAT THE MEM HASN'T BEEN FREED *, i should
still be able to access the content of that mem array.
After cleaning up your C++-isms, you will still have a logical error in the way you call your Test() function. Please examine the following carefully, comparing it to your original.
#include <stdlib.h>
#include <stdio.h>
#if 0
/* mha: apart from the problems of using '//' style comments in usenet
postings, no matter what the language, and with using them with C89
or C90 compilers, where they are errors, commenting out code is best
done with the #if / #endif form exemplified here. */
#include <map>
#endif
#if 0
/* mha: there are no headers named <cmath> or <string> in C. */
#include <cmath>
#include <string>
#endif
#include <string.h> /* mha */
void Test(void **add /* mha: NB */ , char st[2])
{
#if 0
/* mha: the following is a syntax error in C. A replacement follows.
It appears that your function provides no way to report failures. */
char *tt = new char[3];
#endif
char *tt;
if (!(tt = malloc(3))) {
fprintf(stderr, "%s",
"malloc failed, and 'Test()' provides no way to "
"handle that.\nI'm giving up.\n");
exit(EXIT_FAILURE);
}
strcpy(tt, st);
*add = tt; /* mha: NB */
printf(">> %s\n", (char *) *add);
/* mha: it is a good thing you don't free tt; In your off-topic
C++-ism you allocate it with new. Such things are freed with
delete or delete[], not with free(). */
// free( tt );
}
int main(void)
{
void *a;
Test(&a, "ab"); /* mha: NB */
printf("<< %c\n", *((char *) a + 1));
{
#if 0
/* mha: declaration following executable statements in the block
is an error in C89 or C90. I have added a block. */
/* mha: the following is a syntax error in C. A replacement
follows. */
char *ttt = new char[3];
#endif
char *ttt;
if (!(ttt = malloc(3))) {
fprintf(stderr, "%s",
"malloc failed in main(),\nI'm giving up.\n");
free(a);
exit(EXIT_FAILURE);
}
strcpy(ttt, "ab");
a = (void *) ttt;
printf("<< %c\n", *((char *) a + 1));
/* mha: Please clean up after yourself. */
free(ttt); /* mha */
}
/* mha: Please clean up after yourself. */
free(a); /* mha */
return 0;
}
.
- References:
- Prev by Date: Re: Comparing string input to enum data type
- Next by Date: Re: Source Code for C Unleashed
- Previous by thread: Re: allocating mem in a function and assigning a ptr to the first byte of that mem array...
- Next by thread: Polyspace Problem
- Index(es):
Relevant Pages
|