473,385 Members | 1,732 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,385 software developers and data experts.

C++ - Memory Allocation with a void pointer

Good day, I need help ^__^
I have a void pointer: void *data;
I am told to "allocate len+1 characters and set the 'data' to the address of newly allocated memory setting the first character of 'data' to NULL (making the string blank)."

my guess is:

data = new char [len +1];
data[0] = 0;

Is my guess correct?

Thank you so much.
Nov 10 '07 #1
4 4621
JosAH
11,448 Expert 8TB
Good day, I need help ^__^
I have a void pointer: void *data;
I am told to "allocate len+1 characters and set the 'data' to the address of newly allocated memory setting the first character of 'data' to NULL (making the string blank)."

my guess is:

data = new char [len +1];
data[0] = 0;

Is my guess correct?

Thank you so much.
No, sorry, it is not correct. Most likely you want to work/play with C type of strings.
A C 'string' basically is just a sequence of bytes and the last byte contains the
value 0 (or \0 for an alternative notation). So a string "foo" takes four chars, f, o,
o and \0.

Your 'data' pointer is a void pointer now; better make that a char pointer instead
so you refer to all the chars in the sequence it refers to:

Expand|Select|Wrap|Line Numbers
  1. char* data= malloc(len+1);
  2. data[0]= 0; 
  3.  
Those 'len+1' chars can contain a C string up to len+1 chars and the last byte
needs to be \0 char. Setting the first byte to \0 effectively 'constructs' an empty
C string.

You don't have to do that if you're sure that you're going to put a C string in that
area pointed by 'data'. The C string can contain 'len+1' chars including that trailing
\0 char.

For the memory allocation consistently choose either 'new' or 'malloc'. C just
uses 'malloc'; C++ prefers the 'new' operator.

kind regards,

Jos
Nov 10 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
C++ prefers the 'new' operator.
and that means not to use void* in the first place.

void pointers are used in C and not C++.
Nov 10 '07 #3
umm, it's in the assignment specs so i think i don't have a choice, hehehe.

but thanks ^__^
The prof's going to post some code that he made just so we can be on the right track on doing the assignment, I'm just going to wait for that... He showed the starting codes (declaring the first 3 classes, setting up the constructors, etc.) but i forgot to copy the code for allocation with the void *data. I assumed that he's going to post them right away...

Thanks again JosAh and weaknessforcats ^__^
Nov 13 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
umm, it's in the assignment specs so i think i don't have a choice,
Tell your professor to remove void* from C++ problems. This is 2007 and not 1972 with an 8K computer. C++ has specific features (like function overloading) that removes the need foir a void*.
Nov 13 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: jason | last post by:
i'm a little new to VC++, so i'm curious how to appropriate perform the following task. there is a pointer which i wish you point to a buffer of frequently changing size. i'm wondering what is...
11
by: Roman Hartmann | last post by:
hello, I do have a question regarding structs. I have a struct (profil) which has a pointer to another struct (point). The struct profil stores the coordinates of points. The problem is that I...
13
by: Kutty Banerjee | last post by:
Hi, I ve got the following piece of code which does the role of allocating aligned memory (not sure what aligned memory allocation is either). void * _align_calloc(size_t bytes, unsigned long...
72
by: ravi | last post by:
I have a situation where i want to free the memory pointed by a pointer, only if it is not freed already. Is there a way to know whether the memory is freed or not?
1
by: Brandon Langley | last post by:
I have this structure that I am using in conjunction with NetLocalGroupAddMembers: public struct LOCALGROUP_MEMBERS_INFO_3 { public string lgrmi3_domainandname; } I am having failures...
19
by: pinkfloydhomer | last post by:
Please read the example below. I'm sorry I couldn't make it smaller, but it is pretty simple. When client code is calling newThingy(), it is similar to malloc: the client gets a pointer to a...
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
9
by: Steven Powers | last post by:
Imagine the following setup class Parent { virtual void doStuff(); } class Child : public Parent { virtual void doStuff(); }
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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...

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.