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

createing a *char with malloc but its giveing me too much space

hi there i was trying to create memory for a char dynamicaly. However when
i do this it seens to create 4 extra spaces in the veriable for stuff to be
stored.

char *tempChar = (char*)malloc(strlen(cStringBeingLookedAt) );

that is what im doing but when i come to look at it in debug mode it shows
as "IIIIIIIIyyyy"

each I = a space for a char. However it sticks these 4 y's on the end.
This leaves me a 4 char space each time and i dont' want these. i tried to
do

char *tempChar = (char*)malloc(strlen(cStringBeingLookedAt) - 4);

but if my string that im alocating space for is less than 4 it comes up with
a memory error. Im trying to compare it with another script but i can't do
this while these extra y's are on the end.

Any help would be grate

Thanks Gizmo
Nov 13 '05 #1
8 2242
On Thu, 28 Aug 2003 16:48:54 +0100, Gizmo wrote:
hi there i was trying to create memory for a char dynamicaly. However when
i do this it seens to create 4 extra spaces in the veriable for stuff to be
stored.

char *tempChar = (char*)malloc(strlen(cStringBeingLookedAt) );
char *tempChar = malloc(strlen(cStringBeingLookedAt) +1);

Don't cast, leave room for '\0'

that is what im doing but when i come to look at it in debug mode it shows
as "IIIIIIIIyyyy"
How did you put those I's there? Show us all of the code.

each I = a space for a char. However it sticks these 4 y's on the end.
This leaves me a 4 char space each time and i dont' want these. i tried to
do

char *tempChar = (char*)malloc(strlen(cStringBeingLookedAt) - 4);
That's completely wrong.
Im trying to compare it with another script but i can't do
this while these extra y's are on the end.


Yes you can, if you null terminate your string properly and/or remeber
it's length.
regards
NPV
Nov 13 '05 #2
the "IIIIIIIIyyyy" was me just trying to copy what i saw in the debugger
when i went to look at the memory location. all the code is as follows

int InStr(char *cStringBeingLookedAt, char *cStringToCompareTo, bool
CaseSensitive)
{
char *cSubString = NULL;
int iPostion = 0;
char *tempChar = (char*)malloc(strlen(cStringBeingLookedAt) );
char *tempChar1 = (char*)malloc(strlen(cStringToCompareTo) );

if(cStringBeingLookedAt == NULL || cStringToCompareTo == NULL )
{
cSubString = NULL;
delete cSubString;
return -1;
}

//length of cStringBeingLookedAt = 0
if(strlen(cStringBeingLookedAt) == 0 )
{
cSubString = NULL;
delete cSubString;
return 0;
}

//you decide what to return
if(strlen(cStringToCompareTo) == 0)
{
cSubString = NULL;
delete cSubString;
return 0;
}
if (CaseSensitive == false)
{
for (unsigned int x = 0; x < strlen(cStringBeingLookedAt); x++)
{
tempChar[x] = char(toupper(cStringBeingLookedAt[x]));
}

for (unsigned int y = 0; y < strlen(cStringToCompareTo); y++)
{
tempChar1[y] = char( toupper(cStringToCompareTo[y]) );
}
}

//cSubString = strstr(cStringBeingLookedAt, cStringToCompareTo);
cSubString = strstr(tempChar, tempChar1); // <--------- when it comes to
here it has that extra bit on so it does not do what i want

//String not found
if(cSubString == NULL)
{
cSubString = NULL;
delete cSubString;
return -1;
}
//String found
else
{
iPostion = ((int)cSubString - (int)cStringBeingLookedAt);
cSubString = NULL;
delete cSubString;

return iPostion;
}
}

"Nils Petter Vaskinn" <no@spam.for.me.invalid> wrote in message
news:pa****************************@spam.for.me.in valid...
On Thu, 28 Aug 2003 16:48:54 +0100, Gizmo wrote:
hi there i was trying to create memory for a char dynamicaly. However when i do this it seens to create 4 extra spaces in the veriable for stuff to be stored.

char *tempChar = (char*)malloc(strlen(cStringBeingLookedAt) );


char *tempChar = malloc(strlen(cStringBeingLookedAt) +1);

Don't cast, leave room for '\0'

that is what im doing but when i come to look at it in debug mode it shows as "IIIIIIIIyyyy"


How did you put those I's there? Show us all of the code.

each I = a space for a char. However it sticks these 4 y's on the end.
This leaves me a 4 char space each time and i dont' want these. i tried to do

char *tempChar = (char*)malloc(strlen(cStringBeingLookedAt) - 4);


That's completely wrong.
Im trying to compare it with another script but i can't do
this while these extra y's are on the end.


Yes you can, if you null terminate your string properly and/or remeber
it's length.
regards
NPV

Nov 13 '05 #3
On Thu, 28 Aug 2003 16:54:42 +0000, Nils Petter Vaskinn wrote:
On Thu, 28 Aug 2003 17:27:26 +0100, Gizmo wrote:
the "IIIIIIIIyyyy" was me just trying to copy what i saw in the debugger
when i went to look at the memory location. all the code is as follows

