×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Problem with HeapAlloc()

Problem with HeapAlloc()

Problem with HeapAlloc()

(OP)
Hello,
I use Visual Studio 2008, and I have a problem with allocating memory on windows.
This is the part of my code:

char** split_data(char* cache,int bounds)
{
char** parts=NULL;
int i=0,j=0,len=0;
len=strlen(cache);
if(bounds<=0||len==0){return NULL;}
if(bounds>0&&len>0){parts=(char**)HeapAlloc(GetProcessHeap(),(HEAP_NO_SERIALIZE|HEAP_ZERO_MEMORY),sizeof(char*)*bounds);}
if(!parts){error(9,hwnd_main);}
parts[0]=cache;
for(i=0;i<=len;i++)
{
if(cache[i]==SEP)
{
if(j==bounds)
{
cache[i]=0;
break;
}
else
{
j++;
parts[j]=&cache[i+1];
cache[i]=0;
}
}
}
return parts;
}

To explain:
cache parameter = "Jim~Maria~George~Mike~"
SEP = `~`
bound = 4
I want this fonction to return parts:
parts[0] = "Jim"
parts[1] = "Maria"
parts[2] = "George"
parts[3] = "Mike"

The strange thing is, that when I run my application by pushing the "Debug" button in Visual Studio, my function works, but when I run my application from Windows Explorer, either in Debug or Release, my application crushes:

A visual studio Just-in-Time debbuger windows appears
"An unhandled win32 exception occured in application_name.exe [3916]

Any idea on how to fix this issue ?

Thank you

RE: Problem with HeapAlloc()

Have you ever read HeapAlloc argument specification?

Quote (MS C++ Help):

HEAP_NO_SERIALIZE
Specifies that mutual exclusion will not be used
while the HeapAlloc function is accessing the heap.
This flag should not be specified when accessing the
process heap. The system may create additional threads
within the application's process, such as a CTRL+C handler,
that simultaneously access the process heap.
Why simple malloc was not used?
It's a good job for standard library strtok() function...

Apropos, it's not a good style to allocate memory in a function then return a pointer to it. Better pass an array allocated by a callee.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close