473,397 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

C char* and C++ new

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
Jul 22 '05 #1
7 2207
"Dave" <sp*****@austin.rr.com> wrote...
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
Jul 22 '05 #2

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:40pNb.55981$5V2.67078@attbi_s53...
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.


Ouch!
Jul 22 '05 #3
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:<40pNb.55981$5V2.67078@attbi_s53>...
"Dave" <sp*****@austin.rr.com> wrote...
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.
Jul 22 '05 #4
"jeffc" <no****@nowhere.com> wrote...

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:40pNb.55981$5V2.67078@attbi_s53...
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.


Ouch!


What?!
Jul 22 '05 #5

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

Jul 22 '05 #6
Dave wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:<40pNb.55981$5V2.67078@attbi_s53>...
<snip>

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

Victor

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.

<snip>
This may be a task best
suited for malloc.


Ugh. Read the FAQ.

- Adam

--
Reverse domain name to reply.

Jul 22 '05 #7

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:08zNb.74528$xy6.133050@attbi_s02...
"jeffc" <no****@nowhere.com> wrote...

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:40pNb.55981$5V2.67078@attbi_s53...
> 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.


Ouch!


What?!


See Adam's response for better manners.
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Christopher Benson-Manica | last post by:
I need a smart char * class, that acts like a char * in all cases, but lets you do some std::string-type stuff with it. (Please don't say to use std::string - it's not an option...). This is my...
5
by: Alex Vinokur | last post by:
"Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message news:4180f756.197032434@news.individual.net... to news:comp.lang.c > ben19777@hotmail.com (Ben) wrote: > > 2) Structure casted into an...
5
by: Sona | last post by:
I understand the problem I'm having but am not sure how to fix it. My code passes two char* to a function which reads in some strings from a file and copies the contents into the two char*s. Now...
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
4
by: Paul Brettschneider | last post by:
Hello all, consider the following code: typedef char T; class test { T *data; public: void f(T, T, T); void f2(T, T, T);
16
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
29
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.