473,402 Members | 2,061 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,402 software developers and data experts.

Simple malloc problem with Visual C++ 2005 {Heap}

Hello, I am refreshing my memory on C as I've been in a C++
programming environment for a few months.

Why does the following code produce a heap protection error in Visual
C++ 2005?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]){

char
*newString = (char *)malloc(sizeof(char));
strcpy(newString, "2005");
printf("%s\n", newString);
free(newString);

return 0;
}

Nov 15 '05 #1
2 2395
AMT2K5 wrote:
Hello, I am refreshing my memory on C as I've been in a C++
programming environment for a few months.

Why does the following code produce a heap protection error in Visual
C++ 2005?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]){

char
*newString = (char *)malloc(sizeof(char));
First, lose the cast; this only obscures possible errors. C++ wants
this, C doesn't, and it's generally agreed it shouldn't be done.

Second, you've allocated memory for exactly one character...
strcpy(newString, "2005");


....and then copy 4 characters plus a terminating NUL to this area.

From C++ you're probably used to the idea that you can use 'string' and
get dynamic (re)allocation for free. In C you'll have to do it all
yourself, I'm afraid.

S.
Nov 15 '05 #2
AMT2K5 wrote:
Hello, I am refreshing my memory on C as I've been in a C++
programming environment for a few months.

Why does the following code produce a heap protection error in Visual
C++ 2005?


Your compiler is not relevant. Your code is broken:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
#if 0
char
*newString = (char *)malloc(sizeof(char));
/* There should be no cast above; since sizeof(char) == 1 by definition,
that makes a lot of the above line just typing practice, better spent
checking the return value from malloc. */
strcpy(newString, "2005");
printf("%s\n", newString);
#endif
printf("sizeof(char) = %lu (ALWAYS),\n"
"sizeof \"2005\" = %lu\n",
(long unsigned) sizeof(char), (long unsigned) sizeof "2005");
#if 0
free(newString);
#endif
return 0;
}

sizeof(char) = 1 (ALWAYS),
sizeof "2005" = 5
Nov 15 '05 #3

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

Similar topics

22
by: BekTek | last post by:
Hi.. I'm so sorry about that I've postes so many questions recently.. :) I'm still confused about the differences between malloc and operator new.. I know that when we work with class object and...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
8
by: Snis Pilbor | last post by:
First, let me announce that this is very possibly off-topic because malloc is a specific third party accessory to c, etc. I spent about an hour trying to find a more appropriate newsgroup and...
18
by: cs | last post by:
This is the function malloc_m() that should be like malloc() but it should find memory leak, and over bound writing in arrays. How many errors do you see? Thank you...
7
by: mef526 | last post by:
I have had this problem for months now and it has been nagging me. I have a large project that has several C++ DLL's, one of them uses malloc / calloc / free for the other DLL's. I process very...
15
by: Martin Jørgensen | last post by:
Hi, I have a (bigger) program with about 15-30 malloc's in it (too big to post it here)... The last thing I tried today was to add yet another malloc **two_dimensional_data. But I found out that...
41
by: jacob navia | last post by:
In the C tutorial for lcc-win32, I have a small chapter about a debugging implementation of malloc. Here is the code, and the explanations that go with it. I would appreciate your feedback...
37
by: ravi.cs.2001 | last post by:
Hi all, I m relatively new to C. I have few queries related to malloc(): #1. When we perform malloc(), the memory allocated dynamically comes from the heap area of the process in concern....
22
by: ravi | last post by:
Hi all, I m relatively new to C. I have few queries related to malloc(): 1. When we perform malloc(), the memory allocated dynamically comes from the heap area of the process in concern. Well,...
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: 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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.