C char* and C++ new

D

Dave

I've got a problem. I can't seem to figure out how to make a char*
buffer for use as a socket buffer using operator 'new' in it's basic
form. In affect, I need to allocate space dynamically in memory
pointed to by a char*.

It doesn't appear I can use 'char buffer[length + 5];' and then pass
buffer around as a char*. The compiler complains about being unable
to evaluate the non-constant term between the brackets.

So I thought I might be able to do something like this with new:

int length += 5;
char* buffer = new char[length];

It appears to work - at least my 1995 vintage Unix 10.20 compiler
doesn't complain about the non-constantness. But my program is
seriously broken. It looks like a buffer over-run with garbage in the
stream. I have also wondered if my declaration did work but my use is
wrong. Perhaps I really have a char** and not a char*. Do I need to
terminate the message with a \0? What am I doing wrong and how can I
achieve a dynamic allocation like this?

Dave
 
V

Victor Bazarov

Dave said:
I've got a problem. I can't seem to figure out how to make a char*
buffer for use as a socket buffer using operator 'new' in it's basic
form. In affect, I need to allocate space dynamically in memory
pointed to by a char*.

It doesn't appear I can use 'char buffer[length + 5];' and then pass
buffer around as a char*. The compiler complains about being unable
to evaluate the non-constant term between the brackets.

So I thought I might be able to do something like this with new:

int length += 5;

Ahem... You mean

int length = someotherlength + 5;

don't you?
char* buffer = new char[length];

It appears to work

Really? Your declaration of 'length' shouldn't even compile.
- at least my 1995 vintage Unix 10.20 compiler
doesn't complain about the non-constantness.

In this form of new expression, you are allowed to have non-constant
expression in brackets. Mind you, it's not a declaration, it's
an expression that you use to initialise your pointer.
But my program is
seriously broken.

Not due to your 'new' expression.
It looks like a buffer over-run with garbage in the
stream.

Well, you must be overrunning the buffer... But that usually happens
due to an error in either calculation of an index or of the size of
the array. Make sure you calculate them right.
I have also wondered if my declaration did work but my use is
wrong.
Probably.

Perhaps I really have a char** and not a char*.

No, you seem to be doing this part correctly.
Do I need to
terminate the message with a \0?

That's up to your algorithm. Does it require (presume) the zero at
the end?
What am I doing wrong and how can I
achieve a dynamic allocation like this?

Take any book on C++ and read the chapter on dynamic memory allocation.
It's such a basic stuff that nobody should have to explain it in a NG
posting.

Also, read the FAQ section 5, especially question 5.8. Right up your
alley.

Victor
 
J

jeffc

Victor Bazarov said:
Take any book on C++ and read the chapter on dynamic memory allocation.
It's such a basic stuff that nobody should have to explain it in a NG
posting.

Ouch!
 
D

Dave

Victor Bazarov said:
Dave said:
snip<<

So I thought I might be able to do something like this with new:

int length += 5;

Ahem... You mean

int length = someotherlength + 5;

don't you?
char* buffer = new char[length];

It appears to work

Really? Your declaration of 'length' shouldn't even compile.
- at least my 1995 vintage Unix 10.20 compiler
doesn't complain about the non-constantness.

In this form of new expression, you are allowed to have non-constant
expression in brackets. Mind you, it's not a declaration, it's
an expression that you use to initialise your pointer.
But my program is
seriously broken.

Not due to your 'new' expression.
It looks like a buffer over-run with garbage in the
stream.

Well, you must be overrunning the buffer... But that usually happens
due to an error in either calculation of an index or of the size of
the array. Make sure you calculate them right.
I have also wondered if my declaration did work but my use is
wrong.
Probably.

Perhaps I really have a char** and not a char*.

No, you seem to be doing this part correctly.
Do I need to
terminate the message with a \0?

That's up to your algorithm. Does it require (presume) the zero at
the end?
What am I doing wrong and how can I
achieve a dynamic allocation like this?

Take any book on C++ and read the chapter on dynamic memory allocation.
It's such a basic stuff that nobody should have to explain it in a NG
posting.

Also, read the FAQ section 5, especially question 5.8. Right up your
alley.

Victor

Thanks Victor. I'll read section 5. I have several books on C++
including Stroustrups 3rd Ed of C++ Programming Language and Osborne's
Complete Reference. Unfortunately they all love the terse example of
creating a fixed size array. I have yet to find a good example of
dynamically allocating arrays using 'new'. This may be a task best
suited for malloc.
 
H

Howard

Thanks Victor. I'll read section 5. I have several books on C++
including Stroustrups 3rd Ed of C++ Programming Language and Osborne's
Complete Reference. Unfortunately they all love the terse example of
creating a fixed size array. I have yet to find a good example of
dynamically allocating arrays using 'new'. This may be a task best
suited for malloc.

No, no, no, no, no! :) Don't use malloc. Using new[] and delete[] is much
better. You just need a book with better examples. Stroustrup covers so
much ground that there's not much room for example code.

One good method of allocating arrays that might vary in size is to allocate
an array as large as it will ever need to be, and never resizing it.
Assuming you know what the maximum size is, of course. You don't have to
have an array that's EXACTLY the size you need, just one that's AT LEAST
that size. You can always keep the size you're ACTUALLY using in a variable
(which you probably had to do anyway in order to allocate it, right?).

As others have suggested, if you don't know how big the array might need to
get, you can re-allocate it later, but if you do, it's better to grow it by,
say, twice its old size, than just to the amount requested. And never
shrink it. That saves a lot of re-allocations.

Probably the best idea is to use the std::vector class. That stl class has
saved me a LOT of pain, believe me! :)

-Howard
 
A

Adam Fineman

Dave said:
Thanks Victor. I'll read section 5.

Perhaps you should read the entire FAQ before posting again. I'm not
writing this to be nasty; if you aquaint yourself with the FAQ, you'll
find it much easier to get help in the future. Also, you'll almost
certainly find the answer to your particular problem.

This may be a task best
suited for malloc.

Ugh. Read the FAQ.

- Adam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top