Welcome, Guest. Please login or register.

Author Topic: char *buf2=new char[size]; problem  (Read 3811 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 nex4060

  • Full Member
  • ***
  • Join Date: Sep 2004
  • Posts: 238
    • Show only replies by nex4060
Re: char *buf2=new char[size]; problem
« Reply #1 on: November 11, 2004, 09:48:49 AM »
sounds like you are trying to use a C++ code on a C compiler. The new C++ function is called malloc in C. I think the syntax is:

char *buf2 = (char*) malloc((sizeof(char)*"an integer"));

if I remember corretly

btw:

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

seems wrong because you make a array with no length, hence nothing will be allocated! size should at least be size=1 the you would get a single char in your array. otherwise you get a memory violation.
memcpy dosen't make allocations only copies between memory-locations, hence it could/should fail if the array is zero bytes long.
I might be wrong here, and new char[0] sees that you want to make a single char as in new char or new char[1]
\\"Computer games don\\\'t affect kids; I mean if Pac-Man affected us as kids, we\\\'d all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music.\\"
- Kristian Wilson, Nintendo, Inc,1989  :lol:
 

Offline Doppie1200

  • Sr. Member
  • ****
  • Join Date: May 2004
  • Posts: 497
    • Show only replies by Doppie1200
Re: char *buf2=new char[size]; problem
« Reply #2 on: November 11, 2004, 10:06:12 AM »
Looks like this would not work even if it would be C++

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

this would do nothing since size if 0.

If you want to create an array use a size greater then 0 or end up with zero elements.

Arrays will not grow on their own

memcpy(buf2+size,buf1,sizeof(buf1));
size++

This 'buf2+size' will run out of your allocated space soon and cause all kinds of undetermained behaviour.

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. I don't know if this will affect memory fragmentation though.

Good luck.




Regards,
Erno

(O\\\\_|_/O) <- this is supposed to look like the front of my beetle
(entire front not possible in signature)
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: char *buf2=new char[size]; problem
« Reply #3 on: November 11, 2004, 10:11:01 AM »
Quote
sounds like you are trying to use a C++ code on a C compiler. The new C++ function is called malloc in C. I think the syntax is:

char *buf2 = (char*) malloc((sizeof(char)*"an integer"));

if I remember corretly

You don't.

(sizeof(char) * "string") is nonsense, you need to give it the size of the memory to allocate.
For example:
Code: [Select]

char *str = &quot;a string&quot;;
char *buf = malloc(strlen(str) + 1);
if (buf)
{
  strcpy(buf, str);
  ...
  free(buf);
}
 

Offline nex4060

  • Full Member
  • ***
  • Join Date: Sep 2004
  • Posts: 238
    • Show only replies by nex4060
Re: char *buf2=new char[size]; problem
« Reply #4 on: November 11, 2004, 10:15:19 AM »
@Piru

HAHA!.. okey think you misunderstood me:

malloc( sizeof(char) * "an integer" ); as in
malloc( sizeof(char) * 5); for exampel :-)
here you would get an array og 5 chars :-)
\\"Computer games don\\\'t affect kids; I mean if Pac-Man affected us as kids, we\\\'d all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music.\\"
- Kristian Wilson, Nintendo, Inc,1989  :lol:
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: char *buf2=new char[size]; problem
« Reply #5 on: November 11, 2004, 10:24:45 AM »
@nex4060

That was rather confusing, then.

 

Offline kas1eTopic starter

Re: char *buf2=new char[size]; problem
« Reply #6 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 #7 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 Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: char *buf2=new char[size]; problem
« Reply #8 on: November 11, 2004, 11:34:40 AM »
Quote
and next - hardcore reboot/red window of death/etc. why ?:)

Because the code overwrite memory that is not allocated.
 

Offline nex4060

  • Full Member
  • ***
  • Join Date: Sep 2004
  • Posts: 238
    • Show only replies by nex4060
Re: char *buf2=new char[size]; problem
« Reply #9 on: November 11, 2004, 11:37:08 AM »
you have made an memory-voilation.

you have to make a new array and copy the old one. memcpy dosn't make a new array or enlarges it for you!

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

while(recv(new_fd,buf1,sizeof(buf1),0))
{
   temp = malloc(sizeof(char)*size);
   memcpy(temp,buf2,sizeof(buf2));
   memcpy(temp+size-1,buf1,sizeof(buf1));
   free(buf2);
   buf2 = temp;
   temp = 0;
   size++;

   printf("size: %d\n",size);
   printf("buf2: %s\n",buf2);
};

that should work
\\"Computer games don\\\'t affect kids; I mean if Pac-Man affected us as kids, we\\\'d all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music.\\"
- Kristian Wilson, Nintendo, Inc,1989  :lol:
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: char *buf2=new char[size]; problem
« Reply #10 on: November 11, 2004, 11:45:33 AM »
Quote
memcpy(temp,buf2,sizeof(buf2));