int InStr(char *cStringBeingLookedAt, char *cStringToCompareTo, bool


This may be close to what you wanted (untested):

/* Return index of first different character -1 if they match */
int compareStr(char *a, char *b, int caseSensitive)
{
int i = 0;
while (a[i] && b[i]) {
if (caseSensitive && (toUpper(a[i]) != toUpper(b[i]))) return i;
else if (a[i] != b[i]) return i;
}
return -1;
}

compareStr(NULL,"test",0) returns 0
compareStr("Test",NULL,0) returns 0
compareStr("Test","ttst",0) returns 1
compareStr("test","test",0) returns -1
Nov 13 '05 #4
Prob solved thanks all was the '\0' that did it. sorry if i did poast in
wrong one i did in c instead of c++ cos i was using malloc.

thanks for the quick replys

Gizmo
"Gizmo" <sc***********@hotmail.com> wrote in message
news:bi**********@newsg1.svr.pol.co.uk...
hi there i was trying to create memory for a char dynamicaly. However when i do this it seens to create 4 extra spaces in the veriable for stuff to be stored.

char *tempChar = (char*)malloc(strlen(cStringBeingLookedAt) );

that is what im doing but when i come to look at it in debug mode it shows
as "IIIIIIIIyyyy"

each I = a space for a char. However it sticks these 4 y's on the end.
This leaves me a 4 char space each time and i dont' want these. i tried to do

char *tempChar = (char*)malloc(strlen(cStringBeingLookedAt) - 4);

but if my string that im alocating space for is less than 4 it comes up with a memory error. Im trying to compare it with another script but i can't do this while these extra y's are on the end.

Any help would be grate

Thanks Gizmo

Nov 13 '05 #5
On Thu, 28 Aug 2003 16:48:54 +0100, "Gizmo" <sc***********@hotmail.com>
wrote:
that is what im doing but when i come to look at it in debug mode it shows
as "IIIIIIIIyyyy"


That's an artifact of Microsoft's debugger. Your pointer is pointing to a
garbage string, and the debugger knows it's a string, but not how long, so
it prints the string until it finds a NULL. The y's you are seeing are
also garbage, and not part of the memory allocated. The only reason the
display stops where it does is because there happens to be a zero byte
after the four y's.

And, as pointed out, make sure you allocate room for the terminating NUL
byte. And, in the future, please ask your C++ questions in some other
group.

--
#include <standard.disclaimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Per the FCA, this address may not be added to any commercial mail list
Nov 13 '05 #6
Gizmo wrote:
Prob solved thanks all was the '\0' that did it. sorry if i did poast in
wrong one i did in c instead of c++ cos i was using malloc.

thanks for the quick replys

Gizmo


1. Don't Top-Post. Replies are either interspersed or appended
at the bottom (like this reply).
2. Match *alloc() with free(), new with delete. Don't mix them up.
The C language only has *alloc and free.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 13 '05 #7
On Thu, 28 Aug 2003 12:28:10 -0700, Kevin D. Quitt <KQ****@IEEInc.com>
wrote in comp.lang.c:
On Thu, 28 Aug 2003 16:48:54 +0100, "Gizmo" <sc***********@hotmail.com>
wrote:
that is what im doing but when i come to look at it in debug mode it shows
as "IIIIIIIIyyyy"


That's an artifact of Microsoft's debugger. Your pointer is pointing to a
garbage string, and the debugger knows it's a string, but not how long, so
it prints the string until it finds a NULL. The y's you are seeing are

^^^^

I don't think Microsoft's debugger is looking for a null pointer
constant. ITYM "until it finds a NUL", "null character", or "null
byte".

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #8
On Fri, 29 Aug 2003 03:04:23 GMT, Jack Klein <ja*******@spamcop.net>
wrote:
I don't think Microsoft's debugger is looking for a null pointer
constant. ITYM "until it finds a NUL", "null character", or "null
byte".


You're quite right, and I'm astounded that I wrote NULL instead of NUL.
--
#include <standard.disclaimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Per the FCA, this address may not be added to any commercial mail list
Nov 13 '05 #9

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

Similar topics

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...
12
by: LongBow | last post by:
Hello all, From doing a google serach in the newsgroups I found out that a string can't be returned from a function, but using a char* I should be able to do it. I have spent most of the day...
3
by: sieg1974 | last post by:
Hi, I have made this simple program to understand char ** pointers, but I still having many questions. int main() { char ** testPointerPointerChar = 0; char * A = "string01";
4
by: Lawrie | last post by:
Hi, I am fairly new to C programming and would like to ask a little help with the following problem. I am writing a simple ring buffer, implemented as an array of char *. char* ringbuffer;...
12
by: confusedcoder | last post by:
Suppose I have the following function: void doSomething(char** array, int size); Normally, I would call the function with an array of, say, 3 elements... char* strs = { "str1","str2","str3"...
10
by: yogeshmk | last post by:
I need to break a longer string (with strtok()) and store the smaller strings in an array. since the number of small strings is not fixed/known, I cannot use a declaration like char *format, so I...
17
by: dtschoepe | last post by:
Hi, I have a homework project I am working on, so be forwarned, I'm new to C programming. But anyway, having some trouble with a memory allocation issue related to a char * that is a variable...
10
by: vignesh4u | last post by:
I am trying to implement the Split function in C ie. if i have a string: char* S="This is a test"; and if i try to split based on space ' ' it should return an array of strings like: ...
13
by: Kamui Shirow | last post by:
Hello! I will write a snippet to illustrate my doubt: snippet 1: void foo( int l ) { char* tmp; tmp = (char*) malloc( l * sizeof( char ) );
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.