我正在尝试编写一个函数,该函数返回从初始函数参数复制的指针,但在开始和结束时没有空格('','\ n','\ t')。
char *ft_strtrim(char const *s)
{
unsigned char *p;
int start;
int end;
size_t index;
start = 0;
end = ft_strlen(s) - 1;
while (s[start] == '\n' || s[start] == '\t' || s[start] == ' ')
start++;
while (s[end] == '\n' || s[end] == '\t' || s[end] == ' ')
end--;
if (start == 0 && end == ft_strlen(s) - 1)
return ((char*)s);
if (end - start < 0)
return (NULL);
printf("%d\n", end - start);
p = (char*)malloc(sizeof(char) * (end + 1 - start));
index = 0;
while(start <= end)
{
p[index] = s[start];
index++;
start++;
}
p[index] = '\0';
return (p);
}
我想在end-start的值低于0时返回NULL。尽管在我调用函数后使用的printf值为负,但函数不返回NULL,当我编译时代码我得到分段错误。
答案 0 :(得分:0)
当我找到问题的答案时,我真的不知道该怎么做,这只是我的第二个问题。
我改变了
中的条件public void ProcessRequest(HttpContext context)
{
_httpContext = context;
var imageid = context.Request.QueryString["Image"];
if (imageid == null || imageid == "")
{
imageid = "1";
}
using (WebClient wc = new WebClient())
{
// Handler retrieves the image from database and load it on the stream
using (Stream s = wc.OpenRead("http://mypageurl/Image.ashx?Image=" + imageid))
{
using (Bitmap bmp = new Bitmap(s))
{
AddFace(bmp);
}
}
}
}
public void AddFace(Bitmap image)
{
var faceImage = DetectFace(image);
if (faceImage != null)
{
var stream = new MemoryStream();
faceImage.Save(stream, ImageFormat.Bmp);
stream.Position = 0;
byte[] data = new byte[stream.Length];
stream.Read(data, 0, (int)stream.Length);
_httpContext.Response.Clear();
_httpContext.Response.ContentType = "image/jpeg";
_httpContext.Response.BinaryWrite(data);
}
}
private Bitmap DetectFace(Bitmap faceImage)
{
var image = new Image<Bgr, byte>(faceImage);
var gray = image.Convert<Gray, Byte>();
string filePath = HttpContext.Current.Server.MapPath("haarcascade_frontalface_default.xml");
var face = new HaarCascade(filePath);
MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(face, 1.1, 10, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20));
Image<Gray, byte> result = null;
foreach (MCvAvgComp f in facesDetected[0])
{
//draw the face detected in the 0th (gray) channel with blue color
image.Draw(f.rect, new Bgr(Color.Blue), 2);
result = image.Copy(f.rect).Convert<Gray, byte>();
break;
}
if (result != null)
{
result = result.Resize(200, 200, INTER.CV_INTER_CUBIC);
return result.Bitmap;
}
return null;
}
public bool IsReusable
{
get { return false; }
}
并用于结束并启动size_t类型并知道它有效。非常感谢你的评论。
答案 1 :(得分:0)
Code正在有效地执行前导和尾随空格的 trim ,但在许多条件下都有UB,包括所有空格。
while (s[end] == '\n' || s[end] == '\t' || s[end] == ' ')
end--;
以上代码导致未定义的行为,因为end < 0
和s[end]
是未定义的行为。
char *strtrim(char const *s) {
// First first non-white-space
while (isspace((unsigned char ) *s))
s++;
if (*s == 0) return NULL;
// Find last non-WS
const char *end = s;
const char *last_non_space = s;
while (*end) {
if (!isspace((unsigned char ) *end)) {
last_non_space = end;
}
end++;
}
// Allocate
size_t length = (size_t) (last_non_space - s + 1);
char *dest = malloc(length + 1);
if (dest == NULL) return NULL;
memcpy(dest, s, length);
dest[length] = '\0';
return dest;
}
答案 2 :(得分:0)
也许<td><input type="text" name="txtLEName" value="@attribute.Key" /></td>
而不是return (p);
?只是一个建议。