Welcome, Guest. Please login or register.

Author Topic: char *buf2=new char[size]; problem  (Read 3813 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline kas1eTopic starter

char *buf2=new char[size]; problem
« on: November 11, 2004, 09:33:40 AM »
Hi all.

Today I write a little program which read data per one byte to buf1 and must (in loop) adding per one byte to buf2:

--cut---------------------

char buf1[1];
int size=0;
char *buf2=new char[size]; // ????

main()
{

  while(recv(sockfd,buf1,sizeof(buf1),0)
   {
      memcpy(buf2+size,buf1,sizeof(buf1));
      size++
   };

  printf("%s\n",buf2);

};

--cut---------------------


I mean buf2 size is unknown, and must change. On win32 (bcc compiler), this kind of strings:

int size=0;
char *buf2=new char[size];

give me all what i need, but on amiga (sasc):


==================
  char *buf2=new char[size];
file.c 37 error 9: Undifined identifier "new"

==================
  char *buf2=new char[size];
file.c 37 error 57: semi-colon expected

==================
  char *buf2=new char[size];
file.c 37 error 77: identifier expected


So, any help will be very good. Or how i can fix identifier, or maybe any other way for solve this problem.  Thnx
 

Offline kas1eTopic starter

Re: char *buf2=new char[size]; problem
« Reply #1 on: November 11, 2004, 11:23:26 AM »
>If you really want arrays to grow dynamically you should >create a new one that is larger than the previous. Copy the >previous in the newly created one and then delete the >previous to prevent memory leak.

so, i need :
read from socket 1byte by recv, and put to second buffer.
read agayn 1byte, and append to second buffer. and agayn
and agayn,append and append. buffer2 can be or to small or to large - any. so, yes i need "arrays to grow dynamically".
only that i need:

1. buffer 2 with dynamically size. what best way for it ?
on sasc i mean. (example will be very good).

2. append buffer1 to buffer2 in loop. can't buf2+size? i use this on win32, and it work nice.








 

Offline kas1eTopic starter

Re: char *buf2=new char[size]; problem
« Reply #2 on: November 11, 2004, 11:26:59 AM »
btw:

char buf1[1];
int size=0;
char *buf2=malloc(sizeof(char)*size);

while(recv(new_fd,buf1,sizeof(buf1),0))
  {
    memcpy(buf2+size,buf1,sizeof(buf1));
    size++;
    printf("size: %d\n",size);
    printf("buf2: %s\n",buf2);
                     
  };

compile,run:

size: 1
bif2: a
size: 2
bif2: aa
size: 3
bif2: aaa
size: 4
bif2: aaaa

and next - hardcore reboot/red window of death/etc. why ?:)
on win32 this work good. and i try to change on win32/bcc
this string:
char *buf2 = new char[size];
to
char *buf2=malloc(sizeof(char)*size);

and compiler said to me that can't convert void * to char ..


 

Offline kas1eTopic starter

Re: char *buf2=new char[size]; problem
« Reply #3 on: November 12, 2004, 11:41:03 AM »
to PiR:

big thnx, your sample is was good. i wrote all what i want.

"The idea of PhatBoiCollier to use realloc() is really very good in your case (I can explain why, but this can be hard to understand for you)."

plz, if you have some time, i try understand.

btw, maybe anyone have link on good docs about work with memory managment in advance ? amiga/sasc orientation will be good.