This is wrong. sizeof(buf2) is sizeof(char *), size of a pointer, 4 with 32bit addressing systems, 8 with 64bit addressing ones.

It should be memcpy(temp,buf2,size-1);

Anyway, this code is pretty inefficient, it should use larger buffer size, and preferably linked list of chunks, rather than excessive copying which kills cache.
 

Offline PhatBoiCollier

  • Full Member
  • ***
  • Join Date: Feb 2002
  • Posts: 114
    • Show only replies by PhatBoiCollier
    • http://www.tirinoarim.co.uk
Re: char *buf2=new char[size]; problem
« Reply #11 on: November 11, 2004, 11:49:03 AM »
Not at my miggy at the mo but something like...

------------- 8< --------------
Code: [Select]

// Could do char buf1 here and pass &buf1 to function
char buf1[1];
// Initialise to 0 elements
int size=0;
// Initialise to NULL so realloc works properly!
// Could do &quot;char *buf2 = NULL;&quot;, its the same thing!
char buf2[] = NULL;

while (recv(new_fd,buf1,sizeof(buf1),0))
{
    // Increase size of array
    size++;
    // Reallocate buffer (+1 for terminator!)
    realloc(buf2,(size+1)*(sizeof(char));
    // Copy character (-1 because array starts at 0)
    buf2[size-1] = buf1[0];
    // Reset terminator
    buf2[size] = '\0';
    // Print status
    printf(&quot;size: %d\nbuf2: %s\n&quot;,size,buf2);
}

--------------- >8 ------------

should do it.
There are 10 types of people in this world.
Those that understand binary and those that dont.
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: char *buf2=new char[size]; problem
« Reply #12 on: November 11, 2004, 03:26:35 PM »
Quote
Code: [Select]

// Could do char buf1 here and pass &buf1 to function
char buf1[1];
// Initialise to 0 elements
int size=0;
// Initialise to NULL so realloc works properly!
// Could do &quot;char *buf2 = NULL;&quot;, its the same thing!
char buf2[] = NULL;

while (recv(new_fd,buf1,sizeof(buf1),0))
{
    // Increase size of array
    size++;
    // Reallocate buffer (+1 for terminator!)
    realloc(buf2,(size+1)*(sizeof(char));
    // Copy character (-1 because array starts at 0)
    buf2[size-1] = buf1[0];
    // Reset terminator
    buf2[size] = '\0';
    // Print status
    printf(&quot;size: %d\nbuf2: %s\n&quot;,size,buf2);
}

This code is broken. It crashes horribly when realloc can't resize the allocation, but need to allocate new memory area and move the data. Return value of realloc must be tested for failure or new address.
 

Offline Wain

  • Hero Member
  • *****
  • Join Date: Sep 2002
  • Posts: 745
    • Show only replies by Wain
Re: char *buf2=new char[size]; problem
« Reply #13 on: November 11, 2004, 03:39:16 PM »
Considered a class that manages a dynamically grown linked-list??  That way you don't have to recopy everything into a brand new array every time you get a new piece of data.



Professional Expatriate
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: char *buf2=new char[size]; problem
« Reply #14 on: November 11, 2004, 04:00:24 PM »
I haven't read all of this thread, so I might be missing the obvious:

Why grow an array in tiny increments?

Make a linked list of allocation buffers, each of which contains space for a a few kb of data.

Then simply traverse the list in an outer loop, filling the current buffer and keeping track of how full it is. When it is full, add a new buffer to the list and start from there. You only need to keep track of the size of the current buffer being filled and the overall amount of data recieved.

Disadvantages:

Your data is stored in a noncontiguous way, divided into blocks of several KB. Random access to the data is therefore not efficient when scattered accesses span several blocks.

You can, however traverse the list relatively easily and sequential access would not be a huge performance hit.

Advantages:

No limit to the amount of data that can  be recieved (until memory fills, basically). You never copy anything at any point. Your blocksize, inclusive of link pointers can be carefully chosen to give a single block that matches the system page size. This can be an effective optimisation and help reduce framgentation.


If you really need random read access, there is an alternative.

Allocate an array of pointers that point to sequentially allocated blocks of data. These would be all the same size, again several KB each.

You simply fill the first buffer (pointed to by buffers[0]), once that is full, you allocate another, pointed to by buffers[1] etc.

Random access can then be achieved by simply breaking down the supplied index into its block number and offset into the block.

Advantages:

Likely even faster than the linked list for sequential access and dramatically faster for random access.

Disadvantages:

Unless you periodically grow your buffers array, it has an implicit limit to the amount it can hold.
int p; // A