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

G

Gizmo

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
 
N

Nils Petter Vaskinn

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
 
G

Gizmo

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;
}
}
 
N

Nils Petter Vaskinn

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 && b) {
if (caseSensitive && (toUpper(a) != toUpper(b))) return i;
else if (a != b) 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
 
G

Gizmo

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
 
K

Kevin D. Quitt

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.
 
T

Thomas Matthews

Gizmo said:
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
 
J

Jack Klein

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
 
K

Kevin D. Quitt

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.
 

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
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top