Re: allocating mem in a function and assigning a ptr to the first byte of that mem array...



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.

But i don't it crashes.

Please help,

cheers -mark

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

#include <cmath>
//#include <map>
#include <string>

Not standard C headers. Probably C++.


void Test( void *add, char st[2] )
{
char *tt = new char[3];

new operator does not exist in C.
You need to check the value of tt to make sure it's not NULL.
(In your case you should probably catch the exception which is OT
here).

strcpy( tt, st );
add = (void*)tt;

add has the value passed to the function. Here you change the
value that add holds but not the value that was passed to the
function.

printf( ">> %s\n", (char*)add );
//free( tt );

If you'll free tt here, add will point to memory that doesn't
belong to it. Also, be consistent, use new with delete and malloc
with free. Given the fact that this is c.l.c you can only use
malloc with free.

}

int main( int argc, char **argv )
{
void *a;
Test( a, "ab" );

a is an uninitialized pointer. You should not use it like that
because it's value is passed to Test but you are not allowed to
use its value. Remember the observations for add from above? That
function didn't change the value it received because it can't, so
add has the same value as before passing it to Test.

// crashes here ?

Probably. You should pass a pointer to your pointer and change
that value:
void Test( void **add, char st[2] )
and use *a=tt;
Pass *a as Test(&a,"ab") and you should set a to NULL before
calling Test.

printf( "<< %c\n", *((char*)a + 1 ) );

char *ttt = new char[3];
strcpy( ttt, "ab" );
a = (void*)ttt;
printf( "<< %c\n", *((char*)a + 1 ) );

return 0;
}


You'll have to decide what language you want to use. You mix C
and C++ too much. Although the C functions exist in C++ you
should probably use what C++ provides if you want to stay with
C++ (and start posting to the appropriate group, probably
c.l.c++), or use only C code and get help here.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
.



Relevant Pages

  • Re: trying to make my "fillvalues" function work...
    ... void checkPtr_error(const char *location, const void *pointer) ... fprintf(stderr, "Memory allocation failure (%s). ...
    (comp.lang.c)
  • Re: sizeof(ptr) = ?
    ... The value returned by malloc() is of type 'void*', ... The memory is typeless until an object has been written ... Since 'void' is defined to be an incomplete ... an lvalue of a complete type, there must be a pointer conversion ...
    (comp.lang.c)
  • Re: The usage of %p in C
    ... Can you explain how,say, a "special" pointer can be ... stored in a malloc'ed block of memory which basically returns a single ... as long as converting from one to the other and back works. ... instead of the void * it has been told to expect, and what's worse, it ...
    (comp.lang.c)
  • Re: void pointers & void function pointers
    ... > void; ... try to coerce a pointer of one class to a pointer of the other, ... Machines that maintain "permission bits" on regions of memory ...
    (comp.lang.c)
  • Re: alligned memory allocation
    ... > I ve got the following piece of code which does the role of allocating ... > aligned memory. ... It is a case of a clueless programmer trying to return a pointer that is ... of a void* value. ...
    (comp.lang.c